【问题标题】:Joining two different ContainerBuilder加入两个不同的 ContainerBuilder
【发布时间】:2020-05-14 22:01:14
【问题描述】:

我正在从 ASP.Net Framework 迁移到 ASP.Net Core (3.1) 应用程序。我是新来的。 Depency Inyection 在我的旧项目中由 Autofac 管理。所以我需要复制行为。

我的Startup 班级需要ConfigureContainer。类似的东西:

public class Startup
{

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

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

另一方面,我有一些来自 Bootstrapper 类的依赖项。 Bootstrapper 是来自另一个项目的类。 此类由其他项目调用,因此我无法将其删除。它是一种带有一些服务的 BaseContainer,它们被其他项目使用。

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();
   }
}

我想将两个 ContainerBuilder 合二为一。我怎样才能做到这一点?

我看到了Update() 的方法,但似乎是obsolete

有什么想法吗?

【问题讨论】:

  • BootStraper 是什么?你如何使用它 ?为什么你需要保留这个类?
  • @CyrilDurand 我编辑了这篇文章来解释这一点。
  • 你能改变BootStrapper类吗?一种解决方案是添加第二个以 ContainerBuilder 作为参数的构造函数

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


【解决方案1】:

Autofac 的官方网站:asp-net-core-3-0-and-generic-hosting 描述了如何与 NetCore 和 Autofac 集成。我还按照本教程完成了与您类似的要求。

只需对BootStraper类稍作改动即可成为Autofac.Module的派生类。因此,您只需像这样在 Startup.cs 中添加很少且清晰的代码即可获得好处

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();
   //}
}

在创建主机生成器时不要忘记添加这一行。

hostBuilder.UseServiceProviderFactory(new AutofacServiceProviderFactory())

这个 autofac 扩展是来自Autofac.Extensions.DependencyInjection 的 NuGet 包。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 2012-05-29
    • 2015-04-03
    • 1970-01-01
    • 2017-11-02
    相关资源
    最近更新 更多