Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HostingEnviornment.QueueBackgroundWorkItem is not working in azure worker role

I am trying to use HostingEnviornment.QueueBackgroundWorkItem on azure worker role for some task execution on background but I am getting exception in the code "operation is not valid due to the current state of the object.".

Can we use HostingEnviornment.QueueBackgroundWorkItem on azure worker role If not then please help me what could I use for process task in background on worker role.

like image 377
mahesh sharma Avatar asked Feb 01 '17 07:02

mahesh sharma


People also ask

Is it possible to use queuebackgroundworkitem in a web application?

This feature is only available after .Net Framework 4.5.2. So you have to choose that type of web application. The QueueBackgroundWorkItem method has two overloads, each of which accepts a single parameter. You can pass either of the following,

How to create a worker role in Azure service bus queue?

Right click on Roles ->Add ->New Worker Role Project - > Worker Role with the service bus queue. Please, go through my previous article where I have explained about the Azure Service bus queue. The worker role is a simple class library project, by default, it consists of the class file which inherits the RoleEntryPoint class

What is the new hostingenvironmentqueuebackgroundworkitem API?

What is this new API? .NET Framework 4.5.2 has released few new APIs for ASP.NET one of them is HostingEnvironment.QueueBackgroundWorkItem . Any one who has tried to run background tasks “reliably” in ASP.NET must have felt relieved with this.

What is worker role in Azure cloud service?

In my previous article, we have seen the web role in Azure cloud service. In this article, I’m going to talk about Worker Role in Azure Cloud Service. The basic difference between the web and worker role is web role support and runs on IIS (Internet Information Service), where the worker role doesn’t support IIS.


1 Answers

According to System.Web.Hosting.HostingEnvironment and QueueBackgroundWorkItem:

Differs from a normal ThreadPool work item in that ASP.NET can keep track of how many work items registered through this API are currently running, and the ASP.NET runtime will try to delay AppDomain shutdown until these work items have finished executing. This API cannot be called outside of an ASP.NET-managed AppDomain. The provided CancellationToken will be signaled when the application is shutting down.

Per my understanding, HostingEnvironment.QueueBackgroundWorkItem provides you with the ability to enqueue background tasks from within an ASP.Net web application. ASP.NET tracks these tasks and prevents IIS from abruptly terminating the worker process until all background tasks have completed.

Also, I tried to invoke HostingEnvironment.QueueBackgroundWorkItem within my worker role both on my side and Azure, and a new console application. While invoking this method within a ASP.NET Web application, it could work as expected.

I assumed that you could leverage Task-based Asynchronous Programming as follows to run your background jobs:

var tokenSource = new CancellationTokenSource();
var cancellationToken = tokenSource.Token;

Task.Factory.StartNew((token) => {
    var ct = (CancellationToken)token;
    while (!ct.IsCancellationRequested)
    {
        //do your job
    }
}, cancellationToken, cancellationToken);

Moreover, you could leverage Hangfire to perform background processing in DotNet. Here are some references, you could refer to them (tutorial and tutorial).

like image 148
Bruce Chen Avatar answered Oct 19 '22 12:10

Bruce Chen