1.nuget Autofac Autofac.Extensions.DependencyInjection
2.将Startup.cs中的
ConfigureServices
返回类型改为
IServiceProvider
public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc(); 可以将下面代码单独放入到一个文件
var builder = new ContainerBuilder(); builder.Populate(services); builder.RegisterAssemblyTypes(typeof(Startup).Assembly).AsImplementedInterfaces(); var Container = builder.Build(); return new AutofacServiceProvider(Container); }