问题描述
仍在 WCF 学习曲线上:
Still on the WCF learning curve:
我已经设置了一个自托管的 WCF 服务 (WSDualHttpBinding),它在我自己的计算机上运行良好,它位于防火墙后面.如果我在自己的计算机上运行客户端,则一切正常.
I've set up a self-hosted WCF Service (WSDualHttpBinding), which works fine on my own computer, which resides behind a firewall. If I run the client on my own computer, everything works great.
现在我在网络外的计算机上安装了客户端,并且我尝试通过动态 DNS 访问该服务,如下所示:http://mydomain.dyndns.org:8000/MyService代码>.我的端口转发问题在上一个问题中得到解决;我现在可以在浏览器中看到该服务已启动.
Now I installed the client on a computer outside my network, and I'm trying to access the service via a dynamic DNS, like so: http://mydomain.dyndns.org:8000/MyService
. My port forwarding issues were taken care of in a previous question; I can now see the service is up in my browser.
但是现在当我尝试在另一台机器上运行客户端时,我收到以下错误消息:在 00:01:00 分配的超时时间内未完成打开操作.分配给此操作的时间可能有是更长暂停时间的一部分."
But now when I try to run the client on the other machine, I get the following error message: "The open operation did not complete within the allotted timeout of 00:01:00. The time allotted to this operation may have been a portion of a longer timeout."
我已经禁用了服务的安全性,所以不是这样.还有什么可能阻止连接发生?
I have disabled security on the service, so that's not it. What else might be preventing the connection from happening?
推荐答案
WSDualHttpBinding
和大多数人连接到互联网的方式存在一个真正的问题 - 在路由器后面意味着,至少正如您已经发现的那样,IPv4,让 NAT 毁了派对.
There's a real problem with WSDualHttpBinding
and the way most people are connected to the internet - being behind a router means, at least with IPv4, having NAT ruin the party, as you've already discovered.
使用 WSDualHttpBinding
,您有两个连接:从客户端到服务器,以及从服务器到客户端.
With WSDualHttpBinding
, you have two connections: From the client to the server, and from the server to the client.
通常,客户端到服务器的连接没什么大不了的 - 大多数通信都是通过互联网完成的.在您的情况下,您似乎在防火墙后面,并且您已经打开/转发了所需的端口.但这并没有解决第二个连接的问题——从服务器到客户端.基本上,第二个连接发生的情况是客户端充当服务器,而服务器充当客户端.因此,您需要对连接到您的服务的每个客户端进行相同的端口打开/转发,因为它也充当服务器!这当然是对您服务的每个用户的不合理要求.这就是 WSDualHttpBinding
更适合服务器到服务器通信的原因,其中设置是一次性的.
Usually, the client-to-server connection isn't a big deal - that's how most communication is done over the internet. In your case, it seems that you were behind a firewall and you've opened/forwarded the needed ports. But that doesn't solve the problem of the second connection - from the server to the client. Basically what happens with that second connection is that the client acts as a server, and the server acts as a client. So you need to do the same port opening/forwarding with each and every client that connects to your service, because it also acts as a server! This is of course an unreasonable demand to make of every user of your service. That's why WSDualHttpBinding
is more suited to server-to-server communications, where the setup is a one-time affair.
与其尝试让 WSDualHttpBinding
工作,我建议您切换到 NetTcpBinding
.由于 WSDualHttpBinding
和 NetTcpBinding
都是 WCF-only、Microsoft-only、专有的连接方案,所以在互操作性方面不会损失太多.另一方面,您获得的却很多:
Instead of trying to get WSDualHttpBinding
to work, I suggest you switch to NetTcpBinding
. Since both WSDualHttpBinding
and NetTcpBinding
are WCF-only, Microsoft-only, proprietary connection schemes, you're not losing much in the way of interoperability. What you're gaining, on the other hand, is a lot:
NetTcpBinding
只使用一个连接,从客户端到服务器,同时允许像WSDualHttpBinding
这样的双向通信.因此无需在客户端处理端口打开/转发 - NAT 不是问题.- 通信协议是二进制的,比
WSDualHttpBinding
中使用的纯文本 XML 更紧凑.更少的数据传输意味着更好的服务. - 使用
NetTcpBinding
,您可以在客户端断开连接时获得即时通知,因为套接字已关闭.无需像使用WSDualHttpBinding
那样等待 HTTP 超时. - 单个连接意味着没有任何东西可以不同步 - 使用
WSDualHttpBinding
,两个连接之一可能会断开,而另一个可能仍然处于活动状态,只有一种方式通信.WCF 有办法解决这个问题,但最好首先避免这个问题.
NetTcpBinding
uses only a single connection, from the client to the server, while allowing two way communication likeWSDualHttpBinding
. So there's no need to deal with port opening/forwarding on the client side - NAT is a non-issue.- The communication protocol is binary and is more compact than the plain-text XML used in
WSDualHttpBinding
. Less data transfer means a better performing service. - With
NetTcpBinding
, you can get instant notification of when a client disconnects, since a socket is closed. No need to wait for a HTTP timeout like you do withWSDualHttpBinding
. - A single connection means there nothing that can go out of sync - with
WSDualHttpBinding
, one of the two connections may drop while the other may still be active, having only one way communication. WCF has a way of dealing with that, but it's better to just avoid the issue in the first place.
切换到 NetTcpBinding
通常只需要更改配置 - 代码保持不变.这很简单,速度很快,麻烦也少得多,最重要的是 - 它很管用.
Switching to NetTcpBinding
usually only requires a configuration change - the code remains the same. It's simple, it's fast, it's much less of a hassle and most importantly - it just works.
这篇关于使用 wsDualHttpBinding 通过 Internet 连接到 WCF 服务超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!