如何确定控制台应用程序中托管的 WCF 服务是否已启动并正在运行?

发布于 2025-01-04 08:22:39 字数 1245 浏览 2 评论 0原文

我有一个由控制台应用程序托管的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有木有妳兜一样 2025-01-11 08:22:39

您可以让控制台应用程序发出信号 ServiceHost 打开时的事件


更新

您的起始代码应该在 WaitHandle 的实例上调用 WaitOne:

EventWaitHandle evtServiceStarted = new EventWaitHandle(...);

Process hostProcess = Process.Start(info); 

//make sure the service is up and running
evtServiceStarted.WaitOne();

// Go ahead and call your service...

您的服务主机应该在指向同名事件对象的 WaitHandle 实例上调用 Set:

EventWaitHandle eventWaitHandle = EventWaitHandle.OpenExisting(...);
// Set up the service host and call Open() on it

//... when its all done
eventWaitHandle.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:

EventWaitHandle evtServiceStarted = new EventWaitHandle(...);

Process hostProcess = Process.Start(info); 

//make sure the service is up and running
evtServiceStarted.WaitOne();

// Go ahead and call your service...

Your service host should call Set on a WaitHandle instance pointing to the same named event object:

EventWaitHandle eventWaitHandle = EventWaitHandle.OpenExisting(...);
// Set up the service host and call Open() on it

//... when its all done
eventWaitHandle.Set();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文