Hi Benjamin, This is fantastic - thanks very much. I'm going to take a look in more detail tomorrow, but should be able to figure it out from here I think.
Good point about catching exceptions on the ResponseStream also - for the auth use case in particular however, I think I would only be interested in retrying the initial call right? Cheers, Mark On Monday, 18 February 2019 14:11:33 UTC+1, Benjamin Krämer wrote: > > 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 > > <https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fopentracing-contrib%2Fcsharp-grpc%2Fblob%2Fmaster%2Fsrc%2FOpenTracing.Contrib.Grpc%2FStreaming%2FTracingAsyncStreamReader.cs&sa=D&sntz=1&usg=AFQjCNEVNNVmSIy9A-bqul-fE--lYyKayA> > > 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/6729bdfc-75f7-4d13-af5c-7ee3bd360110%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
