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

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

Related

ASP.NET Website - When does session get reset

For an ASP.NET Website where i am making updates. What would cause user sessions to reset?
From my understanding if i make any changes to files in the app_code folder or the global.asax file it will reset everyone's session but if i made a change to .cs file (in the top directory) it wont reset everyone's session?
Would it reset the session of anyone that was on that page that i updated?
Can someone help me with my understanding, thank you.
Assuming memory-based session (as opposed to sql server based).
Well, the app pool restart will blow out session.
(so, in some cases even un-handled errors will blow out session).
Modify web config?
that will case app pool to restart (and blow out session).
Update any folder outside of app_data. Again, app pool re-start (and blows out session).
change content of bin folder - again app pool re-starts, (and blows out session).
Update code behind (.cs, or vb pages (code behind)). - again, app pool re-starts, (and blows out session). (so anything that will cause a re-compile of code).
There are probably a good many more - the above is a basic list.
You can thus consider adopting sql session based, and that will thus be far more robust, and I even re-started IIS services, and users if not doing anything just looking at their web page while logged on? They were able to continue working even after a full re-start of IIS services.
On the other hand, I now always use a asp.net web site "applcation". Thus no souce code is placed on the web server, and my compile is done with visual studio before deploying. The only big issue is that if you change to a application deployment model, the even just changing one line of code behind will force/require a full re-publish of the web site.
It looks like you are using a web site, and thus source code (code behind) is placed on the web server. Thus, IIS has the job of re-compiling such code, and thus any code change will as a general rule cause a app_pool re-start, and thus loss of session.
As noted, if you change from in-memory session to SQL server based (turn that feature on), then most if not all of your session issues will go away. Even if you not using "application deployment" and have/use code behind on the site for IIS to re-compile? Then using sql server-based session management should fix all of your session() woes.

.Net core application failing to upload via FTP, hosted on IIS

I have a problem deploying a .net core application via FTP which is hosted on IIS.
The main DLLs (core application) that I want to update just wont upload, FTP just gives me a generic permission error message. I think the reason is because they are in use because then I stop the application pool, upload and restart it works just fine.
But this isn't really a solution, are there any other methods of publishing that will alleviate this problem?
Edit:
"open for write: failure"
Is the only error I'm getting. I can't find anything online and the only solution I have is restart the app pool.
I found an answer and I figured it should be here for future Googling.
The issue is as I first expected IIS proxies the request to kestrel and that means the process is in use as far as Windows is concerned. There are three solutions.
The Good Solution
Have two (or more) VMs on azure behind a load balancer. Have a script which turns off the sites one at a time, does what it needs to do and turns them back on. Do this right and no downtime!
Intermission
Before I talk about the other solutions a little explanation. I have not been working with .NET for a long time but apparently there was this thing you could do where you add a app_offline.htm and it will temporarily take down the site for you.
In the context of IIS and .Net Core it also releases the process, which is really useful as it solves my problem! Although I had to visit the web page first for it to take effect, unless I'm mistaken.
The Bad Solution
Use an automated script to rename _app_offline.htm to app_offline.htm. Do the upgrade and then revert that change. Takes your site down, kind of ugly but scripting is always better than...
The Ugly Solution
You only have access to FTP, no remote admin or proper deployment process because... reasons.
Upload an app_offline.htm, upload as little as possible and hope it doesn't break anything before deleting or renaming app_offline.htm.
Also you would have to perform any DB migrations by using EnableAutomaticMigrations = true because you have no server access or scripting methods.

application_start doesn't hit after iisreset or web.config changes

