Is it always safe to create a new HttpContextWrapper?

I am trying to make an existing ASP.NET web form application more uniform, validated with some ASP.NET MVC objects, in particular HttpContextWrapper. I saw examples of its use, and they always create a new object. I parsed the source with Reflector and saw that everything it does keeps the past HttpContext. But I was wondering if it is safe to always create a new instance of HttpContextWrapper or follow a singleton pattern? Below is the class that I use in my application.

public static class AppHttpContext { public static HttpContextBase Current { get { return Getter(); } } public static void SetContext(Func<HttpContextBase> getter) { Getter = getter; } private static Func<HttpContextBase> Getter = () => new HttpContextWrapper(HttpContext.Current); } 

And I use it like HttpContext.Current

 AppHttpContext.Current.Session["blah"] = "something"; 
+4
source share
2 answers

The AppHttpContext class AppHttpContext excellent. It perfectly abstracts the HttpContext and allows you to test the device. It is safe to use HttpContextWrapper . As an improvement, you can make this class non-static, and instead of the static SetContext method SetContext you can insert a delegate into the constructor. Then all classes that need to work with context (usually this should be limited only to your web form pages) can have an instance of AppHttpContext .

+2
source
 private readonly Func<HttpContextBase> _httpContext = () => new HttpContextWrapper(HttpContext.Current); 
0
source

All Articles