.net core 使用Autofac

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);
}
posted @ 2018-09-12 09:08  hack301  阅读(237)  评论(0编辑  收藏  举报