【问题标题】:Autofac ASP.NET Core Multitenant: Can't call IServiceCollection extensions on per-tenant ContainerBuilderAutofac ASP.NET Core 多租户:无法在每个租户的 ContainerBuilder 上调用 IServiceCollection 扩展
【发布时间】:2020-11-28 15:38:30
【问题描述】:

我想制作一个多租户 ASP.NET Core 应用程序。我正在关注documentation,我正在打电话的地方

var mtc = new MultitenantContainer(container.Resolve<ITenantIdentificationStrategy>(), container);

mtc.ConfigureTenant(
    tenant.Name,
    builder=>
    {
        // register per-tenant services here
    });

我想为每个租户注册一个数据库连接。我正在使用的库在IServiceCollection 上提供了一个扩展方法,所以我会经常调用services.AddDataBase(),但在lambda 中,builder 参数的类型是ContainerBuilder,所以很自然,扩展在这里不起作用。

有没有办法将builder 转换为IServiceCollection 以便能够调用扩展方法?还是只能通过扩展方法的实现来复制整个逻辑?

【问题讨论】:

    标签: c# asp.net-core autofac multi-tenant


    【解决方案1】:

    我想出了一个相当简单的方法:

    mtc.ConfigureTenant(
        tenant.Name,
        builder =>
        {
            IServiceCollection services = new ServiceCollection();
    
            services.AddDataBase();
    
            builder.Populate(services);
        });
    

    我没有将构建器转换为IServiceCollection,而是实例化一个新构建器,然后使用ContainerBuilder 上的Populate() 扩展方法复制绑定。

    【讨论】:

    猜你喜欢
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多