On a machine with multiple network cards I need to bind a WCF webservice to a specific network interface. It seems that the default is to bind on all network interfaces.
The machine has two network adapters with the IPs 192.168.0.10
and 192.168.0.11
. I have an Apache running that binds on 192.168.0.
10
:80
and need to run the webservice on 192.168.0.
11
:80
. (Due to external circumstances I cannot choose another port.)
I tried the following:
string endpoint = "http://192.168.0.11:80/SOAP";
ServiceHost = new ServiceHost(typeof(TService), new Uri(endpoint));
ServiceHost.AddServiceEndpoint(typeof(TContract), Binding, "");
// or: 开发者_如何学运维ServiceHost.AddServiceEndpoint(typeof(TContract), Binding, endpoint);
But it doesn't work; netstat -ano -p tcp
always shows the webservice listening on 0.0.0.0:80
, which is all interfaces (if I got that correct). When I start Apache first, it correctly binds to the other interface, which in turn prevents the WCF service to bind to "all".
Any ideas?
We are having a similar issue at my workplace, and was doing research for it when I stumbled upon your post. I haven't gotten a chance to try it yet, but plan to when we get the chance: there is a "hostNameComparisonMode" on the Binding that, when set to "Exact", is supposed to always obey your setting. (The default allows it to go to a wildcard if no match can be found.)
If you get a chance to try this before I do, please let me know the results. Otherwise, I'll update my answer and let you know!
精彩评论