.Net Core 修改IOC为Autofac

本文主要讲解如何更换Asp.Net Core的IOC为Autofac

安装Autofac

在Nuget上,找到Autofac和Autofac. Extensions. DependencyInjection,直接安装。

创建容器并注册依赖

修改Startup.cs中的代码,主要ConfigureServices(IServiceCollection services)方法。其中该方法默认的返回值为void,这里需要修改返回值为IServiceProvider。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public IContainer ApplicationContainer { get; private set; }
// ConfigureServices is where you register dependencies. This gets
// called by the runtime before the Configure method, below.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// Add services to the collection.
services.AddMvc();

// Create the container builder.
var builder = new ContainerBuilder();

// Register dependencies, populate the services from
// the collection, and build the container. If you want
// to dispose of the container at the end of the app,
// be sure to keep a reference to it as a property or field.
builder.RegisterType<MyType>().As<IMyType>();
builder.Populate(services);
this.ApplicationContainer = builder.Build();

// Create the IServiceProvider based on the container.
return new AutofacServiceProvider(this.ApplicationContainer);
}

.Net Core 修改IOC为Autofac
http://blog.chcaty.cn/2018/03/17/net-core-xiu-gai-ioc-wei-autofac/
作者
caty
发布于
2018年3月17日
许可协议