How to perform header propagation between Http to gRPC in Asp.Net Core

Viewed 957

I have a number of microservices built in ASP.Net Core WebAPI, each of which support both a REST HTTP and gRPC service for all API methods.

I need to propagate some headers between an HTTP Restful API call and an invoked-from-within gRPC call made to another ASP.Net Core WebAPI service such that the header can be used within the second service.

I also need to be able to propagate headers between a gRPC method service-A to gRPC method service-B and back.

Using Microsoft.AspNetCore.HeaderPropagation we are able to create if non-existent as well as propagate headers to downstream HttpClient calls but can't seem to do that same for a gRPC method invocation.

Any assistance would be greatly appreciated.

1 Answers

Strongly typed gRPC clients use HttpClient internally. You can use something similar:

var channel = GrpcChannel.ForAddress("<GrpcServerEndpoint>", new GrpcChannelOptions
{
    HttpClient = new HttpClient();
});

httpClient.DefaultRequestHeaders.Add("<headerToPropagate>", "<valueFromRequestHeader>");
Related