我有一个相当复杂的MVC应用程序,必须在应用程序启动时初始化。我试图在呈现第一个MVC页面后诊断App池重新启动的原因。为了诊断这个问题,我在Application_Start和Application_End上添加了断点。按预期调用Applicaiton_Start。在我的应用程序第一个返回的HTML / Razor页面的末尾,调用Application_End。在下一页请求中,再次调用Application_Start,然后似乎按预期运行而不重新启动。
我认为这是由Razor在运行时编译视图引起的,然后会更新BIN foldee。我知道当BIN文件夹更新时,IIS和IIS Express会重新启动APP池,所以我认为这个MVC Razor compllation导致IIS进程重新启动应用程序池。为了缓解这个问题,我按照这里的说明进行操作:https://chrismckee.co.uk/asp-net-mvc-compiled-views/预编译我的Razor视图。我知道vies现在是预编译的,因为这确实找到了几个编译问题[编译错误],这些问题直到运行时才会发现,如果没有这些配置更改导致Razor视图的预先复杂化。
所以问题是:
1)如何诊断应用程序池重启的原因?
2)有没有人知道为什么会发生这种情况并在IISExpress中运行MVC应用程序?
[......显然,如何防止它发生]
由于
jloper
更新#2:
我查了浏览器链接,很快发现它没有必要,也没有被使用过。我关闭了BrowserLink,果然,例外消失了。现在按预期调用Application_Start,调用Application_End [并且没有发生异常(System.GetLastError()返回null]。永远不会调用Application_Exception。第二次调用Application_Start。
调用Application_End时,将重置应用程序的所有状态。
更新#1:
根据建议,我添加了Application_Error并使用Server.GetLastError()检索了最后一个异常。以下是返回的异常:
The thread 0xc4c has exited with code 259 (0x103).
System.Web.HttpException (0x80004005): The controller for path '/__browserLink/requestData/8cf754f80e264fd392f4a0fbffea67e4' was not found or does not implement IController.
at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
另外,我向Application_End添加了相同的代码。在调用Application_End时,System.GetLastError()返回空值。
答案 0 :(得分:3)
我在网上找到了一个技巧(忘记在哪里)你可以使用反射来获取application_end事件中的原因:
Sub Application_End(sender As Object, e As EventArgs)
Dim runtime As HttpRuntime = CType(GetType(HttpRuntime).InvokeMember("_theRuntime", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static Or Reflection.BindingFlags.GetField, Nothing, Nothing, Nothing), HttpRuntime)
Dim shutDownMessage = CType(runtime.GetType().InvokeMember("_shutDownMessage", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.GetField, Nothing, runtime, Nothing), String)
Dim shutDownStack = CType(runtime.GetType().InvokeMember("_shutDownStack", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.GetField, Nothing, runtime, Nothing), String)
'Log reason
在添加IIS跟踪之外,这是一种特定于代码的方式,我能够提取原因......
答案 1 :(得分:1)
Brian Main的答案非常有效,向我展示了项目文件夹中文件发生更改的消息,导致重新启动。
对于那些感兴趣的人,这是C#版本。
var runtime = typeof (HttpRuntime).InvokeMember("_theRuntime", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField, null, null, null);
var shutDownMessage = runtime.GetType().InvokeMember("_shutDownMessage", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null, runtime, null);
var shutDownStack = runtime.GetType().InvokeMember("_shutDownStack", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null, runtime, null);
答案 2 :(得分:0)
您的问题是Visual Studio“浏览器链接”功能中的错误。这是在更新中修复的。将最新的更新(Update 4)应用于Visual Studio 2013,您的问题应该得到解决。