为什么他们没有被调用时,我的控制器被实例化?

编程入门 行业动态 更新时间:2024-10-25 18:25:57
本文介绍了为什么他们没有被调用时,我的控制器被实例化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面是我遇到的症状:

我在一个地区一个全新的空控制器:

I have a brand new empty controller in an area:

public class JamController : Controller { public JamController() { throw new Exception("Not implemented!"); }

如果我访问 myprojectserver.example:12345/urlthatdoesnotexist ,我出现以下错误:

If I visit myprojectserver.example:12345/urlthatdoesnotexist, I get the following error:

[CompositionException: The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information. 1) Not implemented! Resulting in: An exception occurred while trying to create an instance of type 'MyWebProject.Areas.Users.Controllers.JamController'. Resulting in: Cannot activate part 'MyWebProject.Areas.Users.Controllers.JamController'. Element: MyWebProject.Areas.Users.Controllers.JamController --> MyWebProject.Areas.Users.Controllers.JamController Resulting in: Cannot get export 'MyWebProject.Areas.Users.Controllers.JamController (ContractName="System.Web.Mvc.IController")' from part 'MyWebProject.Areas.Users.Controllers.JamController'. Element: MyWebProject.Areas.Users.Controllers.JamController (ContractName="System.Web.Mvc.IController") ] System.ComponentModel.Composition.Hosting.CompositionServices.GetExportedValueFromComposedPart(ImportEngine engine, ComposablePart part, ExportDefinition definition) +55 System.ComponentModel.Composition.Hosting.CatalogExportProvider.GetExportedValue(CatalogPart part, ExportDefinition export, Boolean isSharedPart) +78 System.ComponentModel.Composition.Hosting.CatalogExport.GetExportedValueCore() +47 System.ComponentModel.Composition.Primitives.Export.get_Value() +57 System.ComponentModel.Composition.ExportServices.GetCastedExportedValue(Export export) +40 System.ComponentModel.Composition.<>c__DisplayClassa`1.<CreateStronglyTypedLazyOfT>b__6() +39 System.Lazy`1.CreateValue() +416 System.Lazy`1.LazyInitValue() +382 System.Lazy`1.get_Value() +75 MefContrib.Web.Mvc.<>c__DisplayClass4.<GetControllerType>b__0(Lazy`1 e) +53 System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +204 System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +381 System.Linq.Enumerable.ToList(IEnumerable`1 source) +58 MefContrib.Web.Mvc.CompositionControllerFactory.GetControllerType(RequestContext requestContext, String controllerName) +412 System.Web.Mvc.DefaultControllerFactory.System.Web.Mvc.IControllerFactory.GetControllerSessionBehavior(RequestContext requestContext, String controllerName) +61 System.Web.Mvc.MvcRouteHandler.GetSessionStateBehavior(RequestContext requestContext) +122 System.Web.Mvc.MvcRouteHandler.GetHttpHandler(RequestContext requestContext) +33 System.Web.Mvc.MvcRouteHandler.System.Web.Routing.IRouteHandler.GetHttpHandler(RequestContext requestContext) +10 System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context) +9709884 System.Web.Routing.UrlRoutingModule.OnApplicationPostResolveRequestCache(Object sender, EventArgs e) +82 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +699

我的问题是为什么?为什么地球上是它试图实例化这个控制器?我在哪里看,以确定是什么原因造成这一点。我是pretty确保它不是预期的行为实例上的每个控制器找不到网页。

My question is "Why?" Why on earth is it trying to instantiate this controller? Where do I look to determine what is causing this. I'm pretty sure it isn't expected behaviour to instantiate every controller on "page not found".

我茫然地知道去哪里找。有人能指出我的东西,这将有助于方向?

I'm at a loss to know where to look. Can someone point me in the direction of something that will help?

更新:

原来,SetControllerFactory方法如下导致问题:

Turns out that the SetControllerFactory method below is causing the issue:

// Tell MVC3 to use MEF as its dependency resolver. var dependencyResolver = new CompositionDependencyResolver(catalog); DependencyResolver.SetResolver(dependencyResolver); // Tell MVC3 to resolve dependencies in controllers ControllerBuilder.Current.SetControllerFactory( new CompositionControllerFactory( new CompositionControllerActivator(dependencyResolver)));

在告诉MVC3在控制器解决依赖关系一节中注释掉解决我的问题,除了那些我要求被实例没有控制器。幸运的是,这是唯一的,如果你不使用标准的Asp.Net控制器的分辨率需要(我们是)。

Commenting out the "Tell MVC3 to resolve dependencies in controllers" section fixes my issue, and no controllers except those I ask for get instantiated. Luckily, that's only needed if you're not using the standard Asp.Net controller resolution (and we are).

推荐答案

这是下降到一个错误的 MefContrib.Web.Mvc 。该组件实现了它自己的ControllerFactory从继承的 DefaultControllerFactory

This is down to a bug in MefContrib.Web.Mvc. This assembly implements it's own ControllerFactory that inherits from DefaultControllerFactory.

工厂覆盖的 GetControllerType ,我想尝试解决生活在什么地方比默认的应用程序的其他程序集控制器或它的引用。 GetControllerType的实现首先调用到 base.GetControllerType ,看是否能Defaultcontroller解决它。

The factory overrides GetControllerType, I assume to try and resolve controllers that live in assemblies somewhere other than the default application or it's references. The implementation of GetControllerType first calls into base.GetControllerType to see if Defaultcontroller can resolve it.

如果它不能 - 这是不存在的URL的情况下 - 它要求MEF为实现一个IController全部出口。这将返回的IEnumerable&LT;&懒LT;一个IController&GT;方式&gt; 有一个项目为每个在bin /文件夹实现一个IController(默认)班

If it can't - which is the case for urls that don't exist - it asks MEF for all exports that implement IController. This returns an IEnumerable<Lazy<IController>> with one item for every class that implements IController in the bin/ folder (by default).

然后,它运行在IEnumerable的LINQ查询,调用的GetType()每个懒&LT的Value属性;一个​​IController取代。请求懒&LT的价值; T&GT;强制要创建的实例。这就是正在建设中的bin /每个控制器为不存在的页面。

It then runs a linq query over the IEnumerable, calling GetType() on the Value property of each Lazy<IController>. Requesting the Value of a Lazy<T> forces the instance to be created. This is why every controller in the bin/ is being constructed for a page that doesn't exist.

我不认为这是一个简单的问题,妥善解决,因为没有得到来自懒惰和LT类型实例的方式; T&GT; .value的,而无需创建值。但是,通过从注册Asp.Net的的ControllerFactory AppStart_MefContribMVC3.cs删除线 - 你已经有效地利用MefContrib.Web.Mvc的的ControllerFactory停了下来,只用Asp.Net的DefaultControllerFactory而不是

I don't think this is an easy problem to fix properly as there is no way of getting the Type instance from Lazy<T>.Value without creating the value. However by removing the lines from AppStart_MefContribMVC3.cs that register the ControllerFactory with Asp.Net - you have effectively stopped using MefContrib.Web.Mvc's ControllerFactory and just used Asp.Net's DefaultControllerFactory instead.

更多推荐

为什么他们没有被调用时,我的控制器被实例化?

本文发布于:2023-11-03 16:00:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555516.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   实例

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!