Note that there are some explanatory texts on larger screens.

plurals
  1. POAutofac Multi-tenant IoC Container in an ASP.NET Web API Application
    primarykey
    data
    text
    <p><a href="http://code.google.com/p/autofac" rel="noreferrer">Autofac</a> 3.0 will have a <a href="http://code.google.com/p/autofac/wiki/MultitenantIntegration" rel="noreferrer">MultitenantIntegration</a> support and <a href="http://nuget.org/packages/Autofac.Extras.Multitenant" rel="noreferrer">its preview release is out</a> now. To try it out, I created an ASP.NET Web API application with the following configuration:</p> <pre><code>public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { var config = GlobalConfiguration.Configuration; config.Routes.MapHttpRoute("Default", "api/{controller}"); RegisterDependencies(config); } public void RegisterDependencies(HttpConfiguration config) { var builder = new ContainerBuilder(); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); // creates a logger instance per tenant builder.RegisterType&lt;LoggerService&gt;().As&lt;ILoggerService&gt;().InstancePerTenant(); var mtc = new MultitenantContainer( new RequestParameterTenantIdentificationStrategy("tenant"), builder.Build()); config.DependencyResolver = new AutofacWebApiDependencyResolver(mtc); } } </code></pre> <p>It gets the job done and creates a <code>LoggerService</code> instance as <code>ILoggerService</code> per tenant. I have two problems at this stage which I wasn't able to solve:</p> <ol> <li>I used out of the box provided <code>RequestParameterTenantIdentificationStrategy</code> here as the TenantIdentificationStrategy just for this demo application. I am able to create my custom TenantIdentificationStrategy by implementing <code>ITenantIdentificationStrategy</code> interface. However, <code>TryIdentifyTenant</code> method of the <code>ITenantIdentificationStrategy</code> makes you rely on a static instance such as <code>HttpContext.Current</code> which is something that I don't want in an ASP.NET Web API environment as I want my API to be hosting agnostic (I know that I can delegate this work to the hosting layer but I would rather not to). Is there another way to achieve this in a way that I won't rely on a static instance?</li> <li><p>I also have a chance to register tenant specific instance as below:</p> <pre><code>mtc.ConfigureTenant("tenant1", cb =&gt; cb.RegisterType&lt;Foo&gt;() .As&lt;IFoo&gt;().InstancePerApiRequest()); </code></pre> <p>However, one of my situations requires me to pass the tenant name through the constructor parameter and I would love to have something like below:</p> <pre><code>mtc.ConfigureTenant((cb, tenantName) =&gt; cb.RegisterType&lt;Foo&gt;() .As&lt;IFoo&gt;() .WithParameter("tenantName", tenantName) .InstancePerApiRequest()); </code></pre> <p>Currently there is no such an API. Is there another way to achieve this or this kind of requirement doesn't make any sense?</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload