From a4b7d9d5a702a3ca697deccaa2f1f82d21d82034 Mon Sep 17 00:00:00 2001 From: ZLoo Date: Thu, 18 Jul 2024 01:11:15 +0300 Subject: [PATCH] Fix S4457 warning --- src/Polly/AsyncPolicy.ExecuteOverloads.cs | 113 ++++++++++++------ .../AsyncPolicy.TResult.ExecuteOverloads.cs | 46 ++++--- src/Polly/Polly.csproj | 2 +- 3 files changed, 108 insertions(+), 53 deletions(-) diff --git a/src/Polly/AsyncPolicy.ExecuteOverloads.cs b/src/Polly/AsyncPolicy.ExecuteOverloads.cs index 232ba2a3a9a..86b7f683f8f 100644 --- a/src/Polly/AsyncPolicy.ExecuteOverloads.cs +++ b/src/Polly/AsyncPolicy.ExecuteOverloads.cs @@ -100,23 +100,14 @@ public Task ExecuteAsync(Func action, IDiction /// Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods. /// A which completes when is registered. [DebuggerStepThrough] - public async Task ExecuteAsync(Func action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) + public Task ExecuteAsync(Func action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { if (context == null) { throw new ArgumentNullException(nameof(context)); } - SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey); - - try - { - await ImplementationAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); - } - finally - { - PolicyBase.RestorePolicyContext(context, priorPolicyWrapKey, priorPolicyKey); - } + return ExecuteInternalAsync(action, context, continueOnCapturedContext, cancellationToken); } #region Overloads method-generic in TResult @@ -226,23 +217,14 @@ public Task ExecuteAsync(FuncThe value returned by the action. /// Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods. [DebuggerStepThrough] - public async Task ExecuteAsync(Func> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) + public Task ExecuteAsync(Func> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { if (context == null) { throw new ArgumentNullException(nameof(context)); } - SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey); - - try - { - return await ImplementationAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); - } - finally - { - PolicyBase.RestorePolicyContext(context, priorPolicyWrapKey, priorPolicyKey); - } + return ExecuteInternalAsync(action, context, continueOnCapturedContext, cancellationToken); } #endregion @@ -349,22 +331,14 @@ public Task ExecuteAndCaptureAsync(FuncPlease use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods. /// The captured result. [DebuggerStepThrough] - public async Task ExecuteAndCaptureAsync(Func action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) + public Task ExecuteAndCaptureAsync(Func action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { if (context == null) { throw new ArgumentNullException(nameof(context)); } - try - { - await ExecuteAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); - return PolicyResult.Successful(context); - } - catch (Exception exception) - { - return PolicyResult.Failure(exception, GetExceptionType(ExceptionPredicates, exception), context); - } + return ExecuteAndCaptureInternalAsync(action, context, continueOnCapturedContext, cancellationToken); } #region Overloads method-generic in TResult @@ -476,25 +450,88 @@ public Task> ExecuteAndCaptureAsync(FuncThe captured result. /// Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods. [DebuggerStepThrough] - public async Task> ExecuteAndCaptureAsync(Func> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) + public Task> ExecuteAndCaptureAsync( + Func> action, + Context context, + CancellationToken cancellationToken, + bool continueOnCapturedContext) { if (context == null) { throw new ArgumentNullException(nameof(context)); } + return ExecuteAndCaptureInternalAsync(action, context, continueOnCapturedContext, cancellationToken); + } + + #endregion + + #endregion + + private async Task ExecuteInternalAsync(Func action, Context context, bool continueOnCapturedContext, CancellationToken cancellationToken) + { + SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey); + + try + { + await ImplementationAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); + } + finally + { + PolicyBase.RestorePolicyContext(context, priorPolicyWrapKey, priorPolicyKey); + } + } + + private async Task ExecuteInternalAsync( + Func> action, + Context context, + bool continueOnCapturedContext, + CancellationToken cancellationToken) + { + SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey); + + try + { + return await ImplementationAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); + } + finally + { + PolicyBase.RestorePolicyContext(context, priorPolicyWrapKey, priorPolicyKey); + } + } + + private async Task ExecuteAndCaptureInternalAsync( + Func action, + Context context, + bool continueOnCapturedContext, + CancellationToken cancellationToken) + { + try + { + await ExecuteAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); + return PolicyResult.Successful(context); + } + catch (Exception exception) + { + return PolicyResult.Failure(exception, GetExceptionType(ExceptionPredicates, exception), context); + } + } + + private async Task> ExecuteAndCaptureInternalAsync( + Func> action, + Context context, + bool continueOnCapturedContext, + CancellationToken cancellationToken) + { try { return PolicyResult.Successful( - await ExecuteAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext), context); + await ExecuteAsync(action, context, cancellationToken, continueOnCapturedContext) + .ConfigureAwait(continueOnCapturedContext), context); } catch (Exception exception) { return PolicyResult.Failure(exception, GetExceptionType(ExceptionPredicates, exception), context); } } - - #endregion - - #endregion } diff --git a/src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs b/src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs index 8e704d5b2e5..549cf6fedf7 100644 --- a/src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs +++ b/src/Polly/AsyncPolicy.TResult.ExecuteOverloads.cs @@ -100,23 +100,14 @@ public Task ExecuteAsync(Func /// The value returned by the action. /// Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods. [DebuggerStepThrough] - public async Task ExecuteAsync(Func> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) + public Task ExecuteAsync(Func> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { if (context == null) { throw new ArgumentNullException(nameof(context)); } - SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey); - - try - { - return await ImplementationAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); - } - finally - { - PolicyBase.RestorePolicyContext(context, priorPolicyWrapKey, priorPolicyKey); - } + return ExecuteInternalAsync(action, context, continueOnCapturedContext, cancellationToken); } #endregion @@ -221,13 +212,42 @@ public Task> ExecuteAndCaptureAsync(FuncThe captured result. /// Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods. [DebuggerStepThrough] - public async Task> ExecuteAndCaptureAsync(Func> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) + public Task> ExecuteAndCaptureAsync(Func> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { if (context == null) { throw new ArgumentNullException(nameof(context)); } + return ExecuteAndCaptureInternalAsync(action, context, continueOnCapturedContext, cancellationToken); + } + + #endregion + + private async Task ExecuteInternalAsync( + Func> action, + Context context, + bool continueOnCapturedContext, + CancellationToken cancellationToken) + { + SetPolicyContext(context, out string priorPolicyWrapKey, out string priorPolicyKey); + + try + { + return await ImplementationAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); + } + finally + { + PolicyBase.RestorePolicyContext(context, priorPolicyWrapKey, priorPolicyKey); + } + } + + private async Task> ExecuteAndCaptureInternalAsync( + Func> action, + Context context, + bool continueOnCapturedContext, + CancellationToken cancellationToken) + { try { TResult result = await ExecuteAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); @@ -244,6 +264,4 @@ public async Task> ExecuteAndCaptureAsync(Func.Failure(exception, GetExceptionType(ExceptionPredicates, exception), context); } } - - #endregion } diff --git a/src/Polly/Polly.csproj b/src/Polly/Polly.csproj index 177c9f8b3a0..0a4a3835afb 100644 --- a/src/Polly/Polly.csproj +++ b/src/Polly/Polly.csproj @@ -8,7 +8,7 @@ 70 true $(NoWarn);CA1010;CA1031;CA1051;CA1062;CA1063;CA1064;CA1710;CA1716;CA1724;CA1805;CA1815;CA1816;CA2211 - $(NoWarn);S2223;S3215;S3246;S3971;S4039;S4457 + $(NoWarn);S2223;S3215;S3246;S3971;S4039 $(NoWarn);RS0037;