Autofac多租户 - 我怎么路线子域? [英] Autofac MultiTenant - how do I route to a subdomain?

查看:179
本文介绍了Autofac多租户 - 我怎么路线子域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里的n00b。重新问的问题,因为我没有标签是正确的。

n00b here. Re-asking question because I didn't tag it right.

我想利用Autofac的MutliTenant功能。我有一个例子从源文件的工作。我已经扫描的文档时遇到了问题搞清楚如何路线的租户。

I am trying to utilize Autofac's MutliTenant feature. I got an example "working" from the source files. I've scanned the docs and am having trouble figuring out how to "route" the tenants.

目前,我想利用一个基本的CRUD应用程序的单一code基地。该应用程序CRUD由几个不同的网站上使用,只专注于特定服务对个人网站。

Currently, I'd like to utilize a single code base for a basic CRUD app. The CRUD app will be used by several different sites, just focused on specific services for the individual site.

我想最终会做到这一点:

I'm wanting to do this eventually:


  • codebase.website1.com(租客1)

  • codebase.website2.com(租客2)

  • codebase.website3.com(租户3)

任何想法或引用?谢谢你。

Any thoughts or references? Thanks.

推荐答案

如果您签出的 wiki上的多租户Autofac文档你会发现,你确定租户的方式是通过实施 ITenantIdentificationStrategy 。有展示如何从一个参数获得租客的要求,如查询字符串wiki页面上给出一个样本。

If you check out the Autofac multitenant documentation on the wiki you'll notice that the way you determine the tenant is by implementing an ITenantIdentificationStrategy. There is a sample given on that wiki page showing how to get the tenant from a parameter in the request, like a query string.

这是很容易修改这个例子来看看请求的其他部分 - 主机名,域名,或任何其他

It is easy enough to modify the example to look at some other part of the request - the host name, the domain name, or whatever else.

using System;
using System.Web;
using AutofacContrib.Multitenant;

namespace DemoNamespace
{
  public class DomainStrategy : 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.Authority;
          // Here's where you map the site to the tenant ID:
          tenantId = MapTheSiteToTheTenantId(site);
        }
      }
      catch(HttpException)
      {
        // Happens at app startup in IIS 7.0
      }
      return tenantId != null;
    }
  }
}

显然,您需要按摩的为你工作。你是怎么做的映射,无论你作为默认租户​​ID或没有,等返回null。

Obviously you'll need to massage that to work for you. How you do the mapping, whether you return null as the default tenant ID or not, etc.

请注意,如果你基于HTTP请求值测试,那么任何时候的依赖得到解决,没有网络的背景下,你会得到的应用程序级的依赖关系,而不是特定于租户的依赖...因为你将不能够识别的租户。你看在catch块的一个小的神器 - 如果任何依赖关系得到在应用程序启动解决,则不一定是一个网页上下文,以便IIS 7.0抛出,当你调用一个HttpContext.Current HttpException。你必须测试这样的东西。

Note that if you're testing based on an HTTP request value, then any time a dependency is resolved and there is no web context, you're going to get application-level dependencies, not tenant-specific dependencies... because you won't be able to identify the tenant. You see a small artifact of that in the catch block - if any dependencies get resolved at application startup, there's not necessarily a web context so IIS 7.0 throws an HttpException when you call HttpContext.Current. You'll have to test for stuff like that.

此外,你要考虑租户ID映射的缓存策略,如果是,比如说,一个服务调用或东西贵。每当你解决一个多租户依赖的战略被调用,所以你要做出战略实施尽可能高效。

Also, you'll want to consider a caching strategy for tenant ID mappings if it's, say, a service call or something expensive. Every time you resolve a multitenant dependency the strategy gets invoked, so you want to make the strategy implementation as efficient as possible.

我真的建议检查出文档。这是长,但那是因为多租户是一个复杂的话题,有很多的地面覆盖。如果你在那里潜水,你会找到你这样的问题。

I would really recommend checking out that documentation. It's long, but that's because multitenancy is a complex topic and there's a lot of ground to cover. If you dive in there, you'll find the answers to questions like this.

这篇关于Autofac多租户 - 我怎么路线子域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