Long-term code in asp.net process

eg. we have this code in the form of asp.net codebihind:

private void btnSendEmails_OnClick() { Send100000EmailsAndWaitForReplies(); } 

Execution of this code will be killed due to a timeout. To solve the problem, I would 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 somehow outside the w3wp.exe process in a special enveronment.

Does anyone know a structure / toolkit to solve such problems?

Update: The email sending method is just an example of what I mean. In fact, I could have a lot of functionality that should run outside the asp.net workflow.

eg. this moment is very important for an application that combines data from several third-party services, something with it and sends back to another service.

+4
source share
9 answers
+3
source

This was discussed as part of other issues:

Multithreading in asp.net

BackgroundWorker theme in ASP.NET

There is no good way to do this. You do not want any lengthy processes in your ASP.NET workflow, as they can be recycled before you finish.

Record a Windows service that will run in the background, which will work for you. Drop messages in MSMQ to run tasks. Then they can work as much as they want.

+5
source

I can think of two possible ways that I would omit:

  • You can create the Windows service hosting the remote object and call a web application on the remote object to ensure that this method runs outside the IIS process space.
  • You can configure the database or MSMQ with which you will register the request. Your web application can then track the status of the request so that you can subsequently notify the user of its completion. I would anticipate a service filling out requests.
+1
source

ASP.NET hosting is very dangerous for any lengthy process, both for the processor and for I / O, as AppDomain may suddenly unload.

If you want to perform background tasks outside the request processing pipeline, consider using http://hangfire.io , it handles all the difficulties and risks of background processing for you, without the need to install additional Windows services (but they can be used when the time comes). There is also an email tutorial .

+1
source

One option is for the task to execute a certain number of letters, and then Response.Redirect will return to itself and repeat until all your letters have been sent.

0
source

You may have functionality that sends mail as a service. Send a request to him and let him process it. Request each time for your status. If you have control over the server, you can install a Windows service that would probably be ideal for optimal processing and lifecycle management.

0
source

ASP.NET 2.0+ supports the concept of asynchronous pages. Add the Async = "true" page directive to your page.

Then, in Page_Load, use the BeginEventHandler and EndEventHandler delegates so that the code runs asynchronously in their respective "handlers".

 <%@ Page Language="C#" Async="true" %> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { BeginEventHandler begin = new BeginEventHandler(BeginMethod); EndEventHandler end = new EndEventHandler(EndMethod); AddOnPreRenderCompleteAsync(begin, end); } </script> 
0
source

It is often impossible to start a Windows user service if your site is hosted by a hosting provider. Running a single thread that you slept to run at regular intervals may be incompatible, because IIS will strive to recycle your threads periodically and even set the script -execution property in the cofig file or through code, does not guarantee that your thread is not destroyed and not is being processed.

I came up with the cache expiration method, which, given the methods available in the shared hosting service, may be the safest, that is, the most consistent option. This article explains this pretty well and provides a job queue class that helps manage scheduled jobs at the end of the article; see here - http://www.codeproject.com/KB/aspnet/ASPNETService.aspx

0
source

Server.ScriptTimeout = 360000000;

-3
source

Source: https://habr.com/ru/post/1277048/


All Articles