Autofac Multi-tenant IoC Container in an ASP.NET Web API Application

风流意气都作罢 提交于 2019-12-03 00:22:28
Travis Illig

Multitenant support has been available for a long time, it's just that 3.0 is the first time we've had a NuGet package for it. :)

The RequestParameterTenantIdentificationStrategy is, as documented, just a very simple example showing one possible (and not recommended) way to identify tenant. You will have to choose for yourself how to identify your tenant based on the operating context. It could be from a web.config value, an environment variable, or some other thing in the current environment. If you don't want to use HttpContext.Current, don't. It's up to you to pick where you get that info from.

(A note on the RPTIStrategy - the part that isn't recommended is using a querystring or request parameter as the tenant ID mechanism. I use HttpContext in my production apps and it works fine. There's only so much you can abstract out before you have to actually touch the bare metal.)

There is no way out of the box to provide the lambda registration syntax you're asking for, primarily because tenant is not passed through the resolution process. The resolution process is:

  1. Identify the tenant with the strategy.
  2. Find the tenant's configured lifetime scope.
  3. Use standard Autofac Resolve style syntax.

It's intentionally simple and analogous to the existing operations. At the time of resolve, the sub-lifetime-scope belonging to the tenant is tagged with the tenant ID but the resolution operation doesn't know about the tenant ID... so the lambda wouldn't work (and probably won't anytime soon because it'd change the fundamental internals of the way Autofac works if it did).

To accomplish what you're looking for, you can use a combination of the InstancePerTenant extension when registering...

var builder = new ContainerBuilder();
builder.RegisterType<Foo>().As<IFoo>().InstancePerTenant();

...and registering the ITenantIdentificationStrategy as a dependency in your container.

builder.Register(myIdStrategy).As<ITenantIdentificationStrategy>();

Then make your class take an ITenantIdentificationStrategy rather than the tenant ID directly. Use the strategy to get the tenant ID instead.

If you REALLY want to get fancy, you could register a keyed lambda that resolves the ID strategy, then gets the tenant ID. Then you could add a parameter registration to the object like you did but using a keyed service. (I'm going to go by memory now, so you'll have to double-check my syntax here, but it'll be something like this...)

builder.Register(c => 
       {  var s = c.Resolve<ITenantIdentificationStrategy>();
          object id;
          s.TryIdentifyTenant(out id);
          return id;
       }).Keyed<object>("tenantId");

builder.RegisterType<Foo>()
       .As<IFoo>()
       .WithParameter(
         (pi, c) => pi.Name == "tenantId",
         (pi, c) => c.ResolveKeyed<object>("tenantId"))
       .InstancePerApiRequest();

Again, you'll want to double-check me on that, but I'm pretty sure that (or a minor variation) should work to get you what you want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!