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

*爱你&永不变心* 提交于 2020-01-29 07:30:25

问题


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


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/13038823/how-to-run-long-lasting-process-asynchronously-under-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!