2

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:

castle PerRequestLifestyle not recognize

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

Community
  • 1
  • 1
Paul Hiles
  • 9,558
  • 7
  • 51
  • 76

3 Answers3

4

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 Kozmic
  • 27,267
  • 12
  • 73
  • 115
4

Try using an hybrid webrequest/transient lifestyle.

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
  • althought I still say - refactor should be your first option – Krzysztof Kozmic Jun 25 '10 at 21:45
  • @mauricio-scheffer I'm a bit confused, let's say I register IDBcontext with hybrid webrequest in my applicaiton_start, now when the app starts there is no httpcontext, so it will be registered as tansient. So how will IDBcontext ever be registered as perrequest lifestyle? – Saber Nov 09 '15 at 08:55
  • @Arvand if there is a HttpContext the container will try to resolve it from the context. Otherwise it will create a fresh instance. – Mauricio Scheffer Nov 09 '15 at 14:10
0

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
  • 4,288
  • 23
  • 41