[英]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();
}
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();
}
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
Hangfire does not rely on OWIN and they have acknowledged so Hangfire不依赖OWIN,他们已经承认
您可以使用ThreadPool.QueueUserWorkItem来委派您的进程,它将由CLR使用线程进行管理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.