How can I run bootstrap Castle Windsor in my ASP.NET MVC 6 project?

I am trying to use the Castle Windsor container in an ASP.NET MVC 6 project.

I followed the steps described in the Windsor Tutorial - ASP.NET MVC 3 Application to Part 4 , which provides instructions:

... we will use in the application (a single instance), install our installer and tell the MVC infrastructure to use our factory controller instead of our own default. All this happens in the global.asax file.

How in MVC 6 Startup.csreplaces global.asax, how do I need to start loading Castle Windsor into my application?

+4
source share
1 answer

I have not tried this yet, but it seems that the solution is given in the documentation for ASP.NET 5 Injection Dependency in the section " Replacing the default service container ".

Examples for AutoFac, but the same three steps can be applied to Castle Windsor, I believe:

Add the appropriate container container to the dependency property in project.json.

"dependencies" : {
  "Autofac": "4.0.0-beta8",
  "Autofac.Framework.DependencyInjection": "4.0.0-beta8"
},

Then, in Startup.cs, configure the container in ConfigureServicesand change this method to return IServiceProviderinstead of void:

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

    // Add Autofac
    var containerBuilder = new ContainerBuilder();
    containerBuilder.RegisterModule<DefaultModule>();
    containerBuilder.Populate(services);
    var container = containerBuilder.Build();
    return container.Resolve<IServiceProvider>();
}

Finally, configure Autofac as usual in the DefaultModule:

public class DefaultModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<CharacterRepository>().As<ICharacterRepository>();
    }
}
0
source

Source: https://habr.com/ru/post/1621131/


All Articles