1

I have simple web service in console application:

static void Main(string[] args)
        {

            WSHttpBinding binding = new WSHttpBinding();
            binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            binding.Security.Mode = SecurityMode.None;

            Uri baseAddress = new Uri("http://localhost:8001/LogService");

            using (ServiceHost serviceHost =
                new ServiceHost(typeof(MyLogService), baseAddress))
            {
                // Check to see if it already has a ServiceMetadataBehavior
                ServiceMetadataBehavior smb =
                    serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();

                if (smb == null)
                    smb = new ServiceMetadataBehavior();

                smb.HttpGetEnabled = true;
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy12;
                serviceHost.Description.Behaviors.Add(smb);

                // Add MEX endpoint
                serviceHost.AddServiceEndpoint(
                    ServiceMetadataBehavior.MexContractName,
                    MetadataExchangeBindings.CreateMexHttpBinding(),
                    "mex"
                    );

                serviceHost.AddServiceEndpoint(typeof(ILogService), binding, baseAddress);


                serviceHost.Open();

                Console.WriteLine("The service is running. Press any key to stop.");
                Console.ReadKey();
            }


        }

Service interface and class:

 [ServiceContract]
    interface ILogService
    {
        [OperationContract]
        int LogIt(ref int id, string data, ref LogLevel level);
    }

    class MyLogService : ILogService
    {
        DBLogger dbl = new DBLogger();

        public int LogIt(ref int id, string data, ref LogLevel level)
        {
            try
            {

                String IP = HttpContext.Current.Request.UserHostAddress;
                Console.WriteLine("conneted from host " + IP);

            }
            catch (Exception d)
            {
                Console.WriteLine(d.Message);

            }



            return 0;
        }
    }

Trying to get clients IP in line:

String IP = HttpContext.Current.Request.UserHostAddress;

But Current is NULL and I have exception. How to get client IP in my project case?

vico
  • 17,051
  • 45
  • 159
  • 315
  • Be aware that once you have this working, it's something that's only generally going to be useful on local networks. You can't (generally) get the client's IP address when you're working across the internet, nor should you try to. Not sure whether this warning applies to your particular usage, but though it worth adding anyway. – Damien_The_Unbeliever Dec 03 '14 at 14:46
  • I'm planning to run this service on internet. You mean I will not get client IP addres in this case? What might be solution? – vico Dec 03 '14 at 14:55
  • There are lots of devices on the internet through which IP traffic can flow (routers, gateways, NAT devices, proxies, etc) and the only IP address that your code can pick up (directly) is the IP address of the closest of those devices to your machine. Some (such as proxies) *may* add extra headers to indicate the original IP address (that *they* could obtain) but then you have to trust that device. The "solution" is to not try to obtain the client's IP address but instead to solve the *problem* where you thought that obtaining that IP address was part of the solution. – Damien_The_Unbeliever Dec 03 '14 at 15:02
  • The question is closely related to this question http://stackoverflow.com/questions/93162/obtaining-client-ip-address-in-wcf-3-0 – Codor Dec 04 '14 at 07:27

2 Answers2

0

You can't use HttpContext because you are not running the service in ASP.NET mode.

Try this

MessageProperties props = OperationContext.Current.IncomingMessageProperties;

RemoteEndpointMessageProperty prop = (RemoteEndpointMessageProperty)props[RemoteEndpointMessageProperty.Name];

string addr = prop.Address;
int iPort   = prop.Port;
matthijsb
  • 909
  • 5
  • 12
0

Your approach is for web hosted version, for self hosted try

object property;
Request.Properties.TryGetValue(typeof(RemoteEndpointMessageProperty).FullName, out property);
RemoteEndpointMessageProperty remoteProperty = property as RemoteEndpointMessageProperty;

refer here for documentation

faby
  • 7,394
  • 3
  • 27
  • 44