【发布时间】:2012-02-11 01:28:24
【问题描述】:
我有一个由控制台应用程序托管的 WCF 服务。 客户端通过命名管道连接到服务。 并且控制台只有在客户端需要时才会执行,并且在客户端完成后控制台会被杀死。
下面是启动和调用服务的代码:
Process hostProcess = Process.Start(info);
//make sure the service is up and running
//todo: find out a better way to check if the service is up and running.
Thread.Sleep(200);
EndpointAddress endpointAddress = new EndpointAddress("net.pipe://localhost/test");
NetNamedPipeBinding binding = new NetNamedPipeBinding();
IHostedService service=hannelFactory<IHostedService>.CreateChannel(binding, endpointAddress);
service.Run();
hostProcess.Kill();
我正在使用 Thread.Sleep 来确保服务启动并运行,但这绝对不是正确的做法。
那么,如何确定托管在控制台应用程序中的 WCF 服务是否已启动并正在运行?
后续问题,如何在不使用 Thread.Sleep 的情况下等待事件触发?
private static EventWaitHandle GetEventWaitHandle()
{
try
{
EventWaitHandle eventWaitHandle = EventWaitHandle.OpenExisting(string.Format(serviceStartedEventName, taskIndex));
return eventWaitHandle;
}
catch (Exception)
{
//if we do not sleep here, it may cause a stack over flow exceptoin.
Thread.Sleep(10);
return GetEventWaitHandle();
}
}
【问题讨论】:
-
你能检查托管它的进程是否正在运行吗? stackoverflow.com/questions/1276629/…
-
@kenny 进程正在运行并不意味着服务已启动。服务启动通常需要一点时间。
标签: wcf console named-pipes wcf-hosting