How to use Windsor IoC in ASP.net Core 2
Asked Answered
D

4

27

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.

Die answered 6/12, 2017 at 10:56 Comment(0)
D
24

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;
}
Die answered 6/12, 2017 at 14:7 Comment(0)
C
13

For .net core, which centers DI around the IServiceProvider, you would need to create you own wrapper

Reference : Introduction to Dependency Injection in ASP.NET Core: Replacing the default services container

public class ServiceResolver : IServiceProvider {
    private static WindsorContainer container;

    public ServiceResolver(IServiceCollection services) {
        container = new WindsorContainer();
        // a method to register components in container
        RegisterComponents(container, services);
    }

    public object GetService(Type serviceType) {
        return container.Resolve(serviceType);
    }

    //...
}

and then configure the container in ConfigureServices and return an IServiceProvider:

When using a third-party DI container, you must change ConfigureServices so that it returns IServiceProvider instead of void.

public IServiceProvider ConfigureServices(IServiceCollection services) {
    services.AddMvc();
    // Add other framework services

    // Add custom provider
    var container = new ServiceResolver(services);
    return container;
}

At runtime, your container will be used to resolve types and inject dependencies.

Countersign answered 6/12, 2017 at 13:29 Comment(2)
I have done as your guild but came following error: Castle.MicroKernel.ComponentNotFoundException: 'No component for supporting the service Microsoft.AspNetCore.Hosting.Internal.WebHostOptions was found'Brierroot
And I wanted to know where the controller injections comes to play ?Brierroot
I
12

This answer is for .NET Core 2 only

There is an official Castle Windsor support for ASP.NET Core which has been released as version 5 (get it from nuget Castle.Windsor, Castle.Facilities.AspNetCore). The documentation how to use it is here.

More info in the related issues here and here

Indore answered 25/5, 2018 at 14:43 Comment(0)
C
0

I wanted to add that answer Yahya Hussein offers works for me, however I had to adjust my scope to LifestyleCustome<MsScopedLifestyleManager>() on each dependency I registered.

This may be second nature to some but it took me a minute to figure out.

Carincarina answered 4/9, 2019 at 17:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.