views:

44

answers:

2

I am building a set of class libraries that produce office open xml based reports and I am using a static Windsor IoC container.

My problem is that one possible entry point to the reporting system is via a web front end which means that the reporting systems static IoC Container is being shared amongst multiple web requests which causes exceptions as for each new request the reporting system is trying re-register components in Windsor that were already registered by an earlier request.

I dont want to move the registration into the web app global.asax as my reporting system will no longer be stand-alone.

How can I have a Windsor IoC container that can be shared amongst my reporting classes but not shared across multiple web requests?

+1  A: 

Did you try the Lifestyle option for you components. There is a PerWebRequestLifestyleManager and a PerWebRequestLifestyleModule option. See here the documentation for lifestyles. And see here an example that uses the lifestyle options.

Ikaso
mmm doesn't that suggest that the reporting application 'knows' that its being called from a web application? Or maybe that doesnt matter as if it wasn't it would still be happy.
Si Keep
It is something you put in the Castle configuration. Your reporting application can come with whatever configuration you want. When the reporting runs inside the web application the configuration will change to use the perwebrequest options. Please note that you can configure the container programmatically too. So if the reporting module configures the container programmatically you will have some refactoring to do so that the reporting module will be indifferent to the container configuration.
Ikaso
+2  A: 

Don't recreate the container over and over again. Do create and setup it once per your application in global start point. You can still make your components self contained by encapsulating all registration in installers. Then in global.asax you'll only install the installers which is fairly easy to do and in upcoming Windsor 2.5 it's literally 2 lines of code.

Krzysztof Koźmic
@Krzysztof: +1 for this option. I was not familiar with it.
Ikaso
@Krzysztof: Thanks, the installer idea is working nicely however I have still had to make the container passed to the installer class accessable using static data.
Si Keep
@Si - you won't have to in Windsor 2.5 - you'll be able to just write: `container.Install(FromAssembly.Named("YourAssembly"))`
Krzysztof Koźmic