How can I get started with ASP.NET (5) Core and Castle Windsor for Dependency Injection?

后端 未结 3 1313
滥情空心
滥情空心 2021-02-05 12:34

Background:

I\'ve used Castle Windsor with Installers and Facilities according to the Castle Windsor tutorial with earlier versions of MVC (pre-6) and WebAPI.

3条回答
  •  离开以前
    2021-02-05 13:00

    For now I don't think you can use Castle Windsor Container as the DI container because Windsor doesn't support the new DNVM. But AutoFac does and they follow the same rule.

    In the Startup.cs there is a ConfigureServices method whose return type is void. You can change the return type to ISerivceProvider and return a concrete IServiceProvider, the system will use the new IServiceProvider as the default DI container. Below is the AutoFac example.

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
           services.Configure(Configuration.GetSubKey("AppSettings"));
           services.AddMvc();
    
           var builder = new ContainerBuilder();
           AutofacRegistration.Populate(builder, services);
           var container = builder.Build();
           return container.Resolve();
    }
    

    The other DI adapters also implemented the similar interfaces. You can try yourself, but note AutoFac is in beta5 now so you need to make some adjustment to make your application run.

    Hope this helps

提交回复
热议问题