Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3483682
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:42:08+00:00 2026-05-18T10:42:08+00:00

I have the following assemblies in my ASP.NET app: Website – this is an

  • 0

I have the following assemblies in my ASP.NET app:

Website – this is an ASP.NET website
ClassLib – this is just a class lib that contains all the business logic

Class Lib needs to interact with the HttpContext Session and Request objects. This is a code upgrade from an old ASP app, where I’ve hoovered all the VBScript that contained the logic and put it into VB.NET. We simply didn’t have time to rewrite.

Instead of ClassLib interacting directly with HttpContext, which I thought was BAD and also prevented us from unit testing, I introduced the following abstraction layer:

Public Class Request
 Private Shared _requestWrapper as IRequestWrapper
     Public Shared ReadOnly Property RequestWrapper()
        Get
            If _requestWrapper Is Nothing Then
                Throw New Exception("_requestWrapper is null.  Make sure InitRequest() is called with valid parameters")
            End If
            Return _requestWrapper
        End Get
    End Property


    Public Shared Sub InitRequest(ByRef requestWrapper As IRequestWrapper)
        _requestWrapper = requestWrapper
    End Sub

     Public Shared Function GetVal(ByVal key As String) As Object
        Return RequestWrapper.GetVal(key)
    End Function

etc.

This means in the unit tests I can supply my own MockRequest object into this Request class, which is just a simple NameValue collection. The code in the ClassLib and the Website then simply use the Request class and are none the wiser that it isn’t coming from the HttpContext, but rather this mock class.

When it comes to the real deal, I simply have the following (C#) class:

public class RealRequest : IRequestWrapper
    {
        public void Initialize(HttpContext context)
        {
        }

        #region Implementation of IRequestWrapper

        public object GetVal(string index)
        {
            return HttpContext.Current.Request[index];
        }

etc.

This is initialised in Session_Start of global.asax in the Website, as follows:

 protected void Session_Start(object sender, EventArgs e)
    {


        IRequestWrapper requestWrapper = new RealRequest();
        WebSession.Request.InitRequest(ref requestWrapper);
    }

I think this is similar to the Static Gateway pattern.

Now, I am aware of singletons and static vars in a multi threaded environment such as ASP.NET, but this is slightly different. When it gets down to the RequestWrapper.GetVal(), its actually going to the HttpContext for that running thread – and pulling the value from that.

Certainly, any concurrent tests that we do with multiple users hitting the same server have never shown up any strange behaviour.

I’m just looking for re-assurance that this is a sound design, and if not why not?

Thanks
Duncan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:42:09+00:00Added an answer on May 18, 2026 at 10:42 am

    This is fine. We have a very similar case in our applications that either uses HttpContext if it exists or fake implementations otherwise.

    The one thing to watch out for is that there is a very specific instance where HttpContext.Current will return a value but HttpContext.Current.Request will throw an exception when triggered by the Application_Start event. In framework code, you really don’t know (or want to know) what triggered the call though.

    Workaround for HttpContext.HideRequestResponse being internal? Detect if HttpContext.Request is really available?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following setup, I have three assemblies. Web (ASP.NET MVC 3.0 RC1) Models
I have an existing ASP.NET application that implements Forms Authentication site-wide. The application is
I have the following C# project targetting .NET 4.0 that takes a source code
Let's say, for the sake of argument, that I have 30 asp.net web applications
We have the following shared component: public class OurServiceBase : System.ServiceProcess.ServiceBase This class has
I have existing code for an ASP .NET application that uses reflection to load
I have the following setup: Main assembly that contains a WcfService and IWcfService contract.
I have following use-case: there are several assemblies decorated with ProtoContract classes and I
I have following fiddle: http://jsfiddle.net/BFSH4/ As you see there are two issues: The h1
I have following script that executes all the .reg files in the current directory.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.