Application_start not working

后端 未结 5 504
青春惊慌失措
青春惊慌失措 2021-01-14 00:32

I have written some code in the application_start() method in my global.asax file. It does not get called when I deploy my application on IIS

5条回答
  •  伪装坚强ぢ
    2021-01-14 01:05

    There are few things you need to know before you are trying to debug Appplication_Start. There are -

    One : When the code executes and why it is almost impossible to debug by attaching to it.

    The application start method is executed when the application pool starts and your website is being started up for the first time. If you deploy new deliverables to IIS, then IIS might restart it itself, but there is no guarantee that it will. So, deploying new codes does not guarantee that it will restart the pool and he execution of application start. You should restart your application pool to guarantee execution of application start.

    While debugging IIS applications, Visual Studio attaches itself to a process something named w3wp.exe or similart (I forgot the actual executable name), which is the worker process and only available after, remember after, your application pool is up and your site is up. So, in other words, if you are seeing this in service list, then the application start has already been executed and attaching to it will not give you a chance to debug it. It is kind of a tug of war with time.

    So, in other words, it is kind of impossible to debug application start unless you are very very quick.

    Two, the solution 1 - With Dev Server

    Launch your application in visual studio with Asp.net development server or IIS express, then you will be able to debug. But if you really want to debug on IIS, then check the next section

    Two, the solution 2 - With IIS

    There is a library in the name System.Diagnostics, Debuggerand it has a nice way to call debugger in code. You can read it here - http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.break(v=vs.110).aspx

    Modify you application start with this -

    public void Application_Start(){
        ....... //other codes
        Debugger.Break() or Debugger.Launch()
    }
    

    When this line executes, IIS will halt execution, and will show you a debugger selector window (similar to the one attached)enter image description here, keep your solution open in vs and select that vs from the list, will be able to debug as usual... :)

    In case you are using windows 8 and the debugger does not launch, read this article to enable it -

    http://blogs.msdn.com/b/mapo/archive/2013/11/07/debugger-launch-not-displaying-jit-debugger-selection-popup-on-windows-8-8-1.aspx

    Three: A very important thing

    I noticed that you said, you are adding db entries in Application_Start. You should keep in mind that, Application_Start does not have a HttpContext, ViewContext, So your db access code may fail for so many others reasons.

提交回复
热议问题