none
get client IP, port and application name in wcf web service hosted in IIS 8 RRS feed

  • Question

  • i use this code to get the clients IP address:

    public static string Ip()
    {
        System.Web.HttpContext context = System.Web.HttpContext.Current;
    
        string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    
        if (!string.IsNullOrEmpty(ipAddress))
        {
            string[] addresses = ipAddress.Split(',');
            if (addresses.Length != 0)
            {
                return addresses[0];
            }
        }
    
        return context.Request.ServerVariables["REMOTE_ADDR"];
    
    now i want to get the clients port number and application name?(application name like mobile, web browser type or ...) . is there any way to do that?
    Tuesday, June 30, 2015 3:54 AM

Answers

  • Hi Bouki,

    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);
             }
    }

    For more information , refer the following links http://blogs.msdn.com/b/phenning/archive/2007/08/08/remoteendpointmessageproperty-in-wcf-net-3-5.aspx

    Best Regards,

    Tracy Dj

    • Marked as answer by Bouki Saturday, July 4, 2015 6:46 AM
    Thursday, July 2, 2015 2:08 AM
    Moderator

All replies

  • The WCF client's port number is known, which is the port number the WCF service is listening on. The client is the one that must make the initial contact with the WCF service on the specified port the WCF service is accepting inbound communications on. It is the client side WCF that will determine what type of client wants to make the contact with the WCF service using a Web browser or whatever and not the WCF service.

    Tuesday, June 30, 2015 11:22 AM
  • The WCF client's port number is known, which is the port number the WCF service is listening on. The client is the one that must make the initial contact with the WCF service on the specified port the WCF service is accepting inbound communications on. It is the client side WCF that will determine what type of client wants to make the contact with the WCF service using a Web browser or whatever and not the WCF service.

    thank you dear DA924.

    but the wcf client establish the connection from a random port on his side to my listening port(in my case is 80). i want to find out which port is that.

    so it means i cant found out the consumer  application name and port number?

    Wednesday, July 1, 2015 4:16 AM
  • Hi Bouki,

    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);
             }
    }

    For more information , refer the following links http://blogs.msdn.com/b/phenning/archive/2007/08/08/remoteendpointmessageproperty-in-wcf-net-3-5.aspx

    Best Regards,

    Tracy Dj

    • Marked as answer by Bouki Saturday, July 4, 2015 6:46 AM
    Thursday, July 2, 2015 2:08 AM
    Moderator