简体   繁体   English

如何在asp.net下异步运行持久的进程?

[英]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? .net 4.5,asp.net mvc:从ASP.NET应用程序运行持久进程(1-2分钟)的最佳方法是什么,它应该在单线程环境中运行,我的意思是该进程是为了一次只有一个用户,所有其他用户的执行必须等到当前执行完毕? 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. 方案如下:用户单击按钮运行某种持久计算,http响应立即返回给用户,然后用户必须手动请求单独请求的计算状态。 Asp.net http session abortion should not lead to the process termination, it should keep going. Asp.net http会话流产不应该导致流程终止,它应该继续下去。 The process might be run on the same or separate 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. 我将向您展示如何使用http://hangfire.io执行此任务 - 这是一种非常简单的方法,可以在ASP.NET应用程序中执行“即发即忘”,“延迟”和“重复”任务。 No Windows Service required. 无需Windows服务。

First, install the package through NuGet. 首先,通过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: 打开OWIN Startup类并添加以下行:

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. 如果要通知用户有关作业完成的信息,请考虑使用SignalR并相应地附加LongRunning方法。

.Net 4.5.2 adds QueueBackgroundWorkItem that you can use to schedule a task. .Net 4.5.2添加了可用于计划任务的QueueBackgroundWorkItem 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). 如果您不控制服务器(当它重新启动时),appPool关闭的90秒默认延迟将不起作用(除非您可以检测到任务未完成并在另一台服务器上运行)。 For more details see " QueueBackgroundWorkItem to reliably schedule and run background processes in ASP.NET " 有关更多详细信息,请参阅“ QueueBackgroundWorkItem以在ASP.NET中可靠地计划和运行后台进程

I would suggest using a product such as NServiceBus to offload the processing and run it in single threaded mode. 我建议使用NServiceBus等产品卸载处理并以单线程模式运行。 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. 这样做的好处是所有请求都将按顺序处理,并且可以从Web服务器卸载处理,因为您不希望在Web服务器上发生长时间运行的进程。

如果您控制服务器,并且需要更简单的完整框架(如Hangfire),您可以创建一个控制台应用程序(.exe),并做任何事情......,那么您可以使用Process.Start方法调用.exe,您可以从SQL Server调用.exe,服务等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM