刚刚发布了1.2.3-preview版本,开始支持Autofac,喜欢的可以下载尝鲜了。
使用用法如下。
1、在项目中添加Autofac.Extensions.DependencyInjectionpackage
2、添加一个类,继承自 AutofacInjector
抽象类,并重载 Inject
,如下:
public class ExampleInjector : AutofacInjector { public override IServiceProvider Inject() { // Create the container builder. var builder = new ContainerBuilder(); // register your service // ... var container = builder.Build(); return new AutofacServiceProvider(container); } }
需要注意的点
- 不同于继承自
Injector
,在一个项目中继承自AutofacInjector
的类只允许出现一次。 - 不要使用Autofac文档中示例代码中的
builder.Populate(services)
,其实你也用不了,因为在AutofacInjector
抽象类中没有提供一个services
让你使用。