[C#] Add ConfigureAwait to AsyncUnaryCall and AsyncClientStreamingCall (#28235)

* Add ConfigureAwait to AsyncUnaryCall and AsyncClientStreamingCall

* Update comment
pull/28177/head
James Newton-King 3 years ago committed by GitHub
parent 540a785e5f
commit 65efed181d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      src/csharp/Grpc.Core.Api/AsyncClientStreamingCall.cs
  2. 16
      src/csharp/Grpc.Core.Api/AsyncUnaryCall.cs
  3. 21
      src/csharp/Grpc.Core.Tests/ClientServerTest.cs

@ -111,14 +111,27 @@ namespace Grpc.Core
}
/// <summary>
/// Allows awaiting this object directly.
/// Gets an awaiter used to await this <see cref="AsyncClientStreamingCall{TRequest,TResponse}"/>.
/// </summary>
/// <returns></returns>
/// <returns>An awaiter instance.</returns>
/// <remarks>This method is intended for compiler use rather than use directly in code.</remarks>
public TaskAwaiter<TResponse> GetAwaiter()
{
return responseAsync.GetAwaiter();
}
/// <summary>
/// Configures an awaiter used to await this <see cref="AsyncClientStreamingCall{TRequest,TResponse}"/>.
/// </summary>
/// <param name="continueOnCapturedContext">
/// true to attempt to marshal the continuation back to the original context captured; otherwise, false.
/// </param>
/// <returns>An object used to await this task.</returns>
public ConfiguredTaskAwaitable<TResponse> ConfigureAwait(bool continueOnCapturedContext)
{
return responseAsync.ConfigureAwait(continueOnCapturedContext);
}
/// <summary>
/// Gets the call status if the call has already finished.
/// Throws InvalidOperationException otherwise.

@ -93,13 +93,27 @@ namespace Grpc.Core
}
/// <summary>
/// Allows awaiting this object directly.
/// Gets an awaiter used to await this <see cref="AsyncUnaryCall{TResponse}"/>.
/// </summary>
/// <returns>An awaiter instance.</returns>
/// <remarks>This method is intended for compiler use rather than use directly in code.</remarks>
public TaskAwaiter<TResponse> GetAwaiter()
{
return responseAsync.GetAwaiter();
}
/// <summary>
/// Configures an awaiter used to await this <see cref="AsyncUnaryCall{TResponse}"/>.
/// </summary>
/// <param name="continueOnCapturedContext">
/// true to attempt to marshal the continuation back to the original context captured; otherwise, false.
/// </param>
/// <returns>An object used to await this task.</returns>
public ConfiguredTaskAwaitable<TResponse> ConfigureAwait(bool continueOnCapturedContext)
{
return responseAsync.ConfigureAwait(continueOnCapturedContext);
}
/// <summary>
/// Gets the call status if the call has already finished.
/// Throws InvalidOperationException otherwise.

@ -65,6 +65,8 @@ namespace Grpc.Core.Tests
Assert.AreEqual("ABC", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "ABC"));
Assert.AreEqual("ABC", await Calls.AsyncUnaryCall(helper.CreateUnaryCall(), "ABC"));
Assert.AreEqual("ABC", await Calls.AsyncUnaryCall(helper.CreateUnaryCall(), "ABC").ConfigureAwait(false));
}
[Test]
@ -197,12 +199,21 @@ namespace Grpc.Core.Tests
return result;
});
var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall());
await call.RequestStream.WriteAllAsync(new string[] { "A", "B", "C" });
Assert.AreEqual("ABC", await call.ResponseAsync);
{
var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall());
await call.RequestStream.WriteAllAsync(new string[] { "A", "B", "C" });
Assert.AreEqual("ABC", await call);
Assert.AreEqual(StatusCode.OK, call.GetStatus().StatusCode);
Assert.IsNotNull(call.GetTrailers());
}
Assert.AreEqual(StatusCode.OK, call.GetStatus().StatusCode);
Assert.IsNotNull(call.GetTrailers());
{
var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall());
await call.RequestStream.WriteAllAsync(new string[] { "A", "B", "C" });
Assert.AreEqual("ABC", await call.ConfigureAwait(false));
Assert.AreEqual(StatusCode.OK, call.GetStatus().StatusCode);
Assert.IsNotNull(call.GetTrailers());
}
}
[Test]

Loading…
Cancel
Save