简体   繁体   中英

Autofac Multi-Tenant ASP.NET Application return the tenant instead of the identifier

I am using Autofac as a IoC container with Autofac.Multitenant package for multi-tenancy.

I have a container setup like this:

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));

And my identification strategy:

public class RequestSubdomainStrategy : ITenantIdentificationStrategy
{
    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;

                tenantId = 1;
            }
        }
        catch { }

        return tenantId != null;
    }
}

Then in my controller where I need the tenant I can do the following after injecting the ITenantIdentificationStrategy :

var tenantId = this.TenantIdStrategy.IdentifyTenant<int>();

My question is, how can I store the tenant object during my identification process so I can access all the properties of the tenant?

Because now it only returns the id.

Don't know if this is a correct solution, but I ended up by doing the following.

First I created a new interface to extend the current ITenantIdentificationStrategy :

public interface IDeskfulTenantIdentificationStrategy : ITenantIdentificationStrategy
{
    ITenant Tenant { get; }
}

I extended the interface with the Tenant property.

Then in my identifier class I set the Tenant property during identification:

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;
    }
}

And finally I register the new inteface in my autofac container:

var tenantIdentifier = new RequestSubdomainStrategy();

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

When I then use this inside my controller, I can do the following:

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

    return "Home - Index: " + TenantIdStrategy.Tenant.TenantName;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM