简体   繁体   English

IIS 7.5应用程序池回收不让方法完成

[英]IIS 7.5 Application Pool Recycle not letting method finish

I have 2 methods: 我有两种方法:

public void Stop(bool immediate)
{
    Logger.Log(LogLevel.Debug, "Shutdown detected. Immediate: " + immediate);
    Shutdown();
    Logger.Log(LogLevel.Debug, "Unregistering");
    HostingEnvironment.UnregisterObject(this);
}

public void Shutdown()
{
    Logger.Log(LogLevel.Debug, "Preparing to stop UploadQueue");
    IsProcessing = false;

     //Set tasks to cancel to prevent queued tasks from parsing
     _cancellationTokenSource.Cancel();

     Logger.Log(LogLevel.Debug, "Waiting for " + _workerTasks.Count + " tasks to finish or cancel.");
     //Wait for tasks to finish
     Task.WaitAll(_workerTasks.Values.ToArray());

     Logger.Log(LogLevel.Debug, "Stopped UploadQueue");
}

The class is using the IRegisteredObject interface to receive the shutdown notification. 该类使用IRegisteredObject接口来接收关闭通知。 In my log I get this: 在我的日志中,我得到了这个:

2014-07-18 15:30:55,913,DEBUG,Shutdown detected. Immediate: False
2014-07-18 15:30:55,913,DEBUG,Preparing to stop UploadQueue
2014-07-18 15:30:55,913,DEBUG,Waiting for 35 tasks to finish or cancel.
...
bunch of stuff
...
2014-07-18 15:31:28,471,DEBUG,Shutdown detected. Immediate: True
2014-07-18 15:31:28,471,DEBUG,Preparing to stop UploadQueue
2014-07-18 15:31:28,471,DEBUG,Waiting for 0 tasks to finish or cancel.
2014-07-18 15:31:28,471,DEBUG,Stopped UploadQueue
2014-07-18 15:31:28,471,DEBUG,Unregistering

Why isn't it getting to the Logger.Log(LogLevel.Debug, "Stopped UploadQueue"); 为什么不进入Logger.Log(LogLevel.Debug, "Stopped UploadQueue"); the first time? 第一次? it certainly seems like its cancelling the tasks and letting the ones that are running finish. 它肯定似乎取消了任务并让正在运行的任务完成。 (The task checks if its cancelled before running, otherwise does it's thing). (任务检查它是否在运行前被取消,否则就是这样)。

From Task.WaitAll : 来自Task.WaitAll

AggregationException: AggregationException:

At least one of the Task instances was canceled -or- an exception was thrown during the execution of at least one of the Task instances. 至少有一个Task实例被取消 - 或者 - 在执行至少一个Task实例期间抛出了异常。 If a task was canceled, the AggregateException contains an OperationCanceledException in its InnerExceptions collection. 如果任务被取消,则AggregateException在其InnerExceptions集合中包含OperationCanceledException。

You are cancelling your Tasks via _cancellationTokenSource.Cancel(); 您正在通过_cancellationTokenSource.Cancel();取消您的任务_cancellationTokenSource.Cancel(); and i assume that is causing at least one of them to throw an exception. 我认为这至少导致其中一个抛出异常。 You might be catching it in a higher level stackframe and disregarding it. 您可能会在更高级别的堆栈框架中捕获它并忽略它。 Wrap Task.WaitAll inside a try-catch block: 在try-catch块中包装Task.WaitAll

public void Shutdown()
{
    Logger.Log(LogLevel.Debug, "Preparing to stop UploadQueue");
    IsProcessing = false;

    //Set tasks to cancel to prevent queued tasks from parsing
    _cancellationTokenSource.Cancel();

    Logger.Log(LogLevel.Debug, "Waiting for " + _workerTasks.Count + " tasks to finish or cancel.");

     try
     {
         //Wait for tasks to finish
         Task.WaitAll(_workerTasks.Values.ToArray());

         Logger.Log(LogLevel.Debug, "Stopped UploadQueue");
     }
     catch (AggregationException e)
     {
         // Recover from the exception
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM