好久沒有寫文章了,最近在用.net core3.0,一些開發中問題順便記錄; 1.首先nuget引入 Autofac Autofac.Extensions.DependencyInjection 2.修改Program.cs 添加.UseServiceProviderFactory(new Auto ...
好久沒有寫文章了,最近在用.net core3.0,一些開發中問題順便記錄;
1.首先nuget引入
Autofac
Autofac.Extensions.DependencyInjection
2.修改Program.cs 添加.UseServiceProviderFactory(new AutofacServiceProviderFactory())
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }
3.修改Startup.cs
添加 services.AddControllersWithViews().AddControllersAsServices();
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews().AddControllersAsServices(); }
添加
public void ConfigureContainer(ContainerBuilder builder) { // 在這裡添加服務註冊 }
完成現在就可以在控制器中註入方法了。