private static void RegisterCancellationCallback <TRequest, TResponse>(AsyncCall <TRequest, TResponse> asyncCall, CancellationToken token) { if (token.CanBeCanceled) { token.Register(() => asyncCall.Cancel()); } }
public void ClientStreaming_WriteAfterCancellationRequestThrowsOperationCancelledException() { var resultTask = asyncCall.ClientStreamingCallAsync(); var requestStream = new ClientRequestStream <string, string>(asyncCall); asyncCall.Cancel(); Assert.IsTrue(fakeCall.IsCancelled); Assert.Throws(typeof(OperationCanceledException), () => requestStream.WriteAsync("request1")); fakeCall.UnaryResponseClientHandler(true, CreateClientSideStatus(StatusCode.Cancelled), CreateResponsePayload(), new Metadata()); AssertUnaryResponseError(asyncCall, fakeCall, resultTask, StatusCode.Cancelled); }
public void ClientStreaming_WriteAfterCancellationRequestThrowsTaskCanceledException() { var resultTask = asyncCall.ClientStreamingCallAsync(); var requestStream = new ClientRequestStream <string, string>(asyncCall); asyncCall.Cancel(); Assert.IsTrue(fakeCall.IsCancelled); var writeTask = requestStream.WriteAsync("request1"); Assert.ThrowsAsync(typeof(TaskCanceledException), async() => await writeTask); fakeCall.UnaryResponseClientCallback.OnUnaryResponseClient(true, CreateClientSideStatus(StatusCode.Cancelled), null, new Metadata()); AssertUnaryResponseError(asyncCall, fakeCall, resultTask, StatusCode.Cancelled); }
public static AsyncUnaryCall <TResponse> AsyncUnaryCall <TRequest, TResponse>(CallInvocationDetails <TRequest, TResponse> call, TRequest req, Action customDisposeAction) where TRequest : class where TResponse : class { var asyncCall = new AsyncCall <TRequest, TResponse>(call); var asyncResult = asyncCall.UnaryCallAsync(req); var token = asyncCall.Details.Options.CancellationToken; if (token.CanBeCanceled) { token.Register(() => customDisposeAction()); return(new AsyncUnaryCall <TResponse>(asyncResult, asyncCall.ResponseHeadersAsync, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel)); } else { return(new AsyncUnaryCall <TResponse>(asyncResult, asyncCall.ResponseHeadersAsync, asyncCall.GetStatus, asyncCall.GetTrailers, () => { customDisposeAction(); asyncCall.Cancel(); })); } }
public static AsyncDuplexStreamingCall <TRequest, TResponse> AsyncDuplexStreamingCall <TRequest, TResponse>(CallInvocationDetails <TRequest, TResponse> call, Action customDisposeAction) where TRequest : class where TResponse : class { var asyncCall = new AsyncCall <TRequest, TResponse>(call); asyncCall.StartDuplexStreamingCall(); var requestStream = new ClientRequestStream <TRequest, TResponse>(asyncCall); var responseStream = new ClientResponseStream <TRequest, TResponse>(asyncCall); var token = asyncCall.Details.Options.CancellationToken; if (token.CanBeCanceled) { token.Register(() => customDisposeAction()); return(new AsyncDuplexStreamingCall <TRequest, TResponse>(requestStream, responseStream, asyncCall.ResponseHeadersAsync, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel)); } else { return(new AsyncDuplexStreamingCall <TRequest, TResponse>(requestStream, responseStream, asyncCall.ResponseHeadersAsync, asyncCall.GetStatus, asyncCall.GetTrailers, () => { asyncCall.Cancel(); customDisposeAction(); })); } }