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

守給你的承諾、 提交于 2019-11-30 20:08:46

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.

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.

RickAndMSFT

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

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.

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