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.
With WSDualHttpBinding
, you have two connections: From the client to the server, and from the server to the client.
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.
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
uses only a single connection, from the client to the server, while allowing two way communication like WSDualHttpBinding
. 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 with WSDualHttpBinding
.
- 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.
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…