19

I'm working on a self-hosted ASP.NET web api-application. Everything works fine, but now I'm struggling with HttpContext:

I need to save session-informations from the client. But HttpContext.Current is always null. So it's obvious that my HttpSelfHostServer don't work with the static HttpContext-Class.

The thing I don't understand is: why..? And I can't figure out a way to tell neither HtttpSelfHostServer nor HttpSelfHostConfiguration to work with HttpContext.

Here's what I'm doing:

  1. Creating a HttpSelfHostConfiguration

    • 1.1 Adding Service-Resolvers & Routes
    • 1.2 Adding custom UserNamePassword-Validator
  2. create new Instance of HttpSelfHostServer with the config

    • 2.1 server.OpenAsync().Wait()

Any help how I can tell my server to work with HttpContext.Current is greatly appreciated! Cheers!

Humayun Shabbir
  • 2,961
  • 4
  • 20
  • 33
Richard
  • 741
  • 2
  • 7
  • 17
  • Maybe need `AspNetCompatibility`: http://stackoverflow.com/questions/5904313/access-httpcontext-current-from-wcf-web-service – mellamokb Jul 05 '12 at 15:43
  • hi mellamokb. Thanks, but if I use the aspNetCompatibelity-Mode, I'm still going to neeed a IIS, which is the thing I want to avoid. – Richard Jul 06 '12 at 09:42
  • Also note the second answer in the link I posted refers to a lot of the same information being available in OperationContext. What exactly do you need out of the HttpContext? – mellamokb Jul 06 '12 at 13:18
  • OperationContext only exists in the WCF world, not in ASP.Net WebApi – Julien Jacobs Jul 15 '12 at 22:23

2 Answers2

30

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:

Donal Lafferty
  • 5,807
  • 7
  • 43
  • 60
Julien Jacobs
  • 2,561
  • 1
  • 24
  • 34
13

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)

Rajeev Bera
  • 2,021
  • 1
  • 16
  • 30
Daniel Crenna
  • 3,326
  • 1
  • 24
  • 34
  • 1
    Thanks for taking the time to post this here and to Github; I had a legacy component that expects to float around in a static variable on the current thread, and this shim beats it into submission even under SelfHost. Works like a charm! – Nicholas Piasecki Oct 06 '15 at 13:02
  • What you are offering seems very cool. However, I was unable to see whether it supports replacements of the `HttpSession` object - `HttpContext.Current.Session` seems unavailable. Does your library support session state? If not, could you recommend any way to get a session state replacement? Could I use `HttpContext.Current.Items` ? – Ivaylo Slavov Oct 17 '15 at 22:43
  • this somehow removed my system.web.http 5.2.3.0 reference and replaced it with 4.0.0.0, got loads of build errors from it – Daniël Tulp Sep 20 '17 at 06:44
  • @DaniëlTulp too late to be valuable but for the next person I'd just pull the code from https://github.com/danielcrenna/vault – Daniel Crenna Sep 18 '19 at 21:04
  • @DanielCrenna which of those projects in particular Daniel? – Daniël Tulp Sep 19 '19 at 12:54