【发布时间】: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