在已解决的子范围上设置活动的租户

时间:2019-06-26 21:11:46

标签: c# autofac

是否可以在子范围内设置活动租户?我以为它将对全局产生影响,但是在子作用域(ILifetimeScope)解决之后 不会。

依赖项

<package id="Autofac" version="4.9.2" targetFramework="net471" />
<package id="Autofac.Multitenant" version="4.2.0" targetFramework="net471" />

复制

使用.net框架控制台应用程序编写的复制品以及上面列出的依赖项。

using Autofac;
using Autofac.Multitenant;

namespace ConsoleAutoFacTenants
{
    class Program
    {
        static void Main(string[] args)
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<GeneralReader>().As<IReader>().InstancePerDependency();
            var appContainer = builder.Build();

            var tenantIdentifier = new AutomationTenantStrategy();
            var mtc = new MultitenantContainer(tenantIdentifier, appContainer);

            mtc.ConfigureTenant("1", b => b.RegisterType<SpecificReader>().As<IReader>().InstancePerDependency());

            // expected
            var reader1 = mtc.Resolve<IReader>();
            System.Diagnostics.Debug.Assert(reader1.Name == "General");

            // unexpected result in debug.assert, assumed that reader2 would resolve type SpecificReader
            var childScoped = mtc.BeginLifetimeScope();
            tenantIdentifier.TenantId = "1";
            var reader2 = childScoped.Resolve<IReader>();
            System.Diagnostics.Debug.Assert(reader2.Name == "Specific");
        }
    }
    internal sealed class AutomationTenantStrategy : ITenantIdentificationStrategy
    {
        public object TenantId { get; set; }
        public bool TryIdentifyTenant(out object tenantId)
        {
            var activeTenant = this.TenantId;
            if (TenantId == null)
            {
                tenantId = null;
                return false;
            }
            tenantId = activeTenant;
            return true;
        }
    }
    public interface IReader
    {
        string Name { get; }
    }
    public sealed class GeneralReader : IReader
    {
        public string Name => "General";
    }
    public sealed class SpecificReader : IReader
    {
        public string Name => "Specific";
    }
}

1 个答案:

答案 0 :(得分:1)

当您从合并范围中解析ILifetimeScope时,它会返回自身并且不会创建子寿命期。

此行:

var childScoped = mtc.Resolve<ILifetimeScope>();

应替换为

var childScoped = mtc.BeginLifetimeScope();

创建新的LifetimeScope时,多租户模块会将tenantId设置为作用域。在您的示例中,您必须在创建子LifetimeScope之前“更改” tenantId

tenantIdentifier.TenantId = "1";
var childScoped = mtc.BeginLifetimeScope();

已解决的IReader将成为为租户指定的地址。