Zapnologica
Zapnologica

Reputation: 22556

Long Running Operations / Threads In ASP.NET IIS Site

How does the threading system work on ASP.NET MVC?

If I spawn a new thread on a request, that does a certain task. Will this thread be killed when the request ends or timesout?

For example,

I have an Action Method Which Receives a csv File, which needs to be parsed and saved to a database. Now the correct way to do this would be to have a service running that executes these requests, However in my case this import is only going to happen once a month and for us the maintenance of having a service running 24/7 is not worth the benefit of it.

Is there maybe a type of service that can be run in the IIS environment which is not effected by the state of the HTTP Request?

Upvotes: 0

Views: 821

Answers (2)

Elad Lachmi
Elad Lachmi

Reputation: 10561

Expanding on Josh`s answer. The thread will not be killed as soon as the request processing ends. But it might get killed if the w3wp for your app pool gets killed for some reason. A side from the reasons Josh stated, this might also happen if an exception bubbles up without being handled by your code and also, the app pool might recycle for several reasons.

A windows service is one solution. In one project I used AppFabric to set the app pool to never recycle and always remain on. This worked very well for me, for the clients specific story and might also be the right solution for you.

Look here for more information.

Upvotes: 1

Josh
Josh

Reputation: 10604

You will always be subject to the application pool's lifetime setting. If, at any point, no additional requests come into IIS that are also served by the application pool, your request will stop as the application pool is shut down. You have two options: increase the application pool's lifetime or go the windows service route. You might be able to get away with scripting the lengthening of the pool's lifetime right before you do the upload and then scripting it back to be a shorter, more manageable length.

Upvotes: 2

Related Questions