How to associate a WCF Http client with a specific outbound IP address before making a request

I want my request to go through specific IP addresses. Is there any way to do this in WCF. The explanation why I need it is a bit long, so I would not go into it.

Here is a sample code

string ipAddress = "192.168.0.32"; IService service; ChannelFactory<IOmlService> factory = new ChannelFactory<IService>(new BasicHttpBinding(), new EndpointAddress("http://" + IPAddress + ":6996/IService")); service = factory.CreateChannel(); service.Test(); 

Here is an example script to explain exactly what I'm looking for. Let's say I have two IP addresses on my machine (192.168.0.30 and 192.168.0.31). Both of them can suffer 192.168.0.32. If I run this code now, it will hit IP (.32) from any of my IP addresses (.30 or .31). How can I make it go through a specific IP address of mine (say 0.30). Is there a way to do this using WCF?

+4
source share
3 answers

The answer to the question is that this is not possible. Here is the answer from Microsoft MVP


So, do you want the client machine to proactively select one of the adpater network interfaces (installed on it) to send WCF requests? I am afraid that this is because of WCF control, since WCF focuses only on the following addresses:

** when they behave as a host, we can choose a binding to a specific host name / address to listen to client requests ** when they behave as a client, we can choose a destination address / host name to send a request.


+2
source

Are you trying to do something similar to IP-Sec on the server so that it only accepts requests from specific IP addresses?

In this case, you need to implement IEndpointBehavior and IDispatchMessageInspector and:

  public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) { RemoteEndpointMessageProperty remoteAddress = (OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty); // validate ip here return null; } 
+1
source

It seems to me that your problem should be solved by installing additional rote in the routing table. Try the following command from a command prompt with administrator privileges:

 route add 192.168.0.32 mask 255.255.255.255 192.168.175.30 

If you want to keep the add -p route optional.

0
source

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


All Articles