views:

35

answers:

1

Hello.

i'm using next piece of code to get client ip on wcf service :

        OperationContext context = OperationContext.Current;
        System.ServiceModel.Channels.MessageProperties prop = context.IncomingMessageProperties;
        System.ServiceModel.Channels.RemoteEndpointMessageProperty endpoint = prop[System.ServiceModel.Channels.RemoteEndpointMessageProperty.Name] as System.ServiceModel.Channels.RemoteEndpointMessageProperty;
        string ip = endpoint.Address;

while this code worked on iis6/server2003 everything were ok, endpoint.Address returned ipv4. but after i recently updated to iis7/server2008 endpoint.Address is returning ipv6.

is it still possible to get ipv4 on iis7/server2008 ?

Thank You !

A: 

This is not so much of a change in WCF that is a change in networking. Your client has used its IPv6 to connect to the server and that is the address which is stored in the context of the message. If you need to get hold of IPv4, use the snippet below:

    IPAddress ipAddress = IPAddress.Parse(ipv6);
    IPHostEntry ipHostEntry = Dns.GetHostEntry(ipAddress);
    foreach (IPAddress address in ipHostEntry.AddressList)
    {
           if(address.AddressFamily == AddressFamily.InterNetwork)
                  Console.WriteLine(address);
    }

This will translate your IPv6 to IPv4.

Aliostad