Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long-running code within asp.net process

Tags:

.net

asp.net

E.g. we this code in the asp.net form codebihind:

private void btnSendEmails_OnClick()
{
    Send100000EmailsAndWaitForReplies();
}

This code execution will be killed by the timeout reason. For resolving the problem I'd like to see something like this:

private void btnSendEmails_OnClick()
{
    var taskId = AsyncTask.Run( () =>  Send100000EmailsAndWaitForReplies() );
    // Store taskId for future task execution status checking.
}

And this method will be executed for some way outside the w3wp.exe process within a special enveronment.

Does anybody know a framework/toolset for resolving this kind of issues?

Update: The emails sending method is only an example of what I mean. In fact, I could have a lot of functionality need to be executed outside the asp.net working process.

E.g. this point is very important for an application which aggregates data from a couple of 3rd party services, do something with it and send it back to another service.

like image 716
noetic Avatar asked Sep 22 '08 23:09

noetic


2 Answers

This has been discussed as a part of other questions:

Multithreading in asp.net

BackgroundWorker thread in ASP.NET

There is no good way to do this. You don't want any long running processes in the ASP.NET worker process, since it might recycle before you are done.

Write a Windows Service to run in the background that does the work for you. Drop messages into MSMQ to initiate tasks. Then they can run as long as they want.

like image 113
Eric Z Beard Avatar answered Sep 27 '22 17:09

Eric Z Beard


  1. QueueBackgroundWorkItem My sample shows sending email.
  2. HangFire Open source project works on shared hosting.
  3. On Azure, you can use WebJobs.
  4. On Azure, Cloud services
like image 40
RickAndMSFT Avatar answered Sep 27 '22 16:09

RickAndMSFT