1

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

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.

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 (Tenant 1)
  • codebase.website2.com (Tenant 2)
  • codebase.website3.com (Tenant 3)

Any thoughts or references? Thanks.

4

1 に答える 1

1

wiki で Autofac マルチテナントのドキュメントを確認すると、テナントを決定する方法がITenantIdentificationStrategy. その wiki ページには、クエリ文字列などのリクエストのパラメーターからテナントを取得する方法を示すサンプルがあります。

この例を変更して、リクエストの他の部分 (ホスト名、ドメイン名など) を確認するのは簡単です。

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 を返すかどうかなど。

HTTP 要求値に基づいてテストしている場合、依存関係が解決され、Web コンテキストがない場合はいつでも、テナント固有の依存関係ではなく、アプリケーション レベルの依存関係を取得することに注意してください...テナントを特定できません。その小さなアーティファクトが catch ブロックに表示されます。アプリケーションの起動時に依存関係が解決された場合、必ずしも Web コンテキストが存在するとは限らないため、IIS 7.0 は HttpContext.Current を呼び出すと HttpException をスローします。そのようなものをテストする必要があります。

また、テナント ID マッピングがサービス コールまたは高価なものである場合は、キャッシュ戦略を検討する必要があります。マルチテナントの依存関係を解決するたびに戦略が呼び出されるため、戦略の実装をできるだけ効率的にしたいと考えています。

そのドキュメントをチェックすることを本当にお勧めします。長くなりますが、これは、マルチテナンシーが複雑なトピックであり、カバーすべきことがたくさんあるためです。そこに飛び込めば、このような疑問への答えが見つかります。

于 2012-06-07T14:59:04.093 に答える