locked
OperationContext.Current.IncomingMessageProperties returning 127.0.0.1 RRS feed

  • Question

  • Hello,

    I have a WCF 3.5 service that is trying to retrieve the client IP Address using the method OperationContext.Current.IncomingMessageProperties(RemoteEndpointMessageProperty.Name).  When I am testing this service on the same machine as the client application, the method is returning an IP Address of 127.0.0.1 instead of my machine's IP.  Is there a way for this method to work when testing a service on a local machine?

    Friday, December 2, 2011 10:14 PM

Answers

  • It depends on how you client address the service. If the client uses "localhost", then it will use the loopback address (127.0.0.1), and that's what you'll see on the server. But if you use either the the machine IP or the machine name (which will be resolved, via DNS, to the machine IP), then you'll see the actual IP of the machine. See the difference on the code below.

        public class Post_1cee0dbb_bb8f_485f_b735_c52b44c9b878
        {
            [ServiceContract]
            public interface ITest
            {
                [OperationContract]
                string Echo(string text);
            }
            public class Service : ITest
            {
                public string Echo(string text)
                {
                    RemoteEndpointMessageProperty remoteEndpointProp;
                    remoteEndpointProp = (RemoteEndpointMessageProperty)OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name];
                    Console.WriteLine("Request from {0}:{1}", remoteEndpointProp.Address, remoteEndpointProp.Port);
                    return text;
                }
            }
            static Binding GetBinding()
            {
                var result = new WSHttpBinding(SecurityMode.None);
                //Change binding settings here
                return result;
            }
            public static void Test()
            {
                string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
                ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
                host.AddServiceEndpoint(typeof(ITest), GetBinding(), "");
                host.Open();
                Console.WriteLine("Host opened");
    
                ChannelFactory<ITest> factory = new ChannelFactory<ITest>(GetBinding(), new EndpointAddress(baseAddress));
                ITest proxy = factory.CreateChannel();
                Console.WriteLine(proxy.Echo("Hello using machine address"));
                ((IClientChannel)proxy).Close();
                factory.Close();
    
                string localhostAddress = baseAddress.Replace(Environment.MachineName, "localhost");
                factory = new ChannelFactory<ITest>(GetBinding(), new EndpointAddress(localhostAddress));
                proxy = factory.CreateChannel();
                Console.WriteLine(proxy.Echo("Hello using localhost address"));
                ((IClientChannel)proxy).Close();
                factory.Close();
    
                Console.Write("Press ENTER to close the host");
                Console.ReadLine();
                host.Close();
            }
        }
    
    


    Carlos Figueira
    Friday, December 2, 2011 11:16 PM