C# 如何检查 net.tcp WCF 服务的可用性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/881754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:15:37  来源:igfitidea点击:

How to check the availability of a net.tcp WCF service

c#wcfnet.tcp

提问by Pablo Retyk

My WCF server needs to go up and down on a regular basis, the client sometimes uses the server, but if it is down the client just ignore it. So each time I need to use the server services I check the connection state and if it's not open I open it. The problem is that if I attempt to open while the server is down there is a delay which hits performance. My question is, is there a way to do some kind of myClient.CanOpen()? so I'd know if there is any point to open the connection to the server.

我的 WCF 服务器需要定期启动和关闭,客户端有时会使用服务器,但如果它关闭,客户端只需忽略它。所以每次我需要使用服务器服务时,我都会检查连接状态,如果它没有打开,我就会打开它。问题是,如果我在服务器关闭时尝试打开,则会出现延迟,从而影响性能。我的问题是,有没有办法做某种事情myClient.CanOpen()?所以我知道是否有任何点可以打开与服务器的连接。

采纳答案by Anderson Imes

There is an implementation of WS-Discovery that would allow you to listen for up/down announcements for your service. This is also a very convenient form of service address resolution because it utilizes UDP multicast messages to find the service, rather than configuring one set address on the client. WS-Discovery for WCF

有一个 WS-Discovery 实现,它允许您收听服务的启动/关闭公告。这也是一种非常方便的服务地址解析形式,因为它利用 UDP 多播消息来查找服务,而不是在客户端配置一组地址。 WCF 的 WS-Discovery

There's also an implementation done by a Microsoft employee: WS-Discovery Sample Implementation

还有一个由微软员工完成的实现: WS-Discovery Sample Implementation

.NET 4.0 will include this natively. You can read about .NET 4.0's implementation on Jesus Rodriguez's blog. It has a great chart that details the ad-hoc communication that goes on in WS-Disco Using WS-Discovery in WCF 4.0

.NET 4.0 将本机包含此内容。您可以在 Jesus Rodriguez 的博客上阅读 .NET 4.0 的实现。它有一个很好的图表,详细说明了在 WS-Disco 中使用 WCF 4.0 中的 WS-Discovery 进行的临时通信

Another thing you might consider, especially if your messages are largely one-way, is a protocol that works natively disconnected, like MSMQ. I don't know what your design for your application looks like, but MSMQ would allow a client to send a message regardless of the state of the service and the service will get it when it comes back up. This way your client doesn't have to block quite so much trying to get confirmation that a service is up before communicating... it'll just fire and forget.

您可能会考虑的另一件事,尤其是当您的消息主要是单向的时,是一种本机断开连接的协议,例如 MSMQ。我不知道您的应用程序设计是什么样的,但是 MSMQ 将允许客户端发送消息而不管服务的状态如何,并且服务将在它恢复时获取它。这样您的客户端就不必在通信之前阻止太多尝试确认服务已启动......它只会触发并忘记。

Hope this helps.

希望这可以帮助。

回答by Konstantinos

I don't think it's possible doing a server side call to your Client to inform him that you the service has been started ... Best method i can see is having a client method figuring out where or not the service is open and in good condition. Unless I am missing some functionality of WCF ...

我认为不可能对您的客户进行服务器端调用以通知他您的服务已启动......我能看到的最佳方法是让客户端方法确定服务在何处打开并处于良好状态健康)状况。除非我缺少 WCF 的某些功能......

There is a good blogpost WCF: Availability of the WCF servicesif you are interested in a read.

如果您有兴趣阅读,有一篇很好的博客文章WCF: Availability of the WCF services

回答by Tormod

If you are doing a synchronous call expecting a server timeout in an application with a user interface, you should be doing it in another thread. I doubt that the performance hit is due to exception overhead. Is your performance penalty in CPU load, gui availability or wall clock time?

如果您在具有用户界面的应用程序中执行同步调用并期望服务器超时,则您应该在另一个线程中执行此操作。我怀疑性能下降是由于异常开销造成的。CPU 负载、GUI 可用性或挂钟时间是否会降低您的性能?

You could investigate to see if you can create a custom binding on TCP, but with faster timeout.

您可以调查是否可以在 TCP 上创建自定义绑定,但超时更快。

I assume you know that "IsOneWay=true" is faster than request->response in your case because you wouldn't be expecting a response anyway, but then you are not getting confirmation or return values. You could also implement a two-way communication that is not request->response.

我假设您知道在您的情况下“IsOneWay=true”比 request->response 更快,因为无论如何您都不会期待响应,但是您没有得到确认或返回值。您还可以实现不是请求-> 响应的双向通信。

回答by Jon

If you were in a local network it might be possible to broadcast a signal to say that a new server is up. The client would need to listen for the broadcast signal and respond accordingly.

如果您在本地网络中,则可能会广播一个信号,表明新服务器已启动。客户端需要监听广播信号并做出相应的响应。

回答by Dave_B

Here's what I'm using and it works like a charm. And btw, the ServiceController class lives in namespace 'System.ServiceProcess'.

这就是我正在使用的,它就像一个魅力。顺便说一句,ServiceController 类位于命名空间“System.ServiceProcess”中。

try
{
    ServiceController sc = new ServiceController("Service Name", "Computer's IP Address");
    Console.WriteLine("The service status is currently set to {0}",
        sc.Status.ToString());

    if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) ||
        (sc.Status.Equals(ServiceControllerStatus.StopPending)))
    {
        Console.WriteLine("Service is Stopped, Ending the application...");
        Console.Read();
        EndApplication();
    }
    else
    {
        Console.WriteLine("Service is Started...");
    }
}
catch (Exception)
{
    Console.WriteLine("Error Occurred trying to access the Server service...");
    Console.Read();
    EndApplication();
}