gpt4 book ai didi

c# - 为所有对 gRPC 服务的调用添加元数据

转载 作者:行者123 更新时间:2023-12-05 03:46:35 30 4
gpt4 key购买 nike

如何为对服务的所有调用添加元数据?我知道如何为特定调用发送元数据,但我似乎无法找到如何为所有调用添加标准元数据。

我尝试使用客户端拦截器在此处添加元数据,但似乎无法在此处操作上下文的 header 。

我在 .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 个客户端调用方法;​​AsyncClientStreamingCallAsyncDuplexStreamingCallAsyncServerStreamingCallAsyncUnaryCallBlockingUnaryCall)

关于c# - 为所有对 gRPC 服务的调用添加元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65220041/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com