asp.net-core - 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?
解决方案
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
.
推荐阅读
- r - 创建一个变量并按值填充,其名称与R中数据集的名称相同
- java - AZURE:使用 kubernetes CronJob 安排作业时,事件不会流入 Azure 应用程序洞察力
- python - 我如何将 1-12 之间的数字转换为月份并给出季节名称(Pyhon)
- doxygen - 为什么 Doxygen 不接收代码注释
- azure - 查找大量页面点击/重新加载的来源
- bash - 复制目录
- r - R中的值区间窗口
- javascript - 在 reactJs 中使用 useState 更改嵌套对象中的值
- php - php -S 随机不工作:localhost 拒绝连接
- kubernetes - kubectl top no 不反映实际 RAM