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 4239962
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:06:12+00:00 2026-05-21T03:06:12+00:00

HttpContextWrapper and HttpContextBase, as explained here , were introduced to make HttpContext more mockable/testable.

  • 0

HttpContextWrapper and HttpContextBase, as explained here, were introduced to make HttpContext more mockable/testable.

I’m trying to use it with S#arp Architecture, and hitting some problems.

My MVC Controllers are set up to accept an HttpContextBase argument in the constructor, and during Application_Start, HttpContextBase is registered with Castle.Windor as follows:

container.Register(Component.For<HttpContextBase>().UsingFactoryMethod(
    () => new HttpContextWrapper(HttpContext.Current)));

This seemed to work OK for a bit, but then I realised Castle is only running that Factory method once, so all requests get the original HttpContextWrapper. Really it needs to be re-created for every request. The Castle.Windsor command for that would be:

container.Register(Component.For<HttpContextBase().
    LifeStyle.PerWebRequest.UsingFactoryMethod(
    () => new HttpContextWrapper(HttpContext.Current)));

… but it turns out that Castle.Windsor doesn’t allow LifeStyle.PerWebRequest to be used within Application_Start (as explained here)

What should I be doing? Is there an easy way round this or should I give up on HttpContextWrapper and inject my own factory to make new ones as needed?

  • 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-21T03:06:12+00:00Added an answer on May 21, 2026 at 3:06 am

    My MVC Controllers are set up to accept an HttpContextBase argument in the constructor

    You gotta be doing something extremely wrong here, so stop before it’s too late and damage has been caused (material, moral and human casualties :-)). You already have the HttpContext inside the controller.

    Don’t register any HttpContexts in your DI framework. The HttpContext handling is the job of ASP.NET.

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

Sidebar

Related Questions

How to write this StructureMap line in Ninject ForRequestedType<HttpContextBase>() .TheDefault.Is.ConstructedBy(x => new HttpContextWrapper(HttpContext.Current)); ?
I am trying to use session state in MVC and I have been stuck
I've been going through the process of cleaning up our controller code to make
I am currently trying to add 301 redirect to my routes in MVC to
If we use this standard route: routes.MapRoute( Default, // Route name {controller}/{action}/{id}, // URL
This question relates to my other post . Ok so after a bit more
To support legacy URLs in my application, I use a regex to convert URLs
I've been trying to solve this problem for a while now, and I'm at
Both have Request and Response properties, but I can't write a method that takes
New to Castle/Windsor, please bear with me. I am currently using the framework System.Web.Mvc.Extensibility

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.