WCF 3.5 RemoteEndpointMessageProperty in load balancing situation

Discussion in 'ASP .Net Web Services' started by John Dow, Jan 2, 2009.

  1. John Dow

    John Dow Guest

    I have a WCF service in .Net 3.5 and I am trying to use
    RemoteEndpointMessageProperty to get the IP address of the client who
    consume the service, for trouble shooting purposes:

    OperationContext context = OperationContext.Current;
    MessageProperties properties = context.IncomingMessageProperties;
    RemoteEndpointMessageProperty endpoint =
    properties[RemoteEndpointMessageProperty.Name] as
    RemoteEndpointMessageProperty;
    if (endpoint != null)
    {
    IP = string.Concat(endpoint.Address, ":", endpoint.Port);
    }

    However, since the WCF service is hosted in IIS behind a load balancer, the
    IP address I got is always the IP of the load balancer.
    Is there any way to get around this so that I can get the true IP of the
    client?

    Thanks
     
    John Dow, Jan 2, 2009
    #1
    1. Advertisements

  2. John Dow

    Guest

    On Friday, January 2, 2009 11:31:50 AM UTC-5, John Dow wrote:
    > I have a WCF service in .Net 3.5 and I am trying to use
    > RemoteEndpointMessageProperty to get the IP address of the client who
    > consume the service, for trouble shooting purposes:
    >
    > OperationContext context = OperationContext.Current;
    > MessageProperties properties = context.IncomingMessageProperties;
    > RemoteEndpointMessageProperty endpoint =
    > properties[RemoteEndpointMessageProperty.Name] as
    > RemoteEndpointMessageProperty;
    > if (endpoint != null)
    > {
    > IP = string.Concat(endpoint.Address, ":", endpoint.Port);
    > }
    >
    > However, since the WCF service is hosted in IIS behind a load balancer, the
    > IP address I got is always the IP of the load balancer.
    > Is there any way to get around this so that I can get the true IP of the
    > client?
    >
    > Thanks



    string retIp = "";
    try
    {

    OperationContext context = OperationContext.Current;

    MessageProperties prop = context.IncomingMessageProperties;

    HttpRequestMessageProperty endpointLoadBalancer =
    prop[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;

    if(endpointLoadBalancer.Headers["X-Forwarded-For"]!=null)
    {
    retIp = endpointLoadBalancer.Headers["X-Forwarded-For"];
    }

    if(string.IsNullOrEmpty(retIp))
    {
    RemoteEndpointMessageProperty endpoint =
    prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
    retIp = endpoint.Address;
    }
    }
    catch (Exception ex) {
    log.Error("Error in GetClientIP: " + ex.ToString());
    }
    return retIp;
     
    , Oct 15, 2012
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. SamIAm
    Replies:
    0
    Views:
    644
    SamIAm
    Nov 11, 2003
  2. Christopher D. Wiederspan

    Load Balancing a .NET WebFarm

    Christopher D. Wiederspan, Dec 4, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    4,706
    Stefan Schachner[MSFT]
    Dec 5, 2003
  3. Jon
    Replies:
    4
    Views:
    691
    Jon Paugh
    Feb 20, 2004
  4. Jeffrey Kelso

    Load Balancing and PostBack - form post lost

    Jeffrey Kelso, Feb 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    759
    Jeffrey Kelso
    Feb 20, 2004
  5. Robert

    Session state with load balancing

    Robert, Feb 26, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    5,836
    shahzad
    Jan 30, 2009
  6. Jack Wright
    Replies:
    0
    Views:
    504
    Jack Wright
    May 5, 2004
  7. Simon
    Replies:
    0
    Views:
    1,416
    Simon
    Oct 13, 2009
  8. Cindy Lee
    Replies:
    1
    Views:
    3,733
    Mr. Arnold
    Mar 19, 2010
Loading...