Skip to main content

# Client interceptors

If we need to create a client we have a few methods that we can override:

- BlockingUnaryCall: Intercepts a blocking invocation of an unary RPC.
- AsyncUnaryCall: Intercepts an asynchronous invocation of an unary RPC.
- AsyncClientStreamingCall: Intercepts an asynchronous invocation of a client-streaming RPC.
- AsyncServerStreamingCall: Intercepts an asynchronous invocation of a server-streaming RPC.
- AsyncDuplexStreamingCall: Intercepts an asynchronous invocation of a bidirectional-streaming RPC.

# Configuring a client interceptor

To We can configure several interceptors, and these are executed in reverse order:

Interceptors order

    var invoker = channel
        .Intercept(new ClientTokenInterceptor())
        .Intercept(new ClientMonitoringInterceptor())
        .Intercept(new ClientLoggerInterceptor());

When it comes to the actual execution order, that happens in reverse:

1. `ClientLoggerInterceptor` - was last
2. `ClientMonitoringInterceptor`
3. `ClientTokenInterceptor` - was first

# Configure interceptors on clients

client interceptors aren't configured with `GrpcChannelOptions`. Instead, client interceptors are configured using the Intercept extension method with a channel. This extension method is in the `Grpc.Core.Interceptors` namespace.

    static async Task Main(string[] args)
    {
       var channel = GrpcChannel.ForAddress("https://localhost:5001");
       var callInvoker = channel.Intercept(new LoggingInterceptor());
       var client = new Greeter.GreeterClient(callInvoker);

    }