如何使用来自远程服务器的 net.tcp 端点 ping 或检查 WCF 服务的状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6310153/
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
How to ping or check status of WCF service using net.tcp endpoint from remote server?
提问by atconway
I really come from the world of Httpand never did much with the old .NET Remoting which used TCP, but I understand the TCP concepts and now have implemented several WCF services using the net.tcp binding over the last few years. Most of the time it is up, running, I consume it, end of story. However sometimes the server setup is more advanced and I get communication errors that exist on 1 server and maybe not from another. To prove if it is a firewall/server/etc. issue I need to see if the WCF service can even be seen or reached without issue. This is for a Windows Service hosted WCF service using net.tcp that I am trying to figure out this situation.
我真的来自Http使用 TCP 的旧 .NET Remoting的世界,并且从来没有做过太多,但我了解 TCP 概念,并且在过去几年中使用 net.tcp 绑定实现了几个 WCF 服务。大部分时间它都启动,运行,我消费它,故事结束。但是,有时服务器设置更高级,并且我会收到一台服务器上存在的通信错误,而另一台服务器可能没有。证明它是否是防火墙/服务器/等。问题 我需要查看 WCF 服务是否可以毫无问题地被看到或访问。这是针对使用 net.tcp 的 Windows 服务托管的 WCF 服务,我试图找出这种情况。
The thing is with a WCF service exposed via a HTTP binding, I can just plop the URI in the browser to view the service page letting me know the service is running properly. Easy test.
事情是通过 HTTP 绑定公开的 WCF 服务,我可以在浏览器中插入 URI 来查看服务页面,让我知道服务正在正常运行。轻松测试。
How do I do the equivalent for a WCF service exposed via a net.tcp binding? Is there any tool or command I can use to test for example net.tcp//mycustomWCFService:8123/MyService? I have seen a few posts on writing code to programmatically determine if the WCF service is available but I do not want to do it this way. I want to do this check if at all possible withoutcode, analogous to me pulling up the http endpoint in a browser.
如何为通过 net.tcp 绑定公开的 WCF 服务执行等效操作?例如,是否有任何工具或命令可用于测试net.tcp//mycustomWCFService:8123/MyService?我看过一些关于编写代码以编程方式确定 WCF 服务是否可用的帖子,但我不想这样做。如果可能的话,我想在没有代码的情况下进行此检查,类似于我在浏览器中拉出 http 端点。
Any help is appreciated, thank you!
任何帮助表示赞赏,谢谢!
回答by Jason Shantz
If your service implements a metadata endpoint (typically named mexand nested beneath the principal endpoint, implemented using the mexTcpBindingin this case), you can "ping" it using the svcutil command line utility that's provided with Visual Studio. E.g.,
如果您的服务实现了元数据终结点(通常命名mex并嵌套在主体终结点之下,mexTcpBinding在本例中使用 实现),您可以使用 Visual Studio 提供的 svcutil 命令行实用程序“ping”它。例如,
svcutil net.tcp://mycustomWCFService:8123/MyService/mex
If it throws an error, your service is (potentially) down. If it succeeds, you're (likely) in business. As the preceding parentheticals suggest, it's an approximation. It means there's a listener at the address and that it's able to service a metadata request.
如果它引发错误,则您的服务(可能)已关闭。如果成功,你(很可能)在做生意。正如前面的括号所暗示的那样,这是一个近似值。这意味着该地址有一个侦听器,并且它能够为元数据请求提供服务。
回答by atconway
Another way I found to at least see if it is listening is to issue the following 'netstat' command (from a command prompt) on the installed server:
我发现至少可以查看它是否正在侦听的另一种方法是在已安装的服务器上发出以下“netstat”命令(从命令提示符):
netstat -ona | find "8123"
netstat -ona | 找到“8123”
(Yes that is a pipe delimiter in the command above). If anything is returned it is actively listening and hosted on the searched port.
(是的,这是上面命令中的管道分隔符)。如果返回任何内容,它会主动侦听并托管在搜索到的端口上。
回答by mthierba
A simple way of doing that would be (assuming the service is hosted in IIS) to add a HTTP binding (using a different port!!) to the same IIS site and to add <serviceDebug httpHelpPageEnabled="true" />to the service behaviors. That way you can easily check that the service is up by navigating to its HTTP URL in a browser. Admittedly, that way you can only find out whether the service is up or not, you wouldn't be able to detect any networking issues on the particular TCP port, for instance.
一种简单的方法是(假设服务托管在 IIS 中)将 HTTP 绑定(使用不同的端口!!)添加<serviceDebug httpHelpPageEnabled="true" />到同一个 IIS 站点并添加到服务行为。这样,您可以通过在浏览器中导航到其 HTTP URL 来轻松检查服务是否已启动。诚然,这样您只能确定服务是否已启动,例如,您将无法检测到特定 TCP 端口上的任何网络问题。
If you need to address the latter issue as well, or if adding a HTTP binding is not possible you could just add a simple "PING" operation to the service contract and use a net.tcp client to invoke that one.
如果您还需要解决后一个问题,或者如果添加 HTTP 绑定是不可能的,您可以向服务合同添加一个简单的“PING”操作并使用 net.tcp 客户端来调用该操作。
回答by Marty Grogan
You can also use the telnet client program to discover listening ports, i.e., telnet {url or IP} {port}. To check if a web site is up.. 'telnet www.godaddy.com 80' should initially produce a blank window followed by a Request Time-out response (400) if no commands follow.
您也可以使用 telnet 客户端程序来发现监听端口,即 telnet {url or IP} {port}。要检查网站是否已启动.. 'telnet www.godaddy.com 80' 最初应生成一个空白窗口,然后是请求超时响应 (400),如果没有命令跟随。

