Concerns with long-running process on IIS

感情迁移 提交于 2020-01-04 02:31:12

问题


Concerns:

I have read through posts/blogs that describe IIS could recycle the app pool at any time. Does this mean (in terms of IIS recycling app pool) it would not matter if I make a call to a long running process synchronous or asynchronous, as IIS could recycle the app pool and terminate the long-running process? If this is the case, what are common paths to make sure this does not happen?

Example

public Task<ActionResult> LongRunningProcessAsync(SubmitFileModel aModel)

    {
        return Task.Run( () => LongRunningProcess( aModel) );
    }

回答1:


HostingEnvironment has a static method, RegisterObject, that allows you to place an object in the list of registered objects for the application.

According to Phil Haack...

When ASP.NET tears down the AppDomain, it will first attempt to call Stop method on all registered objects....When ASP.NET calls into this method, your code needs to prevent this method from returning until your work is done.

Alternatively, you could create a service that is not hosted by IIS.




回答2:


If this is the case, what are common paths to make sure this does not happen?

Your application domain will be unloaded on recycling process, with or without using the RegisterObject method. It only gives your background work some time to complete (30 seconds by default). If you have long-running job that exceeds this "some time", it will be aborted, and no one will care about automatic retry on restart.

If you want to run long-running jobs in your ASP.NET application, want to keep things simple, and forget about ASP.NET/IIS issues, look at HangFire – it uses persistent storages (SQL Server or Redis) and have immunity to the recycling process.



来源:https://stackoverflow.com/questions/13746411/concerns-with-long-running-process-on-iis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!