Gregory Nozik
Gregory Nozik

Reputation: 3374

IIS and Parallel.ForEach

I am using Parallel.ForEach on WCF service that hosted in IIS .

As far as I know for each task in parallel loop, thread will be opened.

But IIS have restriction on numner of threads that can be opened. I think it's 20.

So my question is : Does it recommeneded to use Parallel.ForEach on IIS processes ?

Upvotes: 4

Views: 1815

Answers (1)

Davin Tryon
Davin Tryon

Reputation: 67296

As far as I'm aware there are a few issues when using TPL in IIS.

First, IIS does not have a hard restriction on threads in its thread pool. It really depends on what the threads are doing (CPU bound, IO bound etc). So, the number of threads by itself doesn't matter that much.

Second, as I understand it, TPL will take threads from the thread pool. So, in effect, you are taking threads that could be used to service requests. Again, this could be bad depending on how much traffic you are consuming.

Third, IIS application pools will recycle. When this happens, again as I understand it, IIS will freeze the state of the threads and move them to another process. This could have unintended effects on the threaded operations.

Usually, you want to use TPL (Parallel.ForEach) because you have a long running process. For long running processes, it might be better to call another host (Windows Service) to do the heavy lifting. In a Windows Service, for example, you have more control over how you manage threads.

Hope this helps.

Upvotes: 4

Related Questions