【发布时间】:2019-07-21 05:26:43
【问题描述】:
我正在尝试将最新的 Autofac 与 asp.net core 2.2 项目一起使用。但是 autofac 的文档似乎已经过时了。 我正在尝试在没有 ConfigureContainer 的情况下使用 autofac:
// 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.
//
// Note that Populate is basically a foreach to add things
// into Autofac that are in the collection. If you register
// things in Autofac BEFORE Populate then the stuff in the
// ServiceCollection can override those things; if you register
// AFTER Populate those registrations can override things
// in the ServiceCollection. Mix and match as needed.
builder.Populate(services);
builder.RegisterType<MyType>().As<IMyType>();
this.ApplicationContainer = builder.Build();
// Create the IServiceProvider based on the container.
return new AutofacServiceProvider(this.ApplicationContainer);
}
但在 asp.net core 2.2 中 ConfigureServices 方法签名不同
public void ConfigureServices(IServiceCollection services)
{
}
没有返回类型。 有人知道如何在 asp.net core 2.2 中使用 autofac 吗?
【问题讨论】:
-
ConfigureServices是一种约定方法,您可以将其更改为返回服务提供者,它仍然可以工作。 docs.microsoft.com/en-us/aspnet/core/fundamentals/… -
甚至文档也显示了如何在没有 ConfigureContainer autofac.readthedocs.io/en/latest/integration/… 的情况下做到这一点
标签: c# .net asp.net-core .net-core autofac