net – Long tasks in IIS server

Question:

There is a task to develop a service for sending mail, which will be used by other services / applications / sites within the company. I plan to make a WEB service. The scheme is something like this:

  1. there is a public method that accepts what to send and to whom.
  2. the method receives a batch of tasks.
  3. The method spawns a thread that takes tasks and sends out little by little. The method itself continues to await new challenges.

There are at least 2 problems:

  1. Before that, I always hosted services in IIS . However, given our mailing volumes, we can theoretically get a task that will last for hours. As far as I understand, IIS can itself interrupt tasks that take a long time. How can I avoid this?
  2. The service works in the context of IIS . I think (but not sure) that in reality my method will not spawn threads, but reserve existing ones inside the IIS thread pool. In total, as a result, we can block all streams and completely block the service.

What is the easiest way out of this situation? Maybe I should use SelfHosting WCF ?

Answer:

In total, as a result, we can block all streams and completely block the service.

I would like to highlight this point a little.

You don't need to create one thread for each call every time. I am not at all familiar with development in the context of IIS, but I am sure that inside it is possible to implement a certain service – the main control thread (if required) and N worker threads. The service is just a wrapper over one large queue of jobs that the worker threads take one at a time and execute, and when it becomes necessary to send a bunch of letters, the thread that received these letters should simply feed them to the service, which will simply put them at the end of the queue. After that, problems with server overload disappear by themselves: the number of threads is always fixed, in the case of an empty queue, they sleep, they cannot gobble up the entire processor either, because most of the time they wait for responses over the network. This is where the problems begin with a slightly different nature: you need to monitor not the server load, but so that the queue does not have the opportunity to grow indefinitely. Therefore, the method of receiving messages should return 200 if the size of the queue + transmitted messages do not exceed the limit and 503 if there is a danger of getting out of the reasonable limits of the queue.

Scroll to Top