March 13, 2012 at 5:51 PM
If you're accessing a remote .Net web service where the remote server checks the IP address of the incoming request, either for authentication or some other reason, you can quickly run into some interesting problems on servers with multiple IP addresses, either on different physical interfaces or multiple addresses routed to the same NIC.
So there are some cases where you need to change which of a machines local IP addresses a request will be made from.
While doing this with .Net web services isn't massively complicated, it is tricky finding out any documentation on how to do it.
In this example, I have a remote .Net web service called StatsReporter. I've added the reference and have the generated code. Now I need to extend it:
public partial class StatsReporter : System.Web.Services.Protocols.SoapHttpClientProtocol
protected override System.Net.WebRequest GetWebRequest(Uri uri)
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(uri);
Request.ServicePoint.BindIPEndPointDelegate = BindIPEndPointCallback;
Request.Proxy = null;
public IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
IPAddress IPEndPoint = IPAddress.Any;
IPEndPoint = IPAddress.Parse(GetBindIPAddress());
return new IPEndPoint(IPEndPoint, 0);
The original generated code is implemented as a partial class, so the above code can be added to a new file. You'll need to implement GetBindIPAddress() yourself to return the IP that you actually want to use. But aside from that it works.
So what's going on? All you're doing is replacing the WebRequest object that gets created when a service call is made and replacing it with your own. One that has its BindIPEndPointDelegate set to one that will return the IP address to bind to.
In this example I deliberately set the proxy to null to avoid having the service go and 'look' for a proxy server during the first call (which can cause a significant delay) - if your code is going to operate in an environment where non-transparent proxies are being used, you'll want to remove that line.