none
Get Client IPv4 address from WCF server app RRS feed

  • Question

  • Hi,

    I want to be able to capture and record the IPv4 address that an instance of my client app is being used from. It's the public internet address I'd like to capture - a bit like when you go to www.grc.com, and it tells you that you are using the program from xx.xx.xx.xx.

    I've seen numerous suggestions as to how to do this - but all I've managed to do is to get an IPv6 address.

    Thanks

    John


    Cheers, John

    Sunday, July 26, 2015 12:39 AM

Answers

  • Hi j_dublevay,

    According to this case, I'd like to suggest you to use RemoteEndpointMessageProperty class to get the IP and Port.

    Code shown below:

    public class WCFService : IWCFService
    {
            public string SayHelloToUser(string name)
             {
                OperationContext context = OperationContext.Current;
                MessageProperties properties = context.IncomingMessageProperties;
                RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
                 Console.WriteLine(string.Format("Hello {0},You are  from {1}:{2}", name, endpoint.Address, endpoint.Port));
                 return string.Format("Hello {0},You are  from {1}:{2}", name, endpoint.Address, endpoint.Port);
             }
    }

    I hope that will be helpful to you.

    Best Regards,

    Tracy Dj





    Monday, July 27, 2015 2:01 AM
    Moderator

All replies

  • Hi j_dublevay,

    According to this case, I'd like to suggest you to use RemoteEndpointMessageProperty class to get the IP and Port.

    Code shown below:

    public class WCFService : IWCFService
    {
            public string SayHelloToUser(string name)
             {
                OperationContext context = OperationContext.Current;
                MessageProperties properties = context.IncomingMessageProperties;
                RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
                 Console.WriteLine(string.Format("Hello {0},You are  from {1}:{2}", name, endpoint.Address, endpoint.Port));
                 return string.Format("Hello {0},You are  from {1}:{2}", name, endpoint.Address, endpoint.Port);
             }
    }

    I hope that will be helpful to you.

    Best Regards,

    Tracy Dj





    Monday, July 27, 2015 2:01 AM
    Moderator
  • Thank you Tracy Dj. That worked perfectly.

    I actually was coding in vb.net, so used the following code, in case anyone is searching in future.

            Dim oc As OperationContext = OperationContext.Current
            Dim mp As MessageProperties = oc.IncomingMessageProperties
            Dim remp As RemoteEndpointMessageProperty = mp(RemoteEndpointMessageProperty.Name)
            strClientIP = remp.Address.ToString & ":" & remp.Port.ToString


    Cheers, John

    Monday, July 27, 2015 9:17 PM