I created a WebApi project and configured Castle Windsor as DI container.
When I deploy my WebApi on my WebServer, everything is working fine until I restart the web server (I'm using IIS
) by clicking on the restart button or by executing the iisreset
command. After that, I always have the following error:
No component for supporting the service XXX.ICategoryDomainService was found
When running my project in debug mode inside Visual Studio
, this problem seems to happen randomly. But each time I do a full Clean/Rebuild
, it's then working at least once. Stopping/Starting the project many times (always inside Visual Studio
) brings the problem back. When the exception raised, I inspected the state of the Container and everything seems to be ok
Update:
Ok, I finally managed to find where the problem was (but I do not have a proper solution for now).
I'm doing a registration in a Framework dll that scans all types of my current project and tries then to registered automatically some of these types...
In a Framework Library:
IEnumerable<IInstaller> installers = this.Container.ResolveAll<IInstaller>();
foreach (IInstaller installer in installers)
{
installer.Install(this.Container);
}
This is how IInstaller is registered:
this.Container.Register(Classes
.From(ApplicationDomain.Current.ConcreteClasses)
.BasedOn<IInstaller>()
.WithServiceAllInterfaces()
.LifestylePerWebRequest());
A small extensions method (always in Framework)
public static void RegisterAllServices(this IWindsorContainer container,
IEnumerable<Type> types)
{
container.Register(Classes
.From(types)
.BasedOn<IService>()
.WithServiceAllInterfaces()
.LifestyleTransient());
}
An installer in my project
public class BusinessInstaller : IInstaller
{
public void Install(IWindsorContainer container)
{
Type[] types = typeof(BusinessInstaller).Assembly.GetTypes();
container.RegisterAllServices(types);
}
}
It's very weired, but this is what I can observe: The extensions method RegisterAllServices
is only called when I do a full rebuild. After that, in debug mode, I can see that the debugger just steps over an never get into this method BUT the types are registered anyway (but probably in a wrong way, because Windsor is not able to resolve them!).
The temporary fix is to register all my classes in the BusinessInstaller. Then it works... but it's not optimal, it would be better if everything could be automatically registered...