如何确定控制台应用程序中托管的 WCF 服务是否已启动并正在运行?
我有一个由控制台应用程序托管的 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();
}
}
I have a WCF Service that is hosted by a Console Application.
The client connects to the Service through a named pipe.
And the Console only gets executed when the client needs it, and the console gets killed after the client is done.
Here is the code that starts and calls the service:
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();
I am using Thread.Sleep to make sure the service is up and running, but that is definitely not the right approach to do so.
So, how can I determine if a WCF service that is hosted in a Console application is up and running?
follow up question, how can i wait for the event to be fired without using 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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以让控制台应用程序发出信号 ServiceHost 打开时的事件。
更新
您的起始代码应该在 WaitHandle 的实例上调用 WaitOne:
您的服务主机应该在指向同名事件对象的 WaitHandle 实例上调用 Set:
您的服务主机不应尝试多次打开事件 - 您的起始代码需要确保在启动服务应用程序之前创建事件(并具有正确的安全权限)。
You could get the console application to signal an event when its ServiceHost has been opened.
UPDATE
Your starter code should call WaitOne on an instance of your WaitHandle:
Your service host should call Set on a WaitHandle instance pointing to the same named event object:
Your service host shouldn't try to open the event more than once - your starter code needs to make sure the event is created (and with the right security permissions) before it kicks off the service application.