Application_Start not being hit in ASP.NET web app - asp.net

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.

Related

asp.net references a DLL that runs thread

I am new to ASP.NET and I need to develop an application that communicate with a RFID reader.
In order to do that, I have a DLL project which runs a thread that manages the communication with the reader, while a I have another ASP.NET project which manages the user interface.
Those two projects live in the same solution, so the ASP.NET project references DLL project.
At the beginning of the application, my "Global.asax" file initializes the reader (DLL project), running the thread it has inside, and registering some events such as "CardInside" the DLL fires, when a card is inside the reader.
My doubt is if this thread is really running because it does not do anything. I have placed more than one breakpoints in order to see if that part is being run, but nothing stops there.
I have read something about an issue regarding threads an asp.net, but since this thread is not used in a http request:
-is there any problem running a thread in ISS server at the same time as web page?
-Does an aspx.cs file register events fired by another object normally?As if it were not a web application?
-I am using visual studio 2015, could I use DEBUG object to show messages amongst my DLL lines of code?
Thanks a lot.
Yes, there are issues with running threads inside of IIS that are not requests. When no requests have come in IIS will tear down the AppDomain and that will Thread.Abort() the thread you are running your background work in.
You will need to move your code out of IIS and change it in to a actual windows service if you want it running all the time. You then can have your IIS portion talk to the windows service (or vice-versa) to process whatever the web component is.

global.asax breakpoint not hit

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.

How to identify an iisreset in an asp.net web application?

I've got an application that needs to do some work on startup (before the first request is in).
I've added the initialization code in the global.asax file (Application_start method) but this code doesn't seem to be hit after an iis reset is performed.
Is there an event which is triggered in an asp.net application when an iis reset has occurred?
Thanks.
Application start happens on first request, not on iisreset.
The site doesn't start itself..
See "Restart cache item callback on web process restart" here.
In such cases, the service will stop
running unless a page is hit and the
Application_Start is called.
Application_Start is called only when
a page is visited for the first time
in a web project.
I would suggest having a batch file that contains iisreset and an "iexplore mypage" call
Edit: apparently, you can use application end to trigger application start. YMMV
http://weblogs.asp.net/scottgu/archive/2009/09/15/auto-start-asp-net-applications-vs-2010-and-net-4-0-series.aspx
This may be of help to you.
Tho the features are new to IIS 7.5 which is only on Windows Server 2008 R2 / Windows 7.
Auto-Start Web Applications with ASP.NET 4
Some web applications need to load large amounts of data, or perform expensive initialization processing, before they are ready to process requests. Developers using ASP.NET today often do this work using the “Application_Start” event handler within the Global.asax file of an application (which fires the first time a request executes). They then either devise custom scripts to send fake requests to the application to periodically “wake it up” and execute this code before a customer hits it, or simply cause the unfortunate first customer that accesses the application to wait while this logic finishes before processing the request (which can lead to a long delay for them).
ASP.NET 4 ships with a new feature called “auto-start” that better addresses this scenario, and is available when ASP.NET 4 runs on IIS 7.5 (which ships with Windows 7 and Windows Server 2008 R2). The auto-start feature provides a controlled approach for starting up an application worker process, initializing an ASP.NET application, and then accepting HTTP requests.
Edit: Link to more information about Auto Start feature.
http://www.asp.net/LEARN/whitepapers/aspnet4#0.2__Toc253429241

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

Resources