Current Context in DelegatingChannel

Topics: Web Api
May 27, 2011 at 3:18 AM

My goal is to get the IP address from the requester inside a delegating channel.

I'm trying to do something basic like: 

string ipAddress = string.Empty;
if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Request != null) {
   ipAddress = System.Web.HttpContext.Current.Request.UserHostAddress;

But the System.Web.HttpContext.Current is null.  Is there a way to get the IP address from an HttpRequestMessage (inside a DelegatingChannel)?

May 27, 2011 at 4:53 PM

It seems the the above code actually does work, it just doesn't work on the first request.  System.Web.HttpContext.Current is null on the first request so I kept editing my code and trying different scenarios, but if I just execute another request the Current context does exist and everything seems to be working.

Jul 1, 2011 at 1:43 PM

This seems like a "dirty" way to get the client IP address. Isn't there any recommended way to get the UserHostAddress or IP from within a DelegatingChannel that works during the first call?