Migration from ASP.NET Core's container to Autofac

寵の児 提交于 2019-12-06 07:59:06

You should use the .AddXxx methods from Microsoft.Extensions.DependencyInjection to register it and pass the IServiceCollection to autofac.

This makes it easier to change between containers.

Of course, if you need certain features of Autofac/3rd party IoC container (autodiscovery etc), then you need to use the containers native methods.

private readonly IContainer container;
public IServiceProvider ConfigureServices(IServiceCollection services) 
{
    // your normal registrations
    services.AddSingleton<IMySingleton,MySingleton>();

    var builder = new ContainerBuilder();
    builder.Populate(services);

    // build container 
    container = builder.Build();

    // and return it
    return new AutofacServiceProvider(container);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!