首页 > 解决方案 > Joining two different ContainerBuilder

问题描述

I'm migrating from ASP.Net Framework to ASP.Net Core (3.1) application. I'm new on this. Depency Inyection is managed by Autofac in my old project. So I need to replicate the behavieur.

I need a ConfigureContainer in my Startup class. Something like that:

public class Startup
{

    public void ConfigureContainer(ContainerBuilder builder)
    {
        // Register your own things directly with Autofac, like:

        builder.RegisterType<MyFoo>().As<IMyFoo>();
    }
}

By the other hand, i have some dependencies from Bootstrapper class. Bootstrapper is a class from another project. This class is called by others project so i can't remove it. It's a kind of BaseContainer with some services that they are used by other projects.

public sealed class  BootStraper
{
    public ContainerBuilder Builder { get; set; }

    public BootStraper()
    {

        Builder = new ContainerBuilder();            
        Builder.RegisterType(typeof(Foo1));
        Builder.RegisterType(typeof(Foo2));
        Builder.RegisterType(typeof(Foo3));
        Builder.RegisterType(typeof(Foo4));
        Builder.RegisterType(typeof(Foo5));        
   }

   public void RegisterTypeSingleton(Type type)
   {
        Builder.RegisterType(type).SingleInstance();
   }
}

I would like to join both ContainerBuilder in one. How can i do this?

I saw Update() method to do this but it seems it's obsolete

Any Idea?

标签: asp.net-coreautofacasp.net-core-3.1

解决方案


The official site from Autofac: asp-net-core-3-0-and-generic-hosting describes how to integrate with NetCore and Autofac. I also followed this tutorial to accomplish similar requirement like yours.

Just need to change BootStraper class a little bit to become derived class of Autofac.Module. Thus, you can get benefits by just adding very few and clear codes in Startup.cs like this

Startup.cs

public void ConfigureContainer(Autofac.ContainerBuilder builder)
{
    // Register your custom BootStraper types here
    // If the ordering matters, just swtich this to the first or last line
    builder.RegisterModule<BootStraper>();   

    // Register your own things directly with Autofac, like:
    builder.RegisterType<MyFoo>().As<IMyFoo>();
}

BootStraper.cs

public sealed class  BootStraper : Autofac.Module
{
    // public ContainerBuilder Builder { get; set; }
    protected override void Load(Autofac.Containerbuilder builder)
    {
        base.Load(builder);

        builder.RegisterType(typeof(Foo1));
        builder.RegisterType(typeof(Foo2));
        builder.RegisterType(typeof(Foo3));
        builder.RegisterType(typeof(Foo4));
        builder.RegisterType(typeof(Foo5)); 

        // RegisterTypeSingleton() could be replaced by either
        // 1.registering directly here or 
        // 2.putting in ConfigureContainer() section in Startup
        builder.RegisterType(typeof(Foo6)).SingleInstance();
    }

   //public void RegisterTypeSingleton(Type type)
   //{
   //     Builder.RegisterType(type).SingleInstance();
   //}
}

Don't forget to add this line while creating host builder.

hostBuilder.UseServiceProviderFactory(new AutofacServiceProviderFactory())

This autofac extension is the NuGet package from Autofac.Extensions.DependencyInjection.


推荐阅读