Pub*_*ubu 7 c# rest multithreading windows-services microservices
我在.net 4.6.2中有一个宁静的微服务(web api),并且每次调用某些端点来执行一些数据库清理工作后,我都想调用即发即弃功能。我不想使用Task.Run,并且我想拥有一个非常可靠的专用流程来完成此工作。
我发现了很多有关如何在.net核心中使用IhostedService进行长期运行的后台任务的文章和讨论。
https://thinkrethink.net/2018/02/21/asp-net-core-background-processing/
https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-2.1
但是,我没有找到太多有关.net框架中等效内容的信息。那是IRegisteredObject吗?有人可以帮助我还是将我引导到正确的资源。
注意:我们将API托管为Windows服务,并且不使用IIS
这是我第一次在这里提出问题,因此如果问题不够充分,我们深表歉意。
Ala*_*rda 10
ASP.NET 有一个用于运行后台任务的本机解决方案:HostingEnvironment.QueueBackgroundWorkItem
方法,可在System.Web.Hosting
.
它适用于同步或async
方法。与使用 a 不同ThreadPool
,QueueBackgroundWorkItem
它确保 ASP.NET 运行时将延迟应用程序关闭,以防有任何挂起的工作项正在运行。
来自官方文档:
与普通 ThreadPool 工作项的不同之处在于,ASP.NET 可以跟踪通过此 API 注册的工作项当前正在运行的数量,并且 ASP.NET 运行时将尝试延迟 AppDomain 关闭,直到这些工作项执行完毕。无法在 ASP.NET 管理的 AppDomain 之外调用此 API。当应用程序关闭时,将发出所提供的 CancellationToken 信号。
以下是如何使用它的示例:
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Hosting;
using System.Web.Http;
namespace BackgroundWorkItemExample.Controllers
{
public class HomeController : ApiController
{
[HttpPost]
public IHttpActionResult Execute()
{
Debug.WriteLine("Doing something.");
HostingEnvironment.QueueBackgroundWorkItem(ct => FireAndForgetMethodAsync());
HostingEnvironment.QueueBackgroundWorkItem(ct => FireAndForgetMethod());
HostingEnvironment.QueueBackgroundWorkItem(ct => FaultyFireAndForgetMethod());
Debug.WriteLine("I don't care for that stuff running in the background. I'm out.");
return Ok("It still returns, regardless of exceptions.");
}
private async Task FireAndForgetMethodAsync()
{
Debug.WriteLine("Started running an asynchronous background work item...");
await Task.Delay(5000); // Pretend we are doing something for 5s
Debug.WriteLine("Finished running that.");
}
private void FireAndForgetMethod()
{
Debug.WriteLine("Started running a background work item...");
Thread.Sleep(5000); // Pretend we are doing something for 5s
Debug.WriteLine("Finished running that.");
}
private void FaultyFireAndForgetMethod()
{
throw new Exception("I'm failing just to make a point.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出将是:
Doing something.
I don't care for that stuff running in the background. I'm out.
Started running a background work item...
Started running an asynchronous background work item...
Exception thrown: 'System.Exception' in WebApplication1.dll
An exception of type 'System.Exception' occurred in WebApplication1.dll but was not handled in user code
I'm failing just to make a point.
Finished running that.
Finished running that.
Run Code Online (Sandbox Code Playgroud)
最安全的方法之一是使用线程。您可以触发线程并让它运行。
如果您需要在应用程序关闭时杀死线程,您可以添加 isBackground 参数。
否则,线程应继续运行,直到其工作完成
Thread backgroundThread = new Thread(() =>
{
Console.WriteLine("I'll run until the app Stops or I finish working");
LongRunningJob();
}, IsBackground = true);
Thread foregroundThread = new Thread(() =>
{
Console.WriteLine("I'll stop when I'm finished");
LongRunningJob();
}
backgroundThread.Start();
foregroundThread.Start()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
687 次 |
最近记录: |