3
votes

I'm looking to leverage the QueueBackgroundWorkItem feature in available in .NET 4.5.2+ on our ASP.NET Web API application, however, I consistently am getting "Invalid Operation" exceptions when attempting to put something into the queue. I believe this may be related to the fact that our app is self-hosted using OWIN and the queueing of the work item is an IIS only feature? If this is true, just wanted to see if there would be any suggestions at all in regards to queueing background work from an owin-self hosted API.

2

2 Answers

3
votes

HostingEnvironment.QueueBackgroundWorkItem() is part of System.Web and as a result, it will only work in the classic ASP.NET pipeline. It won't work in self-hosted OWIN pipeline.

Hangfire is your only option to run long background tasks

2
votes

I had this issue in a test environment answered here.

In the end, I used Hangfire and its fire and forget code

//Fire-and-forget tasks
// Static methods are for demo purposes
BackgroundJob.Enqueue(
    () => Console.WriteLine("Simple!"));