diff --git a/src/MockHttp/Responses/TimeoutBehavior.cs b/src/MockHttp/Responses/TimeoutBehavior.cs index 025bf961..78784a0e 100644 --- a/src/MockHttp/Responses/TimeoutBehavior.cs +++ b/src/MockHttp/Responses/TimeoutBehavior.cs @@ -28,20 +28,21 @@ public Task HandleAsync(MockHttpRequestContext requestContext, HttpResponseMessa .ContinueWith(_ => { var tcs = new TaskCompletionSource(); -#if (NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER) - bool isNetCore3 = RuntimeInformation.FrameworkDescription.StartsWith(".NET Core 3", StringComparison.OrdinalIgnoreCase); // Shim for .NET 5 being EOL and no longer producing an assembly for it. + bool isNotNet5OrGreater = true; - if (!cancellationToken.IsCancellationRequested && !isNetCore3) +#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER + isNotNet5OrGreater = RuntimeInformation.FrameworkDescription.StartsWith(".NET Core 3", StringComparison.OrdinalIgnoreCase); // Shim for .NET 5 being EOL and no longer producing an assembly for it. +#endif + + if (cancellationToken.IsCancellationRequested || isNotNet5OrGreater) { - tcs.TrySetException(new TaskCanceledException(null, new TimeoutException())); + tcs.TrySetCanceled(); } else { - tcs.TrySetCanceled(); + tcs.TrySetException(new TaskCanceledException(null, new TimeoutException())); } -#else - tcs.TrySetCanceled(); -#endif + return tcs.Task; }, TaskScheduler.Current)