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

4

7

.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.

Lustrous answered 23/10, 2012 at 20:34 Comment(2)
Can you add a service on the computer?Rysler
I can, but I wonder how to provide consecutive execution for every web app thread?Lustrous
A
3

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.

Amidst answered 23/10, 2012 at 20:44 Comment(3)
We are actually considering this options. There are advantages like you mentioned, but there are downsides as well like necessary infrastructure setup: MSMQ, message queues creation, separate configurationLustrous
NServiceBus will handle a lot of that for you (installation and configuration of MSMQ & DTC) it's designed to solve the problem you have mentioned.Amidst
I like NSBus, probably it's the best solution for us, but what I meant is that there will be some additional administrative costs for this solution like this: 1)MSMQ, RavenDb, and maybe something else need to be installed, I remember we had to download & setup them separately, they were not part of NSBus package, 2) Somebody need to check failed message queue periodically, additionally to our regular log file (two app tracking points instead of one) 3) The separate process means it suppose to have its own configuration (two config files to manage instead of one)Lustrous
O
12

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.

Osset answered 9/6, 2014 at 7:31 Comment(1)
any full sample fo notify using SignalR ?Subterfuge
S
5

.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"

Statistician answered 6/6, 2014 at 4:45 Comment(0)
A
3

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.

Amidst answered 23/10, 2012 at 20:44 Comment(3)
We are actually considering this options. There are advantages like you mentioned, but there are downsides as well like necessary infrastructure setup: MSMQ, message queues creation, separate configurationLustrous
NServiceBus will handle a lot of that for you (installation and configuration of MSMQ & DTC) it's designed to solve the problem you have mentioned.Amidst
I like NSBus, probably it's the best solution for us, but what I meant is that there will be some additional administrative costs for this solution like this: 1)MSMQ, RavenDb, and maybe something else need to be installed, I remember we had to download & setup them separately, they were not part of NSBus package, 2) Somebody need to check failed message queue periodically, additionally to our regular log file (two app tracking points instead of one) 3) The separate process means it suppose to have its own configuration (two config files to manage instead of one)Lustrous
B
0

If you control the server, and need more simplicity that a full framework like Hangfire, you can make a console app (.exe), and make any thing..., then you can call the .exe with Process.Start Method, you can call the .exe from SQL Server to, service, etc.

Byng answered 12/11, 2018 at 22:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.