Castle.Windsor and HttpContextWrapper

前端 未结 3 611
一整个雨季
一整个雨季 2021-01-16 02:16

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

I\'m trying to use it with S#arp Architecture,

相关标签:
3条回答
  • 2021-01-16 02:32

    As Darin noted, it makes no sense to inject an HttpContext into an MVC controller. However, if you need it for other kind of services and also need it in Application_Start(), use an hybrid perwebrequest-transient lifestyle. Or, since it's trivial to build, just make it transient.

    0 讨论(0)
  • 2021-01-16 02:34

    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 讨论(0)
  • 2021-01-16 02:48

    As others have stated - you are doing it wrong. My big question is:

    What are you doing that requires you to inject HttpContextBase in your controller? It might be more helpful to people wanting to help you if you would provide us more context about what you are really trying to do. Lets take Castle out of it and get down to what your controller is doing.

    BTW, your controller already has a reference to HttpContext. If you are doing this for testability, there is nothing you need to do at the controller level. You would just need to mock the HttpContextBase object as needed in your controller tests.

    0 讨论(0)
提交回复
热议问题