views:

557

answers:

3

I'm working with some WebForms/MVC-agnostic tools, and I need to get an instance of HttpContext given a reference to an HttpContextBase object. I can't use HttpContext.Current because I need this to work asynchronously as well (HttpContext.Current returns null during an asynchronous request). I'm aware of HttpContextWrapper, but goes the wrong way.

+3  A: 

You can't.

The whole purpose of HttpContextBase is to abstract away the dependency on the concrete HttpContext class. While it may contain a concrete HttpContext (such as is the case with httpContextWrapper), other implementations may have absolutely nothing to do with HttpContext.

Your best option is to define a custom Abstract Factory that can get a HttContextBase for your, since you can always wrap a concrete HttpContext in a HttpContextWrapper.

Mark Seemann
A: 

Isn't HttpContextBase abstract class, meaning no one could create instance of it? My guess you just have to cast

Sergej Andrejev
A: 

You can,

var abstractContext = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current);

Marc Chouteau