Implementing interceptors and handling faults
gRPC interceptors are a way to perform additional processing during requests and responses and they can be injected at the client or service. They are often used for logging, monitoring, and validation.
Adding a client-side interceptor
Let’s add a client-side gRPC interceptor for logging:
- In the
Northwind.Grpc.Client.Mvc
project, add a new folder namedInterceptors
. - In the
Interceptors
folder, add a new class file namedClientLoggingInterceptor.cs
, and then add statements to define an interceptor, as shown in the following code:using Grpc.Core.Interceptors; // To use Interceptor and so on. using Grpc.Core; // To use AsyncUnaryCall<T>. namespace Northwind.Grpc.Client.Mvc.Interceptors; public class ClientLoggingInterceptor : Interceptor { private readonly ILogger _logger; public ClientLoggingInterceptor(ILoggerFactory loggerFactory) { _logger = loggerFactory.CreateLogger<ClientLoggingInterceptor...