Search

ASP.NET Abstractions: add HttpContextBase.Current by AtsushiEno

Closed
as By Design Help for as By Design

2
2
Sign in
to vote
Type: Suggestion
ID: 376426
Opened: 10/19/2008 3:25:12 AM
Access Restriction: Public
0
Workaround(s)
There should be some replacement for HttpContext.Current in ASP.NET abstractions layer.
Details (expand)
Product Language
English

Version

.NET Framework 3.5 Service Pack 1
Operating System
Windows XP Professional
Operating System Language
Japanese
Problem Statement
Currently any modern ASP.NET apps that are based on Abstractions layer still have to resort to HttpContext.Current to create HttpContextBase instance.

A good example is DynamicData which is the only present application of Abstractions. It internally depends on HttpContext.Current and hence it cannot be tested outside ASP.NET runtime.

(I'm unsure if ASP.NET MVC makes use of it; it uses different abstractions/routing assembly than .NET 3.5 SP1).
Proposed Solution
Providing HttpContextBase.Current which works almost similar to HttpContext.Current would solve couple of difficult situations to solve this kind of problem.

Or applications such as DynamicData should (have) expose(d) public HttpContextBase setter that is internally used by the framework.
TAP Code (if applicable)
 
      You can indicate your satisfaction with how Microsoft handled this issue by completing this quick 3 question survey.

 

Sign in to post a comment.
Posted by Adrian Anttila on 5/1/2009 at 2:07 PM
As an alternative, you can use the following line of code to create an HttpContextBase class:

HttpContextBase context = new HttpContextWrapper(HttpContext.Current);

Hope this helps,
Adrian
Posted by Microsoft on 2/19/2009 at 3:49 PM
Hi there! Thanks for the suggestion. This is by design.

Suppose HttpContextBase.Current existed.

To logically mimic HttpContext.Current, the property would have to be static. If the property is static you couldn’t mock it anyway! Even if it’s settable that is unacceptable to unit tests since you never want to use statics (they are global and affect global state). Unit tests should not affect global state in such a way as that could affect other unit tests. Unit tests need to be isolated and independent.

If something in Dynamic Data isn’t testable, it has nothing to do with System.Web.Abstractions, but probably something specific to Dynamic Data itself. I didn't actually see a specific problem in this bug report, but it might be worth reporting to the Dynamic Data team if you have something specific. For example, in MVC we always pass our contexts along and never use HttpContext.Current. If Dynamic Data did the same then that might solve the problem in their case.
Sign in to post a workaround.