I am using http://code.google.com/p/autofac/wiki/MultitenantIntegration version 2.4.4 with Autofac 2.4.4 on an ASP.Net MVC 3.0.
I use the new Asp.Net MVC 3 support开发者_运维知识库 (using AutofacDependencyResolver). I encounter a problem that the tenant identification strategy class (implementing ITenantIdentificationStrategy) throws "Request is not available in this context" exception.
I tried using AutofacContrib.Multitenant.Web.RequestParameterTenantIdentificationStrategy class and it also throws the same exception.
My application_start looks as follow
protected void Application_Start()
{
//wire up all the necessary objects used in this web application
IContainer container = BootStrap.RegisterAll().Build();
//multi tenant support
MultitenantContainer multiTenant = new MultiTenancy().Register(container);
DependencyResolver.SetResolver(new AutofacDependencyResolver(multiTenant.BeginLifetimeScope()));
}
Never mind. HttpContext.Current.Request is not available at Application_Start in IIS 7.0. The only solution to this is to capture the HTTPException and set the TenantId to null at Catch and return false.
public bool TryIdentifyTenant(out object tenantId)
{
var current = HttpContext.Current;
try
{
if (current == null)
{
tenantId = null;
return false;
}
var request = current.Request;
}
catch (HttpException)
{
tenantId = null;
return false;
}
//continue with your tenant identification
}
精彩评论