我有一个MVC应用程序,它基本上有两个堆栈跟踪,我想知道为什么有两个呢?...
[MissingMethodException: No parameterless constructor defined for this object.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +98
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241
System.Activator.CreateInstance(Type type, Boolean nonPublic) +69
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +67
[InvalidOperationException: An error occurred when trying to create a controller of type 'PteDotNet.Web.Controllers.BlogController'. Make sure that the controller has a parameterless public constructor.]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +182
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +232
System.Web.Mvc.<>c__DisplayClass6.<BeginProcessRequest>b__2() +49
System.Web.Mvc.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a() +13
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func`1 func) +124
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +98
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970356
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
答案 0 :(得分:0)
一个是内部异常的堆栈跟踪。
看起来你第一次得到MissingMethodException
,原因是
没有为此对象定义无参数构造函数。
导致InvalidOperationException
,原因
尝试创建“PteDotNet.Web.Controllers.BlogController”类型的控制器时发生错误。确保控制器具有无参数的公共构造函数。
显然你的BlogController
需要一个无参数的构造函数。
答案 1 :(得分:0)
我不完全确定,但它看起来像根本原因:
首先触发MissingMethodException
,然后InvalidOperationException
被解雇,因为第一个受到了外来因素
答案 2 :(得分:0)
InvalidOperationException
是抛出的异常,但由于MissingMethodException
而被抛出,此异常包含在InnerException
的{{1}}中。 InvalidOperationException
解释了出现了什么问题,但查看了InvalidOperationException
,您可以看到首先触发故障的位置。
当您对具有内部异常的异常评估InnerException
时,您将获得所有嵌套内部异常的堆栈跟踪,以帮助您理解该问题。一个常见的错误是使用ToString()
进行诊断,但是您丢失了有关内部异常的信息。除非您想隐藏这些有价值的信息,否则在记录异常时应始终使用ex.Message + ex.StackTrace
。
在MVC框架中有类似的代码:
ex.ToString()
因此,您可以获得try {
// Will throw MissingMethodException if there is no parameterless constructor.
var controller = Activator.CreateInstance(type);
...
}
catch (MissingMethodException ex) {
var message = String.Format("An error occurred when trying to create a controller of type '{0}'. Make sure that the controller has a parameterless public constructor.", type);
throw new InvalidOperationException(message, ex);
}
一个很好的解释,而不是让MissingMethodException
感到困惑。这是异常处理的最佳实践示例。