简体   繁体   English

如何在ASP.NET Web.Api中运行后台任务?

[英]How do I run Background Tasks in ASP.NET Web.Api?

I'm developing an ASP.NET MVC Web Api 2 app with C# and .NET Framework 4.5.1. 我正在使用C#和.NET Framework 4.5.1开发ASP.NET MVC Web Api 2应用程序。

Sometimes, when user calls a get on an ApiController I have to run a task in background. 有时,当用户在ApiController上调用get时,我必须在后台运行任务。 The task will fill up a database table with more data. 该任务将用更多数据填充数据库表。

Searching on Internet I have found this article, How to run Background Tasks in ASP.NET . 在Internet上搜索,我发现了这篇文章, 如何在ASP.NET中运行后台任务 And I want to use HangFire . 我想使用HangFire But reading its documentation I haven't found a way to use it without using OWIN on my ASP.NET Web Api app (I'm not using it right now). 但是阅读它的文档后,我没有找到一种方法来在不使用我的ASP.NET Web Api应用程序上的OWIN的情况下使用它(我现在不使用它)。

Is there a way to use Hangfire without OWIN? 没有OWIN,有没有办法使用Hangfire? or maybe, is there another option to run background tasks on an ASP.NET Web Api 2 app? 也许还有其他选择可以在ASP.NET Web Api 2应用程序上运行后台任务?

I'm not too sure if you still need this (I doubt it) but I'll just answer this question since it was ranking high on Google Search. 我不太确定您是否仍然需要这个(我对此表示怀疑),但是我将回答这个问题,因为它在Google搜索中排名很高。 Well, the conventional way to start the Hangfire server using OWIN is as follows... 好了,使用OWIN启动Hangfire服务器的常规方法如下...

public void Configuration(IAppBuilder app){
    app.UseHangfireServer();
}

Server Lifecycle 服务器生命周期

However, based on the documentation...Hangfire is not reliant on OWIN at all and all you need to do is start and stop the BackgroundJobServer 但是,根据文档... Hangfire完全不依赖OWIN,您所需要做的就是启动和停止BackgroundJobServer

(Source) http://docs.hangfire.io/en/latest/background-processing/processing-background-jobs.html (来源) http://docs.hangfire.io/en/latest/background-processing/processing-background-jobs.html

Hangfire Server part is responsible for background job processing. Hangfire服务器部分负责后台作业处理。 The Server does not depend on ASP.NET and can be started anywhere, from a console application to Microsoft Azure Worker Role. 该服务器不依赖ASP.NET,可以在任何地方启动,从控制台应用程序到Microsoft Azure Worker Role。 Single API for all applications is exposed through the BackgroundJobServer class 通过BackgroundJobServer类公开所有应用程序的单个API

That means you would start the server when the application starts... 这意味着您将在应用程序启动时启动服务器...

var server = new BackgroundJobServer();

and you would dispose it when the application ends... 您将在应用程序结束时将其处置...

server.Dispose();

And that's exactly what app.UseHangfireServer(); 而这正是app.UseHangfireServer(); does. 确实。 See source code... 查看源代码...

https://github.com/HangfireIO/Hangfire/blob/master/src/Hangfire.Core/AppBuilderExtensions.cs#L293 https://github.com/HangfireIO/Hangfire/blob/master/src/Hangfire.Core/AppBuilderExtensions.cs#L293

and

https://github.com/HangfireIO/Hangfire/blob/master/src/Hangfire.Core/AppBuilderExtensions.cs#L311 https://github.com/HangfireIO/Hangfire/blob/master/src/Hangfire.Core/AppBuilderExtensions.cs#L311

See example below using a global.asax.. 请参阅以下使用global.asax的示例。

    BackgroundJobServer _server;

    protected void Application_Start(object sender, EventArgs e)
    {
        GlobalConfiguration.Configuration
            .UseSqlServerStorage("YOUR_CONNECTION_STRING");

        _server = new BackgroundJobServer();
    }

    protected void Application_End(object sender, EventArgs e)
    {
        _server.Dispose();
    }

SQL Server Storage SQL Server存储

To register the SQL Server Storage you don't need OWIN, this is all you need to do... 要注册不需要OWIN的SQL Server存储,这就是您需要做的...

Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage("YOUR_CONNECTION_STRING");

Again, I came to that conclusion by looking at the source code here... 同样,通过查看此处的源代码,我得出了这个结论。

https://github.com/HangfireIO/Hangfire/blob/master/src/Hangfire.SqlServer/SqlServerStorageExtensions.cs https://github.com/HangfireIO/Hangfire/blob/master/src/Hangfire.SqlServer/SqlServerStorageExtensions.cs

Conclusion 结论

Hangfire does not rely on OWIN and they have acknowledged so Hangfire不依赖OWIN,他们已经承认

您可以使用ThreadPool.QueueUserWorkItem来委派您的进程,它将由CLR使用线程进行管理。

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

相关问题 如何在ASP.NET Web API中排队后台任务 - How to queue background tasks in ASP.NET Web API ASP.NET Core Web.API中的拦截调用者语言 - Intercept caller language in ASP.NET Core Web.API 带有WEB.API和Castle Windsor容器的ASP.NET MVC - ASP.NET MVC with WEB.API and Castle Windsor container 使用 asp.net web.api json 不起作用 - Consuming asp.net web.api json not working 我可以在Asp.net Web.API中的AppStart一次自定义RequestTelemetry属性吗? - Can I customize RequestTelemetry properties once at AppStart in Asp.net Web.API 如何在AuthorAttribute的ASP.NET Web.API MVC 5的IsAuthorized上获取Post参数 - How to get Post parameters on IsAuthorized of AuthorizeAttribute ASP.NET Web.API MVC 5 ASP.NET Web.api如何处理名称以GET开头的两个方法? - How does ASP.NET Web.api handle two methods with names starting with GET? 使用Asp.net MVC web.api为什么要重定向而不是只调用其他函数? - Using Asp.net MVC web.api why should I redirect rather than just call other functions? WEB API + ASP.NET尝试以json格式显示来自WEB.API的数据 - WEB API + ASP.NET trying to display data from WEB.API in json format ASP.NET Core 2.1 Web.API更改应用程序见解的日志记录检测键 - ASP.NET Core 2.1 Web.API change application insights instrumentation key for logging
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM