global.asax breakpoint not hit - asp.net

I have some code in my ASP.NET app in C# that's in the Global.asax.cs code file. In the Application_Start, Session_Start and Application_Begin Request I have set some breakpoints. However none of these are ever hit. I'm working on my local machine with VS8.
Here's what I've tried:
Stopped the ASP Dev Server
Deleted all ASP.NET Temporary files
Created new Global.asax
Closing VS and opening back up
Clean and Rebuild project
Upon trying my after these, the breakpoints will not hit.
Any ideas why this might be?

I've run into this same problem. I'm assuming you are using your local IIS instead of the VS Development Server. If this is the case, you won't be able to debug/Step through this code in the Global.asax.cs file because by the time the debugger has attached, this code has already executed in IIS. However, if you use the Dev server, you have the ability to get to this code as the debugger will already be attached.
So, Change the server in your project properties to use the Visual Studio Development Server.
This can be found by right clicking your project within Visual Studio > Project Properties > Web tab > Use Visual Studio Development Server.

What helped me was to add:
System.Diagnostics.Debugger.Break(); to that Application_Start() method.

On a project I was working on, the "Start external program" option was selected in the tab:
Visual Studio > Project Properties > Web
This was causing the breakpoints to not be hit.
Changing "Start Action" to "Current Page" fixed this problem for me.

As metioned in another answer, the Global.asax code will run before the debugger gets chance to attach. To get around that you can start debugging, then go into IIS and Stop/Start the Application which should then have the debugger pick up the restart.

If you do not want to use Visual Studio Development Server and use IIS, you can do the following (tested in VS2015 Professional)
1) Application pool in a running state - make sure that used Application Pool is running by accessing a page from your Web app (it may be stopped due to a Idle timeout).
2) Attach to process - Ctrl-Alt-P or Debug -> Attach to Process -> lookup your w3wp process that corresponds to your application.
Attach to process can be greatly sped up by using ReAttach extension which creates shortcuts to recently attached processes.
3) Application pool recycle - make sure that the application pool recycles by either entering IIS and recycling it or even faster, by entering web.config, making a no-effect change (put some blank outside of tags) and saving it
4) Start debugging - Perform a request to any page from your web application to force its initialization. You should be able to place and hit breakpoints from any of the startup methods in the Global.asax
protected void Application_Start()
void Application_BeginRequest(Object sender, EventArgs e)
protected void Session_Start(object sender, EventArgs e)

I had this issue twice. The first project this happened to me, I went to Project Properties->Build->Advanced... and selected "full" from the Debug Info dropdown.
The second time, the breakpoints in my global.asax we're not being hit but it gave this warning if I hovered over the breakpoint: "The source code is different from the original version."
I finally figured out that in this case, I had made a backup copy of this project to preserve the code and was working with the copy. Even though it was in a different folder, it was still using the dll for the virtual directory of the original: http://localhost:4952/
So I changed that in Project Properties->Web->Project URL. I just changed the port to http://localhost:49523/ and it worked.
It prompted me to create the virtual directory, I said yes and haven't had any problems with breakpoints since.

Related

When "Use Local IIS Web server" is on, any thrown exceptions seem to hang W3WP process

At our shop, we develop on Windows Server 2008 VMs running IIS 7, same as our production web servers. With VS2010 (and VS2008, but more so 2010), I've been having issues with debugging our ASP.NET applications. If the application throws an exception, I'll often kill the debugger (Shift-F5) so I can correct the faulty code. But after doing this, the process in IIS continues running, with the exception!
This leads to numerous faulty scenarios. Sometimes the exception floats to the top at IIS, and I get a prompt asking "Would you like to debug this process in [choice of Visual Studios]" and other times there is no prompt but the debugger reattaches to the faulty process instead of starting a new one the next time I go to debug.
What am I doing wrong here? Or what is configured wrong? I want the workflow I had back: run web site, see exception, fix code, run web site again to verify fix. Instead I have to manually kill W3WP in the task manager in order to continue working.
Extra info: It may also be related to the fact that the solution contains both a web site application and a web service application which the site application accesses, both running on the local IIS in two virtual directories. Normally the attached debugger will drop into either the web site or service, if either has a problem.
I wonder if there's something else going on that is being hidden. Can you setup Visual Studio to break on all exceptions. From with in the IDE press CTRL + ALT + E. That will open the Exceptions dialog.
By default, the IDE will only break on user-unhandled exceptions. If a different exception is being thrown you might not know about it. Add checks to each checkbox in the "Thrown" column and try running through your code another time. If another exception is being generated, Visual Studio will break on it now.

Redirecting from asp.net webpage to Silverlight page

Within one solution called online exam i've two web projects one using asp.net other using silver light. Now in asp.net page in the the button click event if i give response.redirect to the page in silverlight project it says file not found.
protected void btnTakePicture_Click(object sender, EventArgs e)
{
Response.Redirect("http://localhost:2861/ImageCaptureSR.Web/ImageCaptureSRTestPage.aspx");
}
Am i missing something ??
I added silver light project by adding existing website option
The URL (namely the port) appears to be served by the MSVS integrated ASP.NET server. If it really is and your solution consists of two independent projects, I think that the link is broken because the Silverlight project is not running. You can launch two instances of MSVS and start both projects in either of them. Then you can point your browser to either application and the URL will work.
Alternatively, you could install IIS and deploy both projects there. Then you would need no MSVS server and you could run your applications anytime. You would attach the debugger to the w3wp.exe process to debug either of the two projects.
--- Ferda

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

Why are the Global.asax events not firing in my ASP .NET website?

I've been developing an ASP .NET 3.5 web application against Cassini, the built-in web development server, rather than against IIS.
In my Global.asax file, in the Application_Start event handler, I have some code which logs the fact that the website has started up. This all works fine with Cassini.
Since deploying the site to a virtual directory on a test server using IIS6, I am finding there are no log entries being written, and so I'm concluding that the Application_Start handler is not firing.
I then tried removing the virtual directory and running the site directly out of the root of the website on the test server, but it didn't make any difference - still no log entry for application start.
I know these events should fire irrespective of my deployment environment, has anyone got any ideas what is going wrong here?
I had a similar problem and I was wrestling with it for several days.
The initial problem was something else - cookies not being set in Application's EndRequest handler.
Finally I somehow managed to realize, that the problem actually was that the event is not being fired at all.
It took some time to find that out, because all was working fine on my machine.
But on the production server - quiet as a tomb.
I am only writing this, because I really hope to save from troubles at least one person.
The real reason for the problem was a missing global.asax file on the production server.
The global.asax file was present on my computer, because the development environment is located there.
I had prepared a deployment bat file, which coppies files from the development folder, removes the unnecessary ones and makes a package.
Well - the global.asax file was marked for deletion in that script.
After starting to deploy it all the problems went away.
I hope I helped.
If you make a request to your app does the "Application_Start" fire then? I don't believe it will be started until the first request is made.
How are you logging? Is it possible that your logging component is not correctly set up? For a quick test try throwing an exception inside Application_Start and that will tell you quickly whether or not the event is being raised.
In your deployed enviroment, what is the thing you're calling? The reason I ask is because if you're calling a WCF based web service (ending in .svc), then Applicaiton_Start will not fire as the call to WCF isn't going through the ASP.NET pipeline. This wouldn't necessarily rear it's head w/ Cassini.
You may be getting a runtime exception that is occurring before your .NET code even gets a chance to run. If you look under the Event Viewer's Application logs, you may see some warnings or errors that will clue you in to what is happening.
When you develop in Cassini you are running the application under the user's account- probably administrator. Once you've deployed to IIS, you are (hopefully) running under a lower privilidge account.
This lack of appropriate permissions is probably the reason why your application is not working correctly- I would check the security settings to write to the log (presumably you are writing to a log file?).
1) iisreset /stop
2) push the published code to iis virtual directory's physical folder.
3) iisreset /start
4) make web request
Still not sure after this. the do a Thread.Sleep(60000); and attach with remote debugger to the process w3wp.exe imagename.
There may be more than one process of that name but this one is managed code also doesn't hurt to attach to multiple. Set the break point after the sleep. After the one minute sleep step through.
I thought I had a similar problem, working on some old projects, I was under the impression that the global.asax was not running because the code would first go into building my authentication controller which would fail because of class not registered (that part happens in the global.asax).
Turned out it was due to a config item. When I moved the AuthenticationModule declaration into handler section in web config, it worked fine with IIS express.
Basically, because of a config item, another piece of code was being executed before global.asax, making me believe that it would not be called.
We tried a lot of things.
global.asax works on local computer but not after i publish to server
Global.asax is not publishing and event are not firing in Global.asax
Global.asax not firing for Release build
Mystery of Global.asax in ASP.NET MVC application
We also tried putting the below files in root and bin directories.
App_global.asax.dll and App_global.asax.compiled files
PrecompiledApp.config
None of it worked!
We had to put raw Global.asax instead of pre-compiled dll, in order to fire the global events, for our asp .net 2.0 website.
Hope this helps someone! Cheers! Happy coding! :D
My fix was an odd one. On my development machine I am using localhost and I changed the port number it used - and then it worked.
also in IIS Manager turn on Dot Net debugging. It's under "Compilation"
If a required dll is missing from the bin directory (for example there could be a 3rd party dll which has been used in your latest build, but accidently excluded from deployment), then the Application_Start will not fire.
My issue was resolved by adding the below compiled files in the bin folder.
App_global.asax.compiled
App_global.asax.dll

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