Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run long-lasting process asynchronously under asp.net?

.net 4.5, asp.net mvc: What is the best way to run long-lasting process (1-2 minutes) from ASP.NET application giving it should be run in a single-threaded environment, I mean the process is initiated for one user at a time only, executions for all other users have to wait till the current execution is done? The scenario is the following: user clicks button that run some sort of long-lasting calculations, http response returned to the user immediately, then user has to request status of the calculations with separate request manually. Asp.net http session abortion should not lead to the process termination, it should keep going. The process might be run on the same or separate server.

like image 313
YMC Avatar asked Oct 23 '12 20:10

YMC


People also ask

Is ASP NET asynchronous?

The . NET Framework 4 introduced an asynchronous programming concept referred to as a Task and ASP.NET 4.5 supports Task. Tasks are represented by the Task type and related types in the System.

What is synchronization and Asynchronization in asp net?

In synchronous operations tasks are performed one at a time and only when one is completed, the following is unblocked. In other words, you need to wait for a task to finish to move to the next one. In asynchronous operations, on the other hand, you can move to another task before the previous one finishes.


3 Answers

I'll show you how to perform this task with http://hangfire.io – incredibly easy way to perform fire-and-forget, delayed and recurring tasks inside ASP.NET applications. No Windows Service required.

First, install the package through NuGet. If you have any problems, please see the Quick Start guide in the official documentation.

PM> Install-Package Hangfire

Open your OWIN Startup class and add the following lines:

public void Configure(IAppBuilder app)
{
    GlobalConfiguration.Configuration.UseSqlServerStorage("connection_string");

    app.UseHangfireDashboard();
    app.UseHangfireServer();
}

Then write the method that will do the long-running work (I applied attribute to perform only one method at a time):

[DisableConcurrentExecution]
public void LongRunning()
{
    // Some processing stuff
}

And then call a method in background as fire-and-forget to respond user immediately:

public ActionResult Perform()
{
    BackgroundJob.Enqueue(() => LongRunning());
    return View();
}

If you want to notify a user about job completion, consider using SignalR and append the LongRunning method correspondingly.

like image 89
odinserj Avatar answered Oct 05 '22 22:10

odinserj


.Net 4.5.2 adds QueueBackgroundWorkItem that you can use to schedule a task. If you don't control the server (when it's rebooted), the 90 second default delay of appPool shut down won't work (unless you can detect the task didn't complete and run it on another server). For more details see "QueueBackgroundWorkItem to reliably schedule and run background processes in ASP.NET"

like image 31
RickAndMSFT Avatar answered Oct 06 '22 00:10

RickAndMSFT


I would suggest using a product such as NServiceBus to offload the processing and run it in single threaded mode. The advantage to this is that all requests will be processed in order and the processing can be offloaded from the web server as you don't really want long running processes to happen on a web server.

like image 42
Trevor Pilley Avatar answered Oct 06 '22 00:10

Trevor Pilley