I have a problem with figuring out what the best way is to register different lifestyles for different applications using the same installers.
I have a web project that use Castle Windsor IoC. Some things are scoped to use PerWebRequest lifestyle. The classes are registered with installers in the respective projects.
All good so far, but I'm thinking about moving some stuff to a windowsService to do some scheduled stuff. This service will resolve the same domain as the web project but get trouble when the classes are registered with PerWebRequest lifestyle. I'd like to change this to Scoped for the service but keep it as it is in the web app. The configuration is done in the installer kept in the respective assemblies.
The installers doesn't know which app is trying to register, but the registrations needs to be with different lifestyle depending on if the installer is registered from the web app or the windows service.
Right now I have restorted to an ugly Hack until I get a better solution.
private static bool scopeLifetime = false;
public static void SetScopedLifetime()
{
scopeLifetime = true;
}
public void Install(IWindsorContainer container, IConfigurationStore store)
{
var registrations = Classes.FromThisAssembly()
.WithService.DefaultInterfaces();
if (scopeLifetime)
{
container.Register(registrations.LifestyleScoped());
}
else
{
container.Register(registrations.LifestylePerWebRequest());
}
}
What is the correct way to tell windsor what I would like to do, without the installer needing to know the context?