views:

72

answers:

3

I want my request to go out through a specific IP Addresses. Is there a way to do that in WCF. The explanation of why I need this is a little long winded so i'd rather not get into that.

Here is 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 scenario to explain exactly what i'm looking for. Let's say I have two IPs on my machine (192.168.0.30 and 192.168.0.31). Both of them can hit 192.168.0.32. If i run this code now, it will hit the IP (.32) from any of my IPs (.30 or .31). How can i force it to go through a specific IP of mine (say .30). Is there any way to do that using WCF?

Thanks

A: 

It seems to me that your problem should be solved by setting of an additional rote in the routing table. Try do following from the Command Prompt started with administrative rights:

route add 192.168.0.32 mask 255.255.255.255 192.168.175.30

If you want to save the route add -p switch additionally.

Oleg
I'm more trying to find how to bind a WCF client to a specific outbound IP within WCF. How would i explicitely bind it. Moreover, I may not be running with administrative privaleges. Also, this is an application that's going to be used on many machines, i'm not sure if it makes sense to programmatically play with the routing table everytime I want to make these requests.
Mark
In general one define binding to a specific address **only for the server** application. Then the server IP will be known. The client just make a connection to this IP using the routing table. The routing table defines through which interface an IP address can be archived. At least the interface metric say this. So if you has so exotic IP addresses on your IP segment, administrator can define one time (`route -p ...`) how the destination IP should be arrived. This information will be persistent and will not changed after rebooting or login another user.
Oleg
It is true that in general one defines the binding only for the server. But for Sockets for example, you can define the binding for the Client. My question is it is possible to do that from WCF. It is a WCF question.
Mark
I have not mush expirience with Sockets, but I used always `bind` before `listen` on the server side. On the client side I used only `connect` with `sockaddr_in` initialized with information about the server only. No call of `bind` on the client side is needed (see also http://msdn.microsoft.com/en-us/library/ms737548.aspx). So I followed with Sockets the same way as decsribed before with WCF.
Oleg
No bind is needed, but binding should be possible. I want to bind. The question is not whether one should bind or not. Nor is the question whether the same thing can be accomplished in a different way. The question is how can we bind on the client side in WCF?
Mark
I explained you that in *my understanding* the binding on the client side is **NOT POSSIBLE in any technique** including WCF and Sockets. If you find a reference in any technique how it is do possible please post me the corresponding reference URL.
Oleg
A: 

Are you trying to make something similar to IP-Sec on the Server so it only accepts request 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;
    }
Aliostad
+1  A: 

The answer to the question is that it cannot be done. Here is the answer from a Microsoft MVP


So you want to let the client-side machine proactively select one of the network adpater interface(installed on it) to send out the WCF requests? I'm afraid this is out of WCF's control since WCF only focus on the following addresses:

** when behave as a host, we can choose to bind to a specific hostname/address to listen for client requests ** when behave as a client, we can choose the destination address/hostname to send request to.


Mark
If it can't be done, and someone opens a feature request, I will vote for said feature.
Justin Dearing