简体   繁体   English

在ASP.net Web API 2服务中处理长时间运行的进程

[英]Handling Long-Running Processes in an ASP.net Web API 2 Service

I created a REST service using ASP.net Web API 2. The service allows a client to POST records which are stored in a SQL Server database. 我使用ASP.net Web API 2创建了一个REST服务。该服务允许客户端对存储在SQL Server数据库中的记录进行POST。 At this point some other process needs to pull the records from the database, process them, and send the results to another, separate REST service. 此时, 其他一些过程需要从数据库中提取记录,进行处理,然后将结果发送到另一个单独的REST服务。

Since the processing of the records will be time-consuming, I don't want to perform this step as part of the POST action, but I'm trying to figure out the best approach to designing this other process . 由于记录的处理将很耗时,因此我不想在POST操作中执行此步骤,但是我试图找出设计此其他过程的最佳方法。

Is there a way to integrate this functionality into the ASP.net REST API project, or is something like a Windows Service the best solution? 有没有办法将此功能集成到ASP.net REST API项目中,还是像Windows Service这样的最佳解决方案?

You could write your web service asynchronously. 您可以异步编写Web服务。 So that, 以便,

public async Task<HttpResponseMessage> PostRecords(Records records)
{
    await [POST data to db]

    // process records asynchronously without awaiting

}

You can also perform the post operation synchronously and at the same time track the completion of the post in your database. 您还可以同步执行发布操作,同时跟踪数据库中发布的完成情况。 And as you mentioned, have a job or windows service process the completed posts separately. 正如您提到的,让工作或Windows服务单独处理完成的帖子。

You can use Quartz.NET scheduling library within the asp.net mvc or web api. 您可以在asp.net mvc或Web API中使用Quartz.NET调度库。 Define a job which executes your query and fire the job from your post action. 定义一个执行查询的作业,并从发布操作中触发该作业。

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

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