C# MVC IIS long running requests - c#

I have an C# MVC app. And one of the calls I know will take like 12 hours, I'm generating some reports.
I want to know if the IIS will keep the process running this long.
If I'll do it async will it run and somehow put it away and let him run for this long?

In general, this is a very poor practice. Your app pool may be recycled for many reasons outside of your control.
You would greatly benefit from processing this in an external process and then providing the user the results via IIS.
However, starting .net 4.5.2 (https://blogs.msdn.microsoft.com/dotnet/2014/05/05/announcing-the-net-framework-4-5-2/) you can use the HostingEnvironment.QueueBackgroundWorkItem API.
The idea behind this is that the IIS will try to finish this work in case of a graceful shutdown of the app pool.

I'm assuming the report must be using something like SSRS. Why don't you create a batch job at the backend to run the reports at a specified time. Update a table with the status of the report and just poll the status at the front end. when Ready just download it. Imagine if your report had been running for 6 hours and it's reliant on the website being up. If someone re-starts the website that's 6 hours of processing gone.

Related

Run a function / process as background in C# asp.net mvc

I'm new to this but I have a question: what is the best way, to run a function / process as background in C# ASP.NET MVC ?
For context, I'm creating a website for a project that does monitoring through snmp. The thing is that the various alerts have different timeouts between them. I want to go through all of alerts and see if the time between the last check and now is already passed. If so, the program will do another monitoring for that alert. It is supposed to run as background so it doesn't affect the rest of the site.
Can someone help me?
Thanks in advance ^^
Check this great article here: https://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx
TLDR: you have built in and third party options.
The built in is not as polished and with as many offerings as for example hangfire
The built in tries to delay the app pool recycle to finish the job.
Hangfire has good connectivity with redis and other persistence options
Both are not suitable for long running jobs, especially longer than the application pool restart period.
Both need some recovery mechanism in case the task is interrupted abruptly.
You can build an application running on a scheduler or a windows service or a linux cron job or even try something on the cloud as web jobs. Those are not related to the iis lifecycles and have none of the drawbacks mentioned above.

Prevent Changes during long running job

Our Web Application uses an .net-core web api running on a loab balancer and an angular client. We access the DB using EF core.
We have a long running background-task that does a great amount of calculation and takes about 2-3 hours to do so, but will only be initiated by administrators of the application 3-4 times a year.
While the job is running we want to prevent users from adding/editing/deleting data and our client told us its even fine if the application is not avaliable for the duration as they will mostly do it overnight.
The easiest way to do this is to redirect users to an informationpage while the job is running but I have found no way of actually getting to the information if the task is running or not.
I could set a flag whether the job is running or not and just check that flag at every request but I found no way to access an applicationwide state.
I cannot save a flag to the DB because while the transaction is commiting at the end of the job (~1 hour) we cannot read from the DB
What baffles me most is that I have not found a single article or question about a problem like that which doesn't seem to be too outlandish to me, so I guess I'm missing something very obvious.
The simplest way is to store the value for your "Maintenance Mode" in a Singleton class on the server. (No database call needed). The value will remain there for as long as the server is actively running.
If distributed cache (as already mentioned) is not an option, you can run long running task in (uniquely) named transaction and then the check list of active transactions to determine if task is still running.
This is completely dependent on your setup but a simple way to approach this problem might be to make it the long-running job's responsibility to divert traffic from your site while it is running, and then undo that once it is finished.
As an example, if you were running this with an old-school .NET site in IIS the job could drop an app_offline.htm file into the site folder, run, then delete it again. Your setup is different, but if you could do something similar with your load-balancer (configure it to serve some static file instead of routing the requests to your servers) then it could work for you.

Is it possible to have an ASP.NET application perform a timed action? [duplicate]

Once a day, I want my ASP.NET MVC4 website, which may be running on multiple servers, to email a report to me. This seems like a pretty common thing to want to do, but I'm having a tough time coming up with a good way to do it.
Trying to run a timer on a server to do this work is problematic for a couple of reasons. If I have multiple servers then I'd have the timer running on all of them (in case a server goes down); I'd need to coordinate between them, which gets complicated. Also, trying to access the database via Entity Framework from a background thread adds the complication that I must employ a locking strategy to serialize construction/disposal of the DbContext object between the periodic background thread and the "foreground" Controller thread.
Another option would be to run a timer on the servers, but to have the timer thread perform a GET to a magic page that generates and emails the report. This solves the DbContext problem because the database accesses happen in a normal Controller action, serialized with all of the other Controller accesses to the database. But I'm still stuck with the problem of having potentially more than one timer running, so I'd need some smarts in the Controller action to ignore redundant report requests.
Any suggestions? How is this sort of thing normally done?
You should not be doing this task from your web application as Phil Haack nicely explains it in his blog post.
How is this sort of thing normally done?
You could perform this task from a Windows Service or even a console application that is scheduled to run at regular intervals using the Windows Scheduler.
The proper solution is to create a background service that runs independently of your website. However, if that is not an option there is a hack where you can use the cache as explained in Easy Background Tasks in ASP.NET by Jeff Atwood.
A few options:
If you are hosting on Azure as a Website, check out WebJobs which was released recently in preview (http://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/)
If you don't want the pain of extracting out your email logic outside of the website, expose that functionality at a url (with a handler, mvc action, etc.) and then run a Windows Scheduled task that hits that url on a schedule.
Write a simple console app that is executed similarly via a Windows Scheduled task.
Or write a simple Windows Service that internally is looping and checking the time and when reached, hits that url, runs that exe, or has it's own code to send you the email.
I would recommend running Quartz.NET as a Windows Service:
Quartz.NET - Enterprise Job Scheduler for .NET Platform
There's boilerplate code for a Windows Service in the download.

Scheduling in Asp.net.What is best solution?

I want to Scheduling in Asp.net
I have following options to implement this
To write SQLServer JOB(I dont want to do this.Dont want to go outside of .Net environment)
Second option is I will write windows service and this window service will call asp.net
webservice then this webservice calls asp.net method
(I also dont need to do this because my hosting provider might not be allow me to install
window service)
Third option is I call my scheduling method in Application_Start event in global class
(Drawback is, webserver will kill thread any time )
To call Scheduling Code in Page_Load event of Home Page(Might be nobody visits my website for hours
,Also page execution might be slow due to scheduling code)
I also found some online services that calls your page at given interval,some are given below
http://www.cronservice.co.uk/new/
http://scheduler.codeeffects.com
Anybody give me bettor solution of this and also explain why it is bettor?
Thanks in Advance
The ASP.NET application isn't the right place to implement scheduling. I would suggest creating a service or a scheduled task that runs in short intervals.
You don't have many options in a shared hosting environment. My host (WinHost) allows remote access to their database, so I was able to create an executable that ran on a local server with Task Scheduler.
The performance isn't great since the database is accessed over the internet, but it's still better than attempting to run pseudo scheduled tasks with ASP.NET.
Some hosts also offer a service that will request a url within your site on a scheduled basis. However, this didn't work for me because the task I had to run took longer than the request timeout.
There is no one solution that fits all. SQL jobs and windows jobs (scheduled thru windows task scheduler) are very widely used. In one of my previous work places they had jobs that ran on multiple platforms (mainframe,windows,sql server). Failure in some of these jobs, would cost in thousands by the day. So they employed something called ESP. This software monitored jobs on all platforms and sent a message to the control room in case of a failure.
If you throw some more light on the requirement, we might be able to help you better.
ASP.NET is not the right place to house your Scheduled Tasks. I'm using Quartz.net when I have to create Scheduled Tasks.
Create a page that launches your task and place it at the URL http://www.mydomain.com/runtask.
Create a scheduled task on your home PC that sends a request to http://www.mydomain.com/runtask.
You'll need to keep your home PC on all the time.
Ideally I would go with number 1 as you get full control/history/error reporting etc. You can write an SSIS job in .NET and have SQL server schedule it.
However, I have had a similar problem with shared hosting that is very restrictive. What I did was create a page which runs the process on page load (using validation in the querystring for security). I then used a machine I have which is always on to schedule a Windows Task Scheduler (this is part of Windows as standard) to call a bit of VB script that opens the browser and then shuts it.

Automated Tasks for a c# .net website

So, this question has been asked lots, and i have seen many different answers, but nothing finite or absolute for my scenario.
_
What I want to do:
We have a website, with a community of users.
In the admin section of the website, there are buttons to run the following functions:
Email all the users our weekly newsletter [Thursday 4pm],
message users that day's information [Daily 6pm],
post to facebook through facebook connect [Daily 8am and 6pm],
etc, etc. (There will be new requirements coming soon too, but they will follow the same principal)
_
All I want to do is to run these functions automatically, so a member of staff does not need to go to the website, login, then click each of the buttons at the set times.
That is, effectively, have the server click these buttons automatically, at the set times mentioned above.
_
I have seen suggestions for building a service with a timer built in, which will call each function, or use windows task scheduler, build an exe / com etc, but i get the gut feeling it should not be this complicated.
We have the code already written to actually do the tasks in a .net web page, and using some custom built classes etc.
I just do not know how to automatically call these functions at the desired times.
_
Server Info:
Dedicated server running Windows Web Server 2008 R2 (64bit)
Development Enviro:
Visual Studio 2010 SP1, using .NET 4.0
_
Thank you in advance for your help.
Kindest Regards,
Del
Splitting out the code into separate exes/dlls that can be called from the Windows task scheduler as well as your application is the way to go.
There's no sense in re-inventing a scheduler inside your application when one exists in the operating system you are running on. Particularly as your application is a web application which should be doing web things not server things.
The added advantage is that you have made your application more modular and easier to upgrade.
By definition, web servers respond to requests. This fundamental design manifests in all kinds of ways, such as application pools being shut down after a period of inactivity. For this reason, it is not a good idea to execute time-dependent code inside the context of a web server, because it is difficult to guarantee that the code will actually run (without jumping through a lot of hoops that have other negative side effects). Instead, an external time-dependent mechanism should send a request to the web server to execute these functions.
You said that you didn't want to use Windows Scheduler or write an external exe because "it shouldn't be this complicated"; but I don't see what's complicated about having a scheduled task call a web service.
Well, though I agree with other answers cheering for windows task scheduler and that website has nothing to do with scheduling tasks (it can be the source of input parameters, not the task runner itself), you could try using scheduling library like quartz.NET to schedule and run your necessary processes in the background.
Download cURL - a command-line HTTP request utility. Use this utility from task scheduler to call your web page / web service with the specific query string required to execute your functions.
http://curl.haxx.se/
This isnt very secure - anyone that knew the correct query string could cause these functions to execute - but it gives you the general idea. In my opinion its much safer to write a console app that uses your assemblies to directly execute your functionality. Call the console app from task scheduler.

Categories

Resources