-1

I'm attempting to create a method within my web service that will log IP of each request for each method in the service. I've attempted using the HttpContext, but no matter what I do it returns a null reference exception. I'd like to be able to grab the IP from the request and log it in a SQL database. Here's an example of one of the methods I'm attempting to log.

public GetPL GPL(string code)
    {
        var db = new TDCDataContext();

        var pq = db.PROC_SELECT_GPL(code).ToList();

        //a bunch of nonsense

        //logging
        var ip = HttpContext.Current.Request.UserHostAddress;
        var method = GetMethod();
        db.PROC_INSERT_Log(ip, code, method, true, null, null);

        return stuff;
    }

Am I headed in the wrong direction?

John
  • 23
  • 3
  • possible duplication see: http://stackoverflow.com/questions/93162/obtaining-client-ip-address-in-wcf-3-0 – Thomas Bates Jul 06 '12 at 15:33
  • Depending on what protocols you use, your WCF service will only be able to access HttpContext if a) you are hosting a RESTFUL service, b) you are ASP.NET compatible, and c) your requests come via a GET or POST. A normal WCF call will not use HttpContext – SASS_Shooter Jul 06 '12 at 18:43

1 Answers1

1
                try
                {
                    var context = OperationContext.Current;
                    if (context != null)
                    {
                        var prop = context.IncomingMessageProperties;

                        if (prop != null && 
                            !string.IsNullOrEmpty(RemoteEndpointMessageProperty.Name) && 
                            prop.ContainsKey(RemoteEndpointMessageProperty.Name))
                        {
                            endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
                        }
                    }                    
                } 
                catch (Exception e)
                {
                    //log
                }

//endpoint.Address - ip address

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52