protobuf-net / protobuf-net.Grpc

GRPC bindings for protobuf-net and grpc-dotnet
Other
846 stars 106 forks source link

Question: Access metadata from client #292

Closed mgkmarco closed 1 year ago

mgkmarco commented 1 year ago

Hi there, Is it possible to access metadata like headers and/or trailers from a client, other than specifying return values like AsyncUnaryCall<T> in the service interface?

Thanks

mgravell commented 1 year ago

Yes, you can do this via CallContext. I'll try to find examples later (on mobile)

mgkmarco commented 1 year ago

Hey Marc thanks a lot for the pointer. Figured it out with something along the lines of:

public TestService(IGreeterService greeterClient, ILoggerFactory loggerFactory)
{
    _greeterClient = greeterClient;
    _logger = loggerFactory.CreateLogger<TestService>();
}

public async Task Unary()
{
    var callContext = new CallContext(flags: CallContextFlags.CaptureMetadata);

    var call = await _greeterClient.SayHelloAsync
        (
            new HelloRequest { Name = "GreeterClient" }, callContext
        );

    var headers = await callContext.ResponseHeadersAsync();

    foreach(var header in headers)
    {
        Console.WriteLine($"Key: {header.Key} - Value: {header.Value}");
    }
}

Thanks

mgravell commented 1 year ago

Yup: exactly that!