​​.net6 引入autofac​​


引入NuGet

.net6 引入autofac_ide

注册Autofac

在Program的var app = builder.Build();前加上这段代码

.net6 引入autofac_.net_02


1 builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
2 builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
3 {
4 builder.RegisterType<LogRecordsService>().As<ILogRecordsService>();
5 });


1 builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
2 builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
3 {
4 builder.RegisterType<LogRecordsService>().As<ILogRecordsService>();
5 });

 自动注册一个类库的服务

在Program注入autofac

.net6 引入autofac_程序集_03


builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());

builder.Host.ConfigureContainer<ContainerBuilder>(builder =>

{

Assembly assembly = Assembly.Load(ServiceCore.GetAssemblyName());

builder.RegisterAssemblyTypes(assembly)

.AsImplementedInterfaces()

.InstancePerDependency();

});


.net6 引入autofac_程序集_04

1 builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
2 builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
3 {
4 Assembly assembly = Assembly.Load(ServiceCore.GetAssemblyName());
5 builder.RegisterAssemblyTypes(assembly)
6 .AsImplementedInterfaces()
7 .InstancePerDependency();
8 });

.net6 引入autofac_程序集_04


在要注册的类库下的某个类中,写一个方法

.net6 引入autofac_ide_06


 .net6 引入autofac_ide_07


public static class ServiceCore
{
/// <summary>
/// 获取程序集名称
/// </summary>
/// <returns></returns>
public static string GetAssemblyName()
{
return Assembly.GetExecutingAssembly().GetName().Name;
}
}

引入NuGet

.net6 引入autofac_ide

注册Autofac

在Program的var app = builder.Build();前加上这段代码

.net6 引入autofac_.net_02


1 builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
2 builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
3 {
4 builder.RegisterType<LogRecordsService>().As<ILogRecordsService>();
5 });


1 builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
2 builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
3 {
4 builder.RegisterType<LogRecordsService>().As<ILogRecordsService>();
5 });

 自动注册一个类库的服务

在Program注入autofac

.net6 引入autofac_程序集_03


builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());

builder.Host.ConfigureContainer<ContainerBuilder>(builder =>

{

Assembly assembly = Assembly.Load(ServiceCore.GetAssemblyName());

builder.RegisterAssemblyTypes(assembly)

.AsImplementedInterfaces()

.InstancePerDependency();

});


.net6 引入autofac_程序集_04

1 builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
2 builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
3 {
4 Assembly assembly = Assembly.Load(ServiceCore.GetAssemblyName());
5 builder.RegisterAssemblyTypes(assembly)
6 .AsImplementedInterfaces()
7 .InstancePerDependency();
8 });

.net6 引入autofac_程序集_04


在要注册的类库下的某个类中,写一个方法

.net6 引入autofac_ide_06


 .net6 引入autofac_ide_07


public static class ServiceCore
{
/// <summary>
/// 获取程序集名称
/// </summary>
/// <returns></returns>
public static string GetAssemblyName()
{
return Assembly.GetExecutingAssembly().GetName().Name;
}
}