HttpSelfHostServer and HttpContext.Current

Solution 1:

You won't be able to use HttpContext in a self-hosted environment. HttpContext is set by the ASP.Net pipeline, which you won't have if you don't run under IIS/ASP.Net.

The HttpContext is only available in the Web-Hosting mode, in which the HttpControllerHandler creates the request.

FYI- I invite you to read the following great articles from Pedro Felix to better understand the different hosting models:

  • HTTP processing architecture overview
  • Web-hosting
  • Self-hosting

Solution 2:

To get around this problem (I find I am using a lot of components these days that need to work equally well in Web API and MVC), you can try this old shim I wrote to give you back an HttpContext-like interface that works in both flavours. It's on NuGet also, here's the source: Link on github (or Link on Nuget)