The situation is weird. We've developed a deployment system for our internal use. The system downloads precompiled binaries and copy to application directory using robocopy, excluding unchanged files.
The problem is, after this operation, the application doesn't hit application_start. Even we restart application domain. (I even restarted the IIS itself but no luck) I even change web.config manually after auto-deployment, again no luck. I run iisreset, the same, did not hit application_start. But the application is running! we use a state server, and we continue to use application like nothing happend (except application variables, they all disappear)
In application_start, we initialize some Application variables by adding like following:
logger.info("bla bla");
Application.Add("KEY",somevaluefromdb);
We do not see any log messages too.
But when i deploy same binaries manually (by copying and pasting.), the application_start hits, we see log message, and the variables are initialized.
Did anyone encounter such situation?
If PrecompiledApp.config file does not exist at publishing app root folder, the events would not execute.
I spend hours for this meaningless error :(
Related answer: Global.asax not firing for Release build

ApplicationShutdownReason.BuildManagerChange and ApplicationPool restart in IISExpress

We have a problem. We are using IISExpress 8.0 for Asp.net WebForm application(.net 4.0).
Comupter is running under Windows 7 x64.
Sometimes without any reason ApplicationPool restarts. I know that it will restart after 15 aspx\ascx file changes. But in that case it restarts without any changes.
On ApplicationEnd we found a reason of this restart. It's ApplicationShutdownReason.BuildManagerChange.
Search in the Internet won't give anything usefull details. Mostly all recomends to use IIS instead of IISExpress.
Do you know what could be a reason for it?
UPDATE:
Digging deeper into .Net 4 source code give two reasons of this shutdown.
One of them is triggered when someone changed hash.web file from Temporary Asp.net folder. For example - "c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\app\a83dcad1\be4aa699\hash\hash.web"
Second reason is when BuildManager built some object and cached BuildResult in HttpCache. And then if it's expired it checks that this BuildResult require ShutdownAppDomainOnChange on cache expiration. And if it's require it, then it triggers BuildManagerChange shutdown.
UPDATE2
In our case restart was caused by hash.web change. Seems IISExpress updates it without any source code change, but why?
UPDATE3
Microsoft has an issue about it - https://connect.microsoft.com/VisualStudio/feedback/details/783440/microsoft-visualstudio-web-host-exe-touches-hash-web-and-should-not-be-running
They say that they fixed it in Visual Studio 2012 Update 2.
This is not a full answer, so take whatever benefit from it you may take.
It seems like two things are happening: the hash.web change is probably because IIS uses the temporary location to store the application DLL that you build. When this file changes, IIS understands that you built a new version of the application, and need to restart it; that may explain the application pool reset.
For the cache expiration, it seems like IIS is trying to unload and reload something in a different app domain. There's no way (in .NET) to unload an assembly without unloading an app domain (I think) once it's been loaded, so this is "the usual" way to achieve this.
Maybe.

Events in Global.asax are not firing

In my ASP.Net application, I can't get the events in Global.asax to fire on my machine. For instance, Session_Start will not fire.
The same Global.asax works fine on other development machines in my office.
I have tried:
Both the ASP.Net Development Server
and my local IIS
Rebooting the machine
Removing and recreating the file from
the project (including deleting it
from disk)
All machines are configured the with
the same level of software (VS2008
SP1, .Net 3.5, XP)
This is driving me crazy, any help out there?
EDIT: Since the application works on other machines, I don't think it is directly related to the contents of Global.asax. For some reason, my machine does not process the files. I have done a repair of VS 2008 and .Net 2.0. Any other ideas?
EDIT: The application running on all three machines is being pulled directly from source control. It is definitely something specific to the one machine.
Thanks,
Pat
Application level events only need proper naming to work. Is your codebehind class being designated in your Global.asax file?
<%# Application Inherits="YourNamespace.YourApplicationClass" Language="C#" %>
This is a long shot but I have been bitten by this before. When you created your Global.asax.cs file did you make sure to include a basic Global.asax file in your project?
Also make sure that the inherits property in that Global.asax is pointing to your custom class (namespace and all) in your Global.asax.cs file.
I had this problem once and it was because I totally forgot to make the Global.asax file and hook it up to my class. I had just copied the Global.asax.cs from another project that did something similar.
becuase project is compiled first and then you have add or modify or changed location of global.asax.vb.
Solution :- Just remove debug folder. Clean project and then rebuild whole project.
Regards,
Mihir
As I write here, in my case, I was publish my site in production and I miss copy to server App_global.asax.compiled file. For this reason was not fire the Events inside Global.asax.
Hope anyelse help this tips, I lost 8 hours seeking.
If you have fiddled with ASP.NET then maybe try reinstalling it.

Resources