Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Autofac多租户ASP.NET应用程序返回租户而不是标识符_C#_Asp.net_Autofac_Ioc Container_Multi Tenant - Fatal编程技术网

C# Autofac多租户ASP.NET应用程序返回租户而不是标识符

C# Autofac多租户ASP.NET应用程序返回租户而不是标识符,c#,asp.net,autofac,ioc-container,multi-tenant,C#,Asp.net,Autofac,Ioc Container,Multi Tenant,我使用Autofac作为IoC容器,并带有多租户包 我有这样的容器设置: var builder = new ContainerBuilder(); // Register the controllers builder.RegisterControllers(typeof(Deskful.Web.DeskfulApplication).Assembly); // Tenant Identifier var tenantIdentifier = new RequestSubdomai

我使用Autofac作为IoC容器,并带有多租户包

我有这样的容器设置:

var builder = new ContainerBuilder();

// Register the controllers    
builder.RegisterControllers(typeof(Deskful.Web.DeskfulApplication).Assembly);

// Tenant Identifier
var tenantIdentifier = new RequestSubdomainStrategy();

builder.RegisterInstance(tenantIdentifier).As<ITenantIdentificationStrategy>();

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

// Tenant container
var mtc = new MultitenantContainer(tenantIdentifier, container);

// Set autofac as dependency resolver
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
然后,在我需要租户的控制器中,我可以在注入
itenantIdentificationsStrategy
后执行以下操作:

var tenantId = this.TenantIdStrategy.IdentifyTenant<int>();
public interface IDeskfulTenantIdentificationStrategy : ITenantIdentificationStrategy
{
    ITenant Tenant { get; }
}
var tenantId=this.tenantitstrategy.IdentifyTenant();
我的问题是,如何在标识过程中存储租户对象,以便访问租户的所有属性


因为现在它只返回id。

不知道这是否是一个正确的解决方案,但我最后做了以下操作

首先,我创建了一个新接口来扩展当前的
ITenantIdentificationStrategy

var tenantId = this.TenantIdStrategy.IdentifyTenant<int>();
public interface IDeskfulTenantIdentificationStrategy : ITenantIdentificationStrategy
{
    ITenant Tenant { get; }
}
我扩展了与租户属性的接口

然后在标识符类中,我在标识期间设置租户属性:

public class RequestSubdomainStrategy : IDeskfulTenantIdentificationStrategy
{
    private ITenant _tenant;

    public ITenant Tenant
    {
        get
        {
            return _tenant;
        }
        private set
        {
            _tenant = value;
        }
    }

    public bool TryIdentifyTenant(out object tenantId)
    {
        tenantId = null;

        try
        {
            var context = HttpContext.Current;
            if (context != null && context.Request != null)
            {
                var site = context.Request.Url.Host;

                Tenant = new Tenant("tenant1.deskfull.be", "connString", "Tenant 1") { Id = 20 };

                tenantId = Tenant.Id;
            }
        }
        catch { }

        return tenantId != null;
    }
}
最后,我在我的autofac容器中注册新接口:

var tenantIdentifier = new RequestSubdomainStrategy();

builder.RegisterInstance(tenantIdentifier)
.As<IDeskfulTenantIdentificationStrategy>();
var tenantIdentifier=newrequestsubdomainstrategy();
builder.RegisterInstance(租户标识符)
.As();
然后,当我在控制器中使用此功能时,我可以执行以下操作:

public string Index()
{
    int tenantId = TenantIdStrategy.IdentifyTenant<int>();

    return "Home - Index: " + TenantIdStrategy.Tenant.TenantName;
}
公共字符串索引()
{
int-tenantId=tenantitstrategy.identificationtenant();
返回“主页-索引:”+tenantitstrategy.Tenant.TenantName;
}