为所有对 gRPC 服务的调用添加元数据
Add metadata for all calls to gRPC service
如何为对服务的所有调用添加元数据?我知道如何为特定调用发送元数据,但我似乎无法找到如何为所有调用添加标准元数据。
我尝试使用客户端拦截器在此处添加元数据,但似乎无法为此处的上下文操作 headers。
我在 .net 框架而不是 .net 核心中使用 gRPC。
如果这是 客户端 调用(而不是服务器调用),您应该能够在拦截器中通过覆盖客户端调用来执行此操作 - 例如:
public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(ClientInterceptorContext<TRequest, TResponse> context, AsyncClientStreamingCallContinuation<TRequest, TResponse> continuation)
{
Metadata newMetadata = // your logic here
context = new ClientInterceptorContext<TRequest, TResponse>(context.Method,
context.Host, context.Options.WithHeaders(newMetadata));
return base.AsyncClientStreamingCall(context, continuation);
}
(您需要覆盖所有 5 个客户端调用方法;AsyncClientStreamingCall
、AsyncDuplexStreamingCall
、AsyncServerStreamingCall
、AsyncUnaryCall
和 BlockingUnaryCall
)
如何为对服务的所有调用添加元数据?我知道如何为特定调用发送元数据,但我似乎无法找到如何为所有调用添加标准元数据。
我尝试使用客户端拦截器在此处添加元数据,但似乎无法为此处的上下文操作 headers。
我在 .net 框架而不是 .net 核心中使用 gRPC。
如果这是 客户端 调用(而不是服务器调用),您应该能够在拦截器中通过覆盖客户端调用来执行此操作 - 例如:
public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(ClientInterceptorContext<TRequest, TResponse> context, AsyncClientStreamingCallContinuation<TRequest, TResponse> continuation)
{
Metadata newMetadata = // your logic here
context = new ClientInterceptorContext<TRequest, TResponse>(context.Method,
context.Host, context.Options.WithHeaders(newMetadata));
return base.AsyncClientStreamingCall(context, continuation);
}
(您需要覆盖所有 5 个客户端调用方法;AsyncClientStreamingCall
、AsyncDuplexStreamingCall
、AsyncServerStreamingCall
、AsyncUnaryCall
和 BlockingUnaryCall
)