views:

101

answers:

3

I am injecting HttpContextBase into a caching class. HttpContextBase is registered as PerWebRequest. I interact with the caching class on each web request and this works fine, but I also need to initialise the cache at application start.

I understand that PerWebRequest does not work in Application_Start though:

http://stackoverflow.com/questions/2670717/castle-perrequestlifestyle-not-recognize

What is the best way to resolve this in my situation?

+2  A: 

Not use stuff that depends on per-web-request stuff outside of web request.

If you need to depend on a class you registered as PWR I'd suggest getting another component for that service with different lifestyle and using it in Application_Start and using IHandlerSelector to return PWR one when you're within a web request, and the other one otherwise

Krzysztof Koźmic
it is not unreasonable to want to pre-populate a cache on application start, so what options do I have if I want to use Windsor to resolve the caching class which depends on HttpContextBase?
zaph0d
How can you use HttpContextBase outside of web request?
Krzysztof Koźmic
You still have an HttpContext in the Application_Start event
zaph0d
Updated the answer with _workaround_. I'd still try to restructure your code not to rely on something you chose to bind to web requests outside of web requests.
Krzysztof Koźmic
+1 Krzysztof's workaround is correct. Use a transient for Application_Start()
Mauricio Scheffer
A: 

To warmup caches when the container starts I usually use the Startable Facility. Here is an example on how to use it: http://blog.bittercoder.com/PermaLink,guid,a621ddda-acb5-4afd-84ff-faafb96a2fa1.aspx

John Simons
it will still eagerly instantiate the service
Krzysztof Koźmic
+2  A: 

Try using an hybrid webrequest/transient lifestyle.

Mauricio Scheffer
althought I still say - refactor should be your first option
Krzysztof Koźmic

related questions