サービス定義
SearchService.cs
using System.ServiceModel;
namespace Hoge
{
[ServiceContract]
public interface ISearchService
{
[OperationContract]
void Execute();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
public class SearchService : ISearchService
{
public delegate void Searcher();
public Searcher SercherDelegate;
public void Execute()
{
SercherDelegate(); // 実装はデリゲートします
}
}
}
サービス起動
SearchService service = new SearchService();
service.SercherDelegate = hogeForm.SearchRequest; // デリゲート登録
serviceHost = new ServiceHost(
service,
new Uri("net.pipe://localhost/Hoge"));
try
{
serviceHost.AddServiceEndpoint(
typeof(ISearchService),
new NetNamedPipeBinding(),
"SearchService");
serviceHost.Open();
}
catch (AddressAlreadyInUseException)
{
MessageBox.Show("既にサービスは起動しています。");
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
サービス呼び出し(クライアント)
new ChannelFactory<ISearchService>(
new NetNamedPipeBinding(),
new EndpointAddress("net.pipe://localhost/Hoge/SearchService")).CreateChannel()
.Execute();