namespace Blog.AutoFacModule.Solucation.AutoFac.Register
{
public class AutofacModuleRegister : Autofac.Module
{
//重写Autofac管道Load方法,在这里注册注入
protected override void Load(ContainerBuilder builder)
{
}
/// <summary>
/// 根据程序集名称获取程序集
/// </summary>
/// <param name="AssemblyName">程序集名称</param>
public static Assembly GetAssemblyByName(String AssemblyName)
{
return Assembly.Load(AssemblyName);
}
}
}
public static IServiceProvider ForRegisterAutofac(IServiceCollection services)
{
var builder = new ContainerBuilder();
builder.Populate(services);
builder.RegisterModule<Blog.AutoFacModule.Solucation.AutoFac.Register.AutofacModuleRegister>();
var container = builder.Build();
return new AutofacServiceProvider(container);
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddControllers();
return Blog.AutoFacModule.Solucation.AutoFac.Provider.RegisterAutofac.ForRegisterAutofac(services);
}
在 Startup 的 ConfigureContainer 方法中配置 AutoFac
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterModule(new AutofacModule());
}
详见 https://docs.autofac.org/en/latest/integration/aspnetcore.html#startup-class
难道是 .net core 不支持返回System.IServiceProvider的ConfigureServices。 吗
– ZaraNet 5年前.net core 2.1 就可以。。
– ZaraNet 5年前