Enabling Outbound WCF Requests Using a Specific Network Interface

I have a remote WCF web service that I'm connecting to from my application.

An application can run on a server with multiple IP addresses (or multiple physical network interfaces)

I need to make sure that I can control which IP address is used for the outgoing request, instead of just using the "preferred" interface in accordance with normal metric rules.

The reason for this is that several copies of the software will work on one computer, each of which is tied to a specific IP address for its own operations, and the remote service to which it is connected must know which one is used to connect to it at a later time (since the wrong address means connecting to the wrong service)

With legacy ASMX services, this is done by overriding GetWebRequest(Uri uri) for the partial class generated for the service. But I can’t figure out how to do this with WCF.

In an unrelated SO post , MVP @JohnSaunders suggested that this is possible if you take the whole transport mechanism used by WCF. But I still do not understand how to do this.

+6
source share
3 answers

This is a complex issue that WCF doesn't seem to be serving very well.

The only component in the .NET platform that appears to be directly concerned with the client address problem is ServicePoint . In particular, it has a BindIPEndPointDelegate property that allows you to control how it selects the client IP address. The documentation for this property includes the following:

Some load balancing methods require the client to use the specific local IP address and port number, not IPAddress.Any (or IPAddress.IPv6Any for Internet Protocol Version 6) and the ephemeral port. Your BindIPEndPointDelegate can satisfy this requirement.

Therefore, you should be able to change the service point associated with your URL as follows:

 var servicePoint = ServicePointManager.FindServicePoint( new Uri("http://contoso.com/service.svc")); servicePoint.BindIPEndPointDelegate = (sp, remote, retryCount) => new IPEndPoint(address, portNumber); 

Obviously, this type of code requires your classes to learn about the protocol and end address the client will interact with. Most likely, it would be most appropriate to configure this logic as client behavior that can be applied to your client channel.

+3
source

you can use the message property (HttpRequestMessageProperty) to add HTTP headers to any outgoing requests. You need to create a “region” in which the property will be added to the current “operation context”, and attach the property with all the headers you want to the properties of the outgoing context message.

please look at this:

how-to-override-getwebrequest-method-using-service-reference-instead-of-web-reference-in-wcf

0
source

Use this: New class:

 using System.Web.Services.Protocols; using System.Windows.Forms; using System; public static class ExtensionMethods { public static string ApplyServerURL(this SoapHttpClientProtocol service) { try { string name = service.GetType().Name; return string.Format("{0}{1}.svc", Settings.Instance.ServerAddress, name); } catch { return string.Empty; } } } 

And here is something like this:

 YourService us = new YourService(); us.Url = us.ApplyServerURL(); 
0
source

Source: https://habr.com/ru/post/949028/


All Articles