Intermittant exception in Application_Start - how do I un-start the application? - asp.net

In our Application_Start event handler we're performing some actions that intermittently fail due to file locking issues. In this scenario we would like to return the application to an "un-started" state.
By this I mean that the user will be shown an error page, and then the next time a user hits the site the Application_Start event will be fired again.
We're using ASP.NET 3.5, WebForms and MVC.

AppDomain.Unload offers what you're looking for, but I wouldn't recommend it.
There are lots of catches; tearing down an app domain programmatically is not without its own set of issues (i.e. if a thread's blocked in native code you may see a CannotUnloadAppDomainException) and is generally a poor design, IMO.
What you're attempting to do is highly unconventional; I would reconsider the approach all together. If you just need to execute some code once at the app domain level, there are lots of better ways to do it, like statics for instance or a flag in the HttpRuntime cache. Just mind the web-garden and concurrency scenarios.
Good luck.

I'm pretty sure that there is no way to quit/force stop an webForms application from within the application. However, an ungentle way to prevent the application from starting is to generate an unhandled error within your application_start. This will prevent start up from happening and upon the next hit to your webForms app, the application_start will try again.

Related

How can I get more detail about ASP.NET app restarts than HostingEnvironment.ShutdownReason?

I'm troubleshooting restarts in an ASP.NET application. The application is restarting about 20 times a day. I'm logging using the log4net library. Following advice from this question, I have the application logging the reason for the shutdown from HostingEnviornment.ShowdownReason inside the Application_End() event in Global.asax. I also log when the application starts up in the Application_Start() event.
After running the logs for several days, I've found that when a shutdown is actually logged, the reason for the restart is always HostingEnvironment. But the shutdown is not always logged. I know that because I can see Application_Start() log events that don't have a corresponding Application_End() event.
What would cause the hosting environment to restart my application? Is there a way to get more specific information about why it decided to trigger the restart? Does the fact that the shutdown event is not always logged shed any light on what may be causing the restarts?
UPDATE
I'm still struggling with this. Links from around the web seem to indicate that using WinDbg/SOS.dll/ADPlus.vbs will be necessary to track down the cause of this issue. But between learning how to use these tools and figuring out how to correctly interpret the results, it seems to come with a very steep learning curve. I'm kind of still hoping there is something out there that can help me track down the cause of the restarts without resorting to disassembling CLR code. Does anyone have any other tools or tricks that can help me with this?

Performance issues with TransferRequestHandler and BeginRequest

I have started to use New Relic to monitor the performance of http://alternativeto.net that is a fairly large website.
What I've noticed is that a significant time is spent in a method they report as "TransferRequestHandler" and when i dive into it i see that it's really the "BeginRequest()" method that is taking time.
It looks like this in New Relic.
The closest thing I've come to find anything that could be the problem is this thread here on Stack Overflow I just discovered why all ASP.Net websites are slow, and I am trying to work out what to do about it but i've actually tried to replace the Session Module but that didn't help.
The site is a hybrid between ASP.NET MVC and Webforms.
I've realized that this is a long shot and you don't have much to "go on" but if someone can put me in the right direction and most importantly be able to reproduce the behavior locally or something like that i would be extremely grateful :)
The BeginRequest is the place that everything starts, so its normally there to be the delay but you must go deeper to find the actually point of your code that makes the delay.
If the session is the issue, then disable the session when the user make long actions, like download a file, or complicate procedures that the page stuck for long time.
relative to session:
call aspx page to return an image randomly slow
ASP.NET Server does not process pages asynchronously
Trying to make Web Method Asynchronous
Web app blocked while processing another web app on sharing same session  
What perfmon counters are useful for identifying ASP.NET bottlenecks?  
Replacing ASP.Net's session entirely  
The next step is to make a totally custom session.
Now, its may help if you use more than one pool (web garden) to run your site, but before do that you must be sure that you have correct synchronize your data and use Mutex and other locking mechanism for run on multi pool environment.

How can a newly added global.asax file make a mess of my WCF service

I have a silverlight app that uses WCF both by polling Duplex and "normal" async calls. everything was working fine, until I added a global.asax file.
After an hour of googling, I came across this rather dated blog by Jean-Dirk Stuart that suggested commenting out the stubbed Session_start event.
Sure enough that corrected the problem, but it raises a concern. Why would this seemly benign member of the global.asax file break the wcf calls.
Yeah, this is a little known but annoying issue. The problem comes down to session state being enabled once you add a global.asax file to your web project. Once session state is enabled, the server will only execute the calls sequentially. Here are two articles with some more in-depth information:
http://blogs.msdn.com/b/silverlightws/archive/2009/09/30/having-a-pollingduplex-service-and-any-other-wcf-service-in-the-same-website-causes-silverlight-calls-to-be-slow.aspx
http://weblogs.asp.net/olakarlsson/archive/2010/05/20/simultaneously-calling-multiple-methods-on-a-wcf-service-from-silverlight.aspx
This behaviour only occurs when using the browser networking stack, so your options are:
Disable session state in your web project, or
Use the client networking stack
Hope this helps...
Chris
There are some types of binding which support accessing asp.net session data, I really dont see how it would affect unless WCF tries to hook into the session_start event just like a http module does. You could also use reflector to disassemble the code where you where getting the exception.

Is there an event that raises after a View/PartialView executes in ASP.NET MVC 2 RC2?

I have the following problem:
We have an ASP.NET MVC 2 RC 2 application that programmatically impersonates an AD Account that the user specifies at logon. This account is used to access the DB. At first we had the impersonating code in the begin_request and we were undoing the impersonation at the end_request, but when we tried to use IIS 7.5 in integrated mode, we learned that it's not possible to impersonate in the Global.asax so we tried different things.
We have succesfully moved our code from the BeginRequest to the ActionExecuting event and the EndRequest to the ResultExecuted, and now, about 80% of our code works.
We've just discovered that since we're passing the Entity Framework objects as models for our views, there's this remaining 20% that won't work because some Navigation Properties are not loaded when the view begins it's execution, so we're getting connection exceptions from Sql Server.
Is there any event or method that executes AFTER the view, so we can undo the impersonation in it? We thought ResultExecuted will do just that, but it doesn't.
We've been told that passing the plain Entities into the view as models is not a good idea, but we have A LOT of views that may have this problem and there's not automated way to know it. If some of you could explain why it's not a good idea, maybe we can convince the team to fix it!

Why Application_Start fire multiple times in Global.asax?

I have a Silverlight application hosted in an ASP.NET page. I need to do some processing when the application first starts up and start up some background processes (various periodic checks).
I thought that the Global.asax Application_Start event would be a good place to do this, but I find that the Application_Start fires multiple times which I didn't expect. From what I've read it seems that when the last user logs out of my application their session disappears and IIS unloads my application. When it's next requested it gets loaded again and the Application_Start runs again, which is not really what I want.
Is this the expected behaviour? Is there any way to keep the application loaded and not have it restart like this?
Secondly, I have these periodic background processes that I want to run. Maybe a Windows Service would be a better place for them, but having a timer run from within a static class in my application is convenient. Is there a way I can keep these running even though there are no active users?
I think you are trying to achieve a behaviour which just doesn't fit the web server model well. Many CMSs try to perform periodic tasks etc. by having some user web requests initiate the work, but I have never seen it done with much success.
If you aren't restricted by deployment issues, access rights etc., I would recommend going with the Windows Service approach. Just make sure to incorporate it in your build/deployment process so that that won't become a hazzle.

Resources