I have an MVC3 razor page that essentially does this
[HttpPost]
public ActionResult Index(Mymodel model)
{
//run background process
return(model);
}
public backgroundprocesscompleted()
{
//write to database
}
The run background process part runs an analysis via a library then once it is done runs backgroundprocesscompleted. This whole thing works for small files (46 rows) but when I do it on bigger files (11k) it runs on IIS and appears to time out. I have changed numerous settings but still it times out. Essentially what I would like to do is force the web application to never timeout. Any good ideas on how to fix this? Thanks
There are several reasons why a longer-running background process in ASP.Net might fail, including
An unhandled exception in a thread not associated with a request will take down the process.
The AppDomain your site runs in can go down for a number of reasons and take down your background task with it.
When you modify web.config, ASP.NET will recycle the AppDomain
IIS will itself recycle the entire w3wp.exe process every 29 hours (by default)
In a shared hosting environment, many web servers are configured to tear down the application pools after some period of inactivity
http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx
The article linked above has some suggestions for mitigating those risks. However, I would recommend doing long-running processing entirely outside of ASP.Net. One straightforward approach is to write the work into a transactional MSMQ Queue, and have a Windows Service read work items off of that queue and process them.
Related
I have a web application that do some work on database every specific time, so how can I keep it doing its work even I close the web browser?
Is using a Thread useful and will it work for me? What other available solutions?
You don't want to do that. A regular web server process isn't a background worker that runs forever, it replies on requests and it keeps some state. You don't want to let your thread end because of the application pool getting recycled, sessions time out, etc.
You have a few options, which you could use:
A Windows service (the most logical thing to do);
The new .NET 4.5.2 HostingEnvironment.QueueBackgroundWorkItem (which separates itself from the user session context. Note: for short-lived background tasks only! Since 'The AppDomain shutdown can only be delayed 90 seconds');
Windows Azure Web Jobs (for in hosted environments);
Task Scheduler.
The Problem
I have an ASP.NET web application connecting to an SQL server database.
Occasionally, the application hangs when some specific users try to log in. I can replicate this when I try to log in as that user on the live site from my machine, but the problem doesn't appear in an identical site (the same code, the same machine, the same database and user account - only the worker process and application pool are different).
The problem also disappears (at least temporarily) on an application pool recycle. When it happens again it tends to be the same users.
Normally I'd assume this was a problem in the application-specific code or database, and I'm still looking for a way that that's possible, but the fact it doesn't happen in an identical site running against the same database makes me doubt it.
More information
From some logging, the hang seems to happen after the Page_Load of the requested page, but before the Page_Load of the user controls on that page.
The hanging requests can be seen in the current requests of the site's worker process in IIS, the state shows as ExecuteRequestHandler and the module is the IsapiModule.
The SQL server's activity monitor shows no long running queries during the time that the application is hanging, the profiler shows no frequent identical queries coming from the application during the hang.
Most users can continue to use the application without problems.
The application stores user details with the in process ASP.NET session state.
On some occasions the problem has disappeared on its own after 10-20 minutes, other times it persists until an application pool recycle.
Occasionally the logging shows that after ~5 minutes of hanging after the requested page's Page_Load, the Page_Load of the first child user control is called, after which there is another hang.
Summary
I'm basically wondering if there is any chance that this might be due to a problem with ASP.NET session locking (are there any known bugs, or dangers of deadlock on session?)
Could it be a problem with the request's thread being assigned a low priority for processor time?
If anybody has any ideas about what this could be I'd be interested, if not, then might debugging a memory dump of the worker process help me? I don't have any experience of or know anything about how safe it is to take a memory dump of a live, running worker process.
I am working on an OLAP application, WCF + Silverlight clients (up to 100 concurrent users). Unfortunately from time to time, a specific service call goes crazy (although it is perfectly valid, just too complex) and occasionally (once a month) brings the whole server down (by consuming all CPU).
A solution would involve killing user request or even the whole user session which is not a big deal for us from the business perspective - recovering/restarting the whole application is.
The idea of isolating user sessions into separate processes is very tempting: CPU/memory throttling and clean resource disposal (not like Thread.Abort) - if modern browsers can do this just for web pages, maybe it's time to do this on servers. We just want to evaluate this concept and see pros and cons in our particular scenario.
Hence the questions:
Is there already an existing library/framework which will be useful for managing processes (like pre-spawning/reusing processes, throttling, kill after timeout)?
Are there any "best practices" or guidelines how to create such architecture?
I was having same problem with my WCF services they too serve more than 100 clients..
and problem which i discovered using IIS logs (C:\Windows\System32\LogFiles\HTTPERR)
I found my problem in Application Pool Recycle timeout on IIS setting.
Application pool was getting restarted every 48 hours and which was causing issues with already subscribed clients.
So i would suggest
1. Analyze the http error logs and IIS logs which will give more information about all the application pools status if any gets shutdown or recycled.
2. If application pool crashes then Setup for Windbg and attach the process set the correct source file path. It will tell you the location if any exceptions are occurring.
Intro
I am developing a web application using ASP.NET MVC 3, C#, targeting IIS 7.0+. We have a number of long-running async requests (utilizing AsyncController/Async action features). I also use HttpRuntime.Cache quite often (and in some non-standard way which doesn't really matter here).
Question
Can IIS application pool be suddenly recycled if there are active long-running requests present? I want to prevent that behavior if possible as I don't want to lose data from cache.
Do I really need to use some persistent storage (i.e. Database) to overcome possible issues?
Normally IIS won't recycle an application which has pending requests for it due to period of inactivity. But IIS could recycle your application if you hit memory or CPU tresholds. But this is something that you could configure in your IIS management console.
When a recycle is triggered there is a "shutdown time limit" that defaults to 90 seconds. That's how long processes have to finish before they will be shutdown forcibly. This is configurable through IIS, you likely want to increase this to a large value in addition to removing non-desired recycle triggers (memory, CPU, time, schedule, requests, etc.)
I have a website hosted under IIS 7 on Window 2008 x64. IIS is running in 64 bit mode and the site has its own Application Pool 64 bit etc. The website appears to run fine most of the time and then all of a sudden each hour it freezes the users request. They don't get a timeout message, it just hangs and appears to wait for about 2-3 minutes before returning the page.
I have monitored the Worker Process on that application pool during and see the processor is at a very steady 25%. Memory is fine and not increasing in any scary way.
I have setup Failed Request Tracing to show me every issue where a request takes more than 30 seconds and yes it records it but with no errors.
Other websites in different application pools on the same server are working fine during the outage.
Any suggestion of how I might debug this issue?
Do you have IIS set to recycle worker processes on that application pool on a given schedule? You indicate you monitored it, but you didn't indicate whether or not you found it to be recycling excessively, just that the memory allocated wasn't increasing in an untoward way.
Do the IIS logs show anything unusual during the time period? Try an app like Fiddler to help debug requests to the web server.
Turn out we were using a control called i-Load to resize images. It has a function to delete temp files after 3 hours. This was locking the IO and causing out entire web-app to halt. Switch it off and all work fine now. Hope this helps someone.
Does the Application depend on a Database that has some Job running every hour?
In case the DB is under heavy load, it would take longer for the queries to execute on your DB and therefor take longer for your web-app to process the pages.
Yes IO process can block the application pool pending thread once not completed other one. So create another thread of IO process and proper handle the cancel token source.