我使用asp.net mvc 4开发多租户mvc应用程序。asp.net mvc 4控制器的依赖关系解析器
我正在使用Autofac作为IOC容器,并且已经为不同程序集中的每个客户端配置了控制器。
根据当前的客户端上下文,Autofac将根据当前的客户端上下文切换出它返回哪个控制器,这通过查看路由数据来确定。
我得到一个异常
多种类型的发现匹配名为“主页”的控制器。
这看起来似乎表明Autofac正在返回多个匹配,但仔细观察,似乎MVC甚至没有为控制器解析调用Autofac。
我可以看到一个呼叫,要求从DependencyResolver
的IControllerFactory
,但从来没有为控制器本身。
是否需要实现我自己的控制器工厂ontop使用依赖关系解析器?
我已经看过mvc的源代码,并且我可以告诉DefaultControllerFactory
使用dependencyresolver并且应该解析控制器,但是我在调试时遇到了麻烦,无法准确查看发生了什么。
租户dll没有被主网站引用,但在构建之后被复制。
我的global.asax
如下所示: //首先,使用标准 // ContainerBuilder创建应用程序级默认值,就像您习惯的那样。 var builder = new ContainerBuilder(); var appContainer = builder.Build();
// Once you've built the application-level default container, you
// need to create a tenant identification strategy. The details of this
// are discussed later on.
var tenantIdentifier = new RouteDataTenantIdentificationStrategy();
// Now create the multitenant container using the application
// container and the tenant identification strategy.
var mtc = new MultitenantContainer(tenantIdentifier, appContainer);
// Configure the overrides for each tenant by passing in the tenant ID
// and a lambda that takes a ContainerBuilder.
var assemblies = AppDomain.CurrentDomain.GetAssemblies()
.AsQueryable()
.Where(a => a.IsDefined(typeof (PluginAssemblyAttribute), false));
foreach (var assembly in assemblies)
{
var pluginSpecification =
assembly.GetCustomAttributes(typeof(PluginAssemblyAttribute), false)
.OfType<PluginAssemblyAttribute>()
.Single();
var assembly1 = assembly;
mtc.ConfigureTenant(pluginSpecification.Tenant,
b => b.RegisterControllers(assembly1));
}
DependencyResolver.SetResolver(new AutofacDependencyResolver(mtc));
租户标识策略如下所示:
public class RouteDataTenantIdentificationStrategy
: ITenantIdentificationStrategy
{
public bool TryIdentifyTenant(out object tenantId)
{
tenantId = null;
var context = HttpContext.Current;
try
{
if (context == null || context.Request == null)
{
return false;
}
}
catch (HttpException)
{
// This will happen at application startup in MVC3
// integration since the ILifetimeScopeProvider tries
// to be resolved from the container at the point where
// a new AutofacDependencyResolver is created.
tenantId = null;
return false;
}
var routeData = RouteTable.Routes.GetRouteData(
new HttpContextWrapper(context));
if (routeData != null)
tenantId = routeData.Values.ContainsKey("tenant") ?
routeData.Values["tenant"] : null;
return tenantId != null;
}
}
编辑:完整的例外是
The request for 'Home' has found the following matching controllers:
MultiTenantViewEngine.Web.Controllers.HomeController
MultiTenantViewEngine.Web.Tenant1.Controllers.HomeController]
System.Web.Mvc.DefaultControllerFactory.GetControllerTypeWithinNamespaces(RouteBase route, String controllerName, HashSet`1 namespaces) +230
System.Web.Mvc.DefaultControllerFactory.GetControllerType(RequestContext requestContext, String controllerName) +833
System.Web.Mvc.DefaultControllerFactory.System.Web.Mvc.IControllerFactory.GetControllerSessionBehavior(RequestContext requestContext, String controllerName) +196
System.Web.Mvc.MvcRouteHandler.GetSessionStateBehavior(RequestContext requestContext) +267
System.Web.Mvc.MvcRouteHandler.GetHttpHandler(RequestContext requestContext) +61
System.Web.Mvc.MvcRouteHandler.System.Web.Routing.IRouteHandler.GetHttpHandler(RequestContext requestContext) +44
System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context) +352
System.Web.Routing.UrlRoutingModule.OnApplicationPostResolveRequestCache(Object sender, EventArgs e) +144
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +239
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +114
更深入地在这看这似乎表明,这个错误发生因为GetControllerTypeWithinNamespaces()返回多个名称空间。
有没有办法解决这个问题?
你的RouteDataTenantIdentificationStrategy是什么样子的,并且你在它里面放置了一个断点,看它是否在解析控制器时被调用? – 2013-02-18 04:27:43
刚刚添加的话,会仔细检查,看看它被称为 – 2013-02-18 04:55:37
看来它被调用和返回正确的tenantid – 2013-02-18 05:03:35