From 7e3dc5b15686556d0d1fabb8027f3e785ad19218 Mon Sep 17 00:00:00 2001 From: Chuang <54572251+xccc-msft@users.noreply.github.com> Date: Thu, 9 Apr 2020 11:37:19 +0800 Subject: [PATCH] Resources: support TaskCanceledException handling for different .NET environment (#1019) * Resources: add exception handling when task is canceled * Resources: fix else if * Resources: handle request cancellation --- .../Core/ExternalChildResourceCollection.cs | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/ResourceManagement/ResourceManager/Core/ExternalChildResourceCollection.cs b/src/ResourceManagement/ResourceManager/Core/ExternalChildResourceCollection.cs index 83631c412..f611cc97b 100644 --- a/src/ResourceManagement/ResourceManager/Core/ExternalChildResourceCollection.cs +++ b/src/ResourceManagement/ResourceManager/Core/ExternalChildResourceCollection.cs @@ -69,12 +69,16 @@ public Task> CommitAndGetAllAsync(CancellationToken cance exceptions.Add(deleteTask.Exception); } } + else if (deleteTask.IsCanceled) + { + cancellationToken.ThrowIfCancellationRequested(); + exceptions.Add(new TaskCanceledException()); + } else { comitted.Add(res); res.PendingOperation = PendingOperation.None; - FluentModelTImpl val; - this.collection.TryRemove(res.Name(), out val); + this.collection.TryRemove(res.Name(), out FluentModelTImpl val); } }, cancellationToken, @@ -91,8 +95,7 @@ public Task> CommitAndGetAllAsync(CancellationToken cance { if (createTask.IsFaulted) { - FluentModelTImpl val; - this.collection.TryRemove(res.Name(), out val); + this.collection.TryRemove(res.Name(), out FluentModelTImpl val); if (createTask.Exception.InnerException != null) { exceptions.Add(createTask.Exception.InnerException); @@ -102,6 +105,12 @@ public Task> CommitAndGetAllAsync(CancellationToken cance exceptions.Add(createTask.Exception); } } + else if (createTask.IsCanceled) + { + this.collection.TryRemove(res.Name(), out FluentModelTImpl val); + cancellationToken.ThrowIfCancellationRequested(); + exceptions.Add(new TaskCanceledException()); + } else { comitted.Add(res); @@ -131,6 +140,11 @@ public Task> CommitAndGetAllAsync(CancellationToken cance exceptions.Add(updateTask.Exception); } } + else if (updateTask.IsCanceled) + { + cancellationToken.ThrowIfCancellationRequested(); + exceptions.Add(new TaskCanceledException()); + } else { comitted.Add(res);