From 5ba6beff18eeca9d7699d0a3eafc33c678a19979 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Fri, 4 Mar 2022 02:47:53 +0800 Subject: [PATCH] [C#] Add cancellation token overloads to streaming interfaces (#27886) * Add cancellation token overloads to streaming interfaces * PR feedback --- src/csharp/Grpc.Core.Api/Grpc.Core.Api.csproj | 2 +- .../Grpc.Core.Api/IAsyncStreamWriter.cs | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/csharp/Grpc.Core.Api/Grpc.Core.Api.csproj b/src/csharp/Grpc.Core.Api/Grpc.Core.Api.csproj index e684cf8b55e..cc21a14c977 100755 --- a/src/csharp/Grpc.Core.Api/Grpc.Core.Api.csproj +++ b/src/csharp/Grpc.Core.Api/Grpc.Core.Api.csproj @@ -13,7 +13,7 @@ - net45;netstandard1.5;netstandard2.0 + net45;netstandard1.5;netstandard2.0;netstandard2.1 true true diff --git a/src/csharp/Grpc.Core.Api/IAsyncStreamWriter.cs b/src/csharp/Grpc.Core.Api/IAsyncStreamWriter.cs index b44eba02d02..717fa1eae94 100644 --- a/src/csharp/Grpc.Core.Api/IAsyncStreamWriter.cs +++ b/src/csharp/Grpc.Core.Api/IAsyncStreamWriter.cs @@ -20,6 +20,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace Grpc.Core @@ -36,6 +37,26 @@ namespace Grpc.Core /// The message to be written. Cannot be null. Task WriteAsync(T message); +#if NETSTANDARD2_1_OR_GREATER + /// + /// Writes a message asynchronously. Only one write can be pending at a time. + /// + /// The message to be written. Cannot be null. + /// Cancellation token that can be used to cancel the operation. + Task WriteAsync(T message, CancellationToken cancellationToken) + { + if (cancellationToken.CanBeCanceled) + { + // Note to implementors: + // Add a netstandard2.1 or greater target to your library and override + // WriteAsync(T, CancellationToken) on stream writer to use the cancellation token. + throw new NotSupportedException("Cancellation of stream writes is not supported by this gRPC implementation."); + } + + return WriteAsync(message); + } +#endif + /// /// Write options that will be used for the next write. /// If null, default options will be used.