Visual Studio don't call Application_End on Debug - asp.net

I implement some code in Application_Start and Application_End (global.asax).
When i start ASP.NET Developer Server the Application_Start run normally.
When i change the web.config file, the method Application_Start is called again but Application_End is never called.
Someone know how can i force Application_End run before the second call to Application_Start

Is Application_End not being called or is it that a break point in Application_End is not being hit?
Application_End is called when the application pool is stopped.
When you are debugging you are attached to the application pool process.
What may be happening is that the debugger looses contact with the application pool before the Application_End is called, and you are therefore not hitting the break point.

My code opens a server socket in Application_Start, so Visual Studio couldn´t restart my application whitout closing it.
For safety VS don´t restart my Application.
VS calls Application_End only if he knows that noone is affected.
In my case i could be using the socket to sending or receiving data.

Related

IIS end application on every request

I've notice that on the production server, my asp.net 4.7.2 application is very slow. By debugging I've found that in global.asax the Application_start and Application_end is called on every request, causing a recompile of the application each time and slowing down the response.
I0ve test it on several servers and the behaviour is always the same.
Running the website on localhost in IIS (not iis express) this not happen.
How can I check why Application_End is called and from where?
Debugging in VS the Application_end stacktrace is empty, I cannot see from where is called.
Thanks
The problem was cause by Kaspersky Endpoint Security for Windows Server version 11.0.0.480.
For some unknown reason it act on w3wp.exe causing the application restart on each requests. The only way to solve the problem is excluding w3wp.exe from the monitored applications/processes.
There are many circumstances when the Application_end will be called, such as application pool's recycle, web.config file's change, or bin file's change. For more detailed information about Application_end be called you need to check the Event log in the sever.

How to exit an ASP.NET application gracefully when stopping debugging in VS 2010?

I develop and debug an ASP.NET application with VS 2010. My ASP.NET application holds some connections to other applications on other machines. When I stop debugging, I want these connections to be released. If this doesn't happen, these other applications fail and I have to restart them.
In the end I will store my termination code in the Application_End method in Global.asax.cs, but this one is not called when stopping debugging.
Is there a way to terminate the debugging of my ASP.NET application so that not everything is killed at once, but so that one last method is called in which I could add my termination code?
I'm not sure what your problem is but probably...
You initialize your debug session by pressing F5 and thus debugging through cassini. This way when you end your debug session the application is terminated.
If you have a configured IIS application you could simply attach to the running process - it's usually "CTRL + ALT + P" - choosing w3wp.exe (mind the checkboxes on the bottom to be checked). This way your app won't be terminated on ending the debugging session.
Does it solve your problem?
I misunderstood how debugging an ASP.NET application works. I thought the moment I stop debugging (by pressing Shift+F5) the ASP.NET application is terminated and no further line of code is executed. It was my explanation to why the Application_End method is not called.
But in fact the ASP.NET application goes on when the debugger is detached and therefore the Application_End method is not called.

Will application_start event be fired when use auto-start mode

I have read this article http://weblogs.asp.net/scottgu/archive/2009/09/15/auto-start-asp-net-applications-vs-2010-and-net-4-0-series.aspx about auto-start mode in asp.net4.0 and still don't understand is application_start event fired on application warm-up or not? Article said that application are automaticaly preloaded with the logic, described in class, registered as serviceAutoStartProvider. But what about application start, is "preloaded" means that application_start also fires, or it fires only after preloading on first request?
The event Application_Start is not fired by the IIS AutoStart feature. It is still fired upon first request.
You may check that the application is ready in "Application_Start" and run the init code if you find it is not. This way the application is compatible with IIS Express (it does not support AutoStart) and IIS 7.5.

Flakey debug behavior in application start, global.asax

Not using Cassini, but IIS7 to run an ASP.NET app.
The debug behavior is flakey. Sometimes it works, sometimes it doesn't. I was able to step into the application start event in global.asax but sometimes I can't.
Sometimes VS2008 opens the published global.asax so I have two instances of global.asax opened in VS, the source and the published.
I reset the AppDomain and IIS and I can't put my finger on why this is happening. Any clues?
To add, while it won't go into application start, I have an OnChangeEventHandler event - it does step into the event handler.
Visual Studio doesn't connect to w3wp.exe soon enough to handle breakpoints in Application_Start for IIS7-hosted applications. To debug, use the following line to call debugger explicitly,
System.Diagnostics.Debugger.Launch();
and choose to launch a new instance of Visual Studio.
make a change in the web.config, the application pool will recycle and the global.asax code will be loaded, the debugger should still be attached

Application_Start not being hit in ASP.NET web app

I'm trying to debug something in the global.asax.cs file in an ASP.NET web app and have set a breakpoint in the Application_Start() event however that event is not getting fired when I start the web app inside VS2008. I'm targeting the 3.5 framework.
What could prevent this event from being fired? Or how could I have messed up the project such that this event is no longer wired up?
One easy trick to debug newly written code in the global.asax file is to save the web.config file. Each time the config.file is saved, the application is stopped and started.
You could find useful information in this blog entry
Workaround: Debugging Global.aspx.cs Application_Start() with ASP.Net Web Server within Visual Studio
The reason behind this is that we do
not kill the ASP.Net Web Server
process after your every debug run and
hence Application_Start() is not fired
every time. There is a good reason
why we do so... Starting ASP.Net Web
Server process is an expensive task
and in most of the scenarios recycling
this process after every debug would
adversely impact your performance...
If you do not want to debug your
Application_Start() method then
probably you do not need to have the
process restart and save performance
most of the time...
One of the proposed workarounds:
You can go to your property pages of
your web application and enable Edit &
Continue like shown below:
(from the Visual Web Developer Team Blog)
If i remember correctly, Application_Start runs before the debugger can hook up to the application.
Try doing something else to check if the Application_Start method runs, like setting an application variable:
Application("app") = "started"
Then display the application variable in the page to see if it was set.

Resources