How can I use Castle Windsor as an IOC instead of the default .net core IOC container?
I have built a service resolver that depends on WindsorContainer
to resolve services.
Something like:
public class ServiceResolver
{
private static WindsorContainer container;
public ServiceResolver()
{
container = new WindsorContainer();
// a method to register components in container
RegisterComponents(container);
}
public IList<T> ResolveAll<T>()
{
return container.ResolveAll<T>().ToList();
}
}
Can not figure out how to let my .net core 2 web API use this resolver as a replacement for IServiceCollection.
For others Reference In addition to the solution Nkosi provided.
There is a nuget package called Castle.Windsor.MsDependencyInjection that will provide you with the following method:
WindsorRegistrationHelper.CreateServiceProvider(WindsorContainer,IServiceCollection);
Which's returned type is IServiceProvider
and you will not need to create you own wrapper.
So the solution will be like:
public class ServiceResolver{
private static WindsorContainer container;
private static IServiceProvider serviceProvider;
public ServiceResolver(IServiceCollection services) {
container = new WindsorContainer();
//Register your components in container
//then
serviceProvider = WindsorRegistrationHelper.CreateServiceProvider(container, services);
}
public IServiceProvider GetServiceProvider() {
return serviceProvider;
}
}
and in Startup...
public IServiceProvider ConfigureServices(IServiceCollection services) {
services.AddMvc();
// Add other framework services
// Add custom provider
var container = new ServiceResolver(services).GetServiceProvider();
return container;
}