I can't debug global.asax file!
I have some codes in Application_Start()
method but when I set a break point in the method, it is ignored!
Is this normal?
I can't debug global.asax file!
I have some codes in Application_Start()
method but when I set a break point in the method, it is ignored!
Is this normal?
A simple way to break in Application_Start()
is to use the System.Diagnostics.Debugger
class. You can force the application to break by inserting System.Diagnostics.Debugger.Break()
where you would like the debugger to break.
void Application_Start(object sender, EventArgs e)
{
System.Diagnostics.Debugger.Break();
// ...
}
Application_Start()
is invoked once per AppDomain. If you're not hitting your breakpoint, it means the AppDomain was already created, so do the following:
Check that your web application is in debug mode (<compilation debug="true">
in web.config).
If you're using developer's IIS started by VS, just restart it or rebuild application.
If you're on normal IIS you have two options:
Debug - Attach to process
menu, enter computer name and then select a process to debug. It is usually a w3wp.exe working in managed mode type.Yes, it is normal.
Application_Start()
is processed by IIS.
But all other methods, for example Session_Start
, and all others, except Application_Start()
can be debugged normally.
Maybe you should try:
Another alternative to the accepted System.Diagnostics.Debugger.Break();
would be
void Application_Start(object sender, EventArgs e)
{
System.Diagnostics.Debugger.Launch();
//...
}
which shouldn't break the code and should start the debugger even if the service were started with different rights.
For me, my debug breakpoint already executed in IIS by the time the debugger is attached. So the solution was to alter global.asax with a little space and save the file. After refresh, my breakpoint is now hit.
Solution here: https://wakeupandcode.com/hitting-breakpoints-in-global-asax/
Delete the global.asax
and add a new one. In my solution, there has been a global.asax
and a global.asax.cs
.
All the methods (Session_Start
, Application_Start
, ...) have been in the bot files, but only the ones in the global.asax
have been considered. So, break points and code in the cs don't do anything.
Only after recreating the file, the global.asax.cs
had the appropriate methods and they ran.
Dont expect the Application_Start() function to be called immediately by pressing f5. Application_Start() is invoked only at the time of first request to the application is made. Strange but true.
In case all of answers not working, try:
<compilation debug="true" ... />
in web.config
. ;)