Posts

Showing posts with the label Dnx

Alternative Solution To HostingEnvironment.QueueBackgroundWorkItem In .NET Core

Answer : Update December 2019: ASP.NET Core 3.0 supports an easy way to implement background tasks using Microsoft.NET.Sdk.Worker. It's excellent and works really well. As @axelheer mentioned IHostedService is the way to go in .NET Core 2.0 and above. I needed a lightweight like for like ASP.NET Core replacement for HostingEnvironment.QueueBackgroundWorkItem, so I wrote DalSoft.Hosting.BackgroundQueue which uses.NET Core's 2.0 IHostedService. PM> Install-Package DalSoft.Hosting.BackgroundQueue In your ASP.NET Core Startup.cs: public void ConfigureServices(IServiceCollection services) { services.AddBackgroundQueue(onException:exception => { }); } To queue a background Task just add BackgroundQueue to your controller's constructor and call Enqueue . public EmailController(BackgroundQueue backgroundQueue) { _backgroundQueue = backgroundQueue; } [HttpPost, Route("/")] public IActionResult SendEmail([FromBody]emailRequest) { ...