asp.netcore3.0mvc用autofac

coding

好久没有写文章了,最近在用.net core3.0,一些开发中问题顺便记录;

1.首先nuget引入

Autofac

Autofac.Extensions.DependencyInjection

2.修改Program.cs 添加.UseServiceProviderFactory(new AutofacServiceProviderFactory())

publicclass Program

{

publicstaticvoid Main(string[] args)

{

CreateHostBuilder(args).Build().Run();

}

publicstatic IHostBuilder CreateHostBuilder(string[] args) =>

Host.CreateDefaultBuilder(args)

.UseServiceProviderFactory(new AutofacServiceProviderFactory())

.ConfigureWebHostDefaults(webBuilder =>

{

webBuilder.UseStartup<Startup>();

});

}

3.修改Startup.cs

添加 services.AddControllersWithViews().AddControllersAsServices(); 

publicvoid ConfigureServices(IServiceCollection services)

{

services.AddControllersWithViews().AddControllersAsServices();

}

添加

public void ConfigureContainer(ContainerBuilder builder)

{

// 在这里添加服务注册

}


完成现在就可以在控制器中注入方法了。

原文出处:https://www.cnblogs.com/lr215/p/11600577.html

以上是 asp.netcore3.0mvc用autofac 的全部内容, 来源链接: utcz.com/z/509648.html

回到顶部