views:

166

answers:

1

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?

+1  A: 

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!

mattdekrey
That looks alot like a step in the right direction! It's in the *TransportBinding. Setting it to Exact makes it bind to the given IP (which it did not before), but unfortunately doesn't prevent it from binding on 0.0.0.0, too. (That looks alot like another error to me, though.) I'm curious if it does the trick for you!
Markus
Weird, so now you're seeing it bound on both the specific and the general IPs...? We have both Server 2003 and 2008, and I'll get my sysadmin to try it on both, so we'll see if we can get this resolved. They REALLY want to use IP binding rather than port binding, and I can't blame them.
mattdekrey
Indeed. Could be that it only binds to "any" because it can, haven't checked that yet.
Markus
(The problem is not solved yet, but I accepted this as the answer.)
Markus
I still plan on checking, I have not forgotten. Thanks for the accept.
mattdekrey