Hi Mark, the problem should be that you are directly returning the Task returned by continuation instead of awaiting it. Therefore, it's not unwrapped and not even checked at this point.
You can see an example of how to intercept it (also using async/await) in my OpenTracing interceptor for gRPC: https://github.com/opentracing-contrib/csharp-grpc/tree/master/src/OpenTracing.Contrib.Grpc The `Interceptors` folder contains the inherited classes, but those are simple calling to the `Handler` classes in the `Handler` folder. In addition, you have to make sure, that the exception could also occur on the `ResponseStream` and not just on the initial call. https://github.com/opentracing-contrib/csharp-grpc/blob/master/src/OpenTracing.Contrib.Grpc/Handler/InterceptedClientHandler.cs#L122 I wrapped the stream here so that I can catch the exception here: https://github.com/opentracing-contrib/csharp-grpc/blob/master/src/OpenTracing.Contrib.Grpc/Streaming/TracingAsyncStreamReader.cs I will have a look at your exact situation and will write an example since I have to implement OAuth on gRPC anyway. Benjamin Am Freitag, 15. Februar 2019 10:58:41 UTC+1 schrieb Mark Nuttall-Smith: > > > Hi, > I'm trying to add a C# interceptor that will handle refreshing an access > token if it was expired. > > Here is my current code: > > public override AsyncServerStreamingCall<TResponse> > AsyncServerStreamingCall<TRequest, TResponse>( > TRequest request, ClientInterceptorContext<TRequest, TResponse > > context, > AsyncServerStreamingCallContinuation<TRequest, TResponse> > continuation) > { > var cancellationToken = context.Options.CancellationToken; > > > var token = Task.Run(() => GetAccessTokenAsync( > cancellationToken), cancellationToken).Result; > > > if (token.IsMissing()) > if (Task.Run(() => RefreshTokensAsync(cancellationToken), > cancellationToken).Result == false) > _logger.Warn("Unable to refresh access token"); > > > AddOrUpdateHeader(context.Options.Headers, > CreateBearerTokenHeader(token)); > > > try > { > return continuation(request, context); > } > catch (RpcException e) when (e.StatusCode == StatusCode. > Unauthenticated) > { > if (Task.Run(() => RefreshTokensAsync(cancellationToken), > cancellationToken).Result == false) > _logger.Warn("Unable to refresh access token"); > > > AddOrUpdateHeader(context.Options.Headers, > CreateBearerTokenHeader(AccessToken)); > > > return continuation(request, context); > } > } > > What is surprising to me is that the RpcException that is thrown is never > caught by the exception handler (it is caught by the client however). > > Could anyone suggest what I'm missing? Or point to an example of a server > streaming retry handler in C#. > > Thanks, > Mark > -- You received this message because you are subscribed to the Google Groups "grpc.io" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/grpc-io. To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/920d4d1f-99d1-4484-8c70-3240b9de8b60%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
