question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ConfigureHttpClient in AddGrpcClient configuration

See original GitHub issue

What version of gRPC and what language are you using?

v2.36 C# .Net 5.0

What operating system (Linux, Windows,…) and version?

macOS 11.2.3

What runtime / compiler are you using (e.g. .NET Core SDK version dotnet --info)

SDK Version: 5.0.102 Runtime: Microsoft.NETCore.App 5.0.2

What did you do?

The way I configure the GrpcClient with the IHttpClientBuilder to manage authorization is as follows:

            services.AddGrpcClient<MyGrpcServiceClient>(options =>
            {
                options.Address = configureOptions.Address;
            })
            .ConfigureHttpClient(client =>
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token);
            });

What did you expect to see?

I expected the authorization header to be set, but it’s empty. This doesn’t seem to be executed anymore. I use this pattern also with .Net 3.1 projects. I have to admit, I can’t find this pattern in the docs, so I guess this pattern isn’t used very much.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tverbooncommented, Apr 1, 2021

@JamesNK The interceptor with CallCredentials doesn’t work on macOS, because it demands a secure connection. This is how I did get it to work on macOS.

services.AddSingleton<AuthorizationHeaderInterceptor>();
services.AddGrpcClient<MyGrpcServiceClient>(options =>
{
    options.Address = configureOptions.Address;
})
.AddInterceptor<AuthorizationHeaderInterceptor>();
public class AuthorizationHeaderInterceptor : Interceptor
{
    public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(
        TRequest request,
        ClientInterceptorContext<TRequest, TResponse> context,
        AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
    {
        var token = "Bearer ey...";
        var headers = new Metadata();
        headers.Add(new Metadata.Entry("Authorization", token));

        var newOptions = context.Options.WithHeaders(headers);

        var newContext = new ClientInterceptorContext<TRequest, TResponse>(
            context.Method,
            context.Host,
            newOptions);

        return base.AsyncUnaryCall(request, newContext, continuation);
    }
}
0reactions
ScottKanecommented, Aug 16, 2022

Also worth noting I’m using protobuf-net.Grpc, can I still use AddGrpcClient? I’m currently using GrpcChannel.FromAddress with grpc-web

Read more comments on GitHub >

github_iconTop Results From Across the Web

gRPC client factory integration in .NET
Provides a central location for configuring logical gRPC client instances. Manages the lifetime of the underlying HttpClientMessageHandler .
Read more >
HTTP Client Configuration
ConfigureHttpClient provides a callback to customize the SocketsHttpHandler settings used for proxying requests. This will be called each time a cluster is ...
Read more >
Registering a “typed” gRPC client with the ASP.NET Core ...
AddGrpcClient <GrpcService>();. At the moment I have the following code, which works fine: services.AddSingleton<IService, GrpcService>();.
Read more >
Optimally Configuring ASP.NET Core HttpClientFactory
In this post, I'm going to show how to optimally configure a HttpClient using the new HttpClientFactory API in ASP.NET Core 2.1.
Read more >
Configuring gRPC clients in .NET - Irina Dominte(Scurtu)
How to configure gRPC Clients in a global way. ... By default, the AddGrpcClient , configures the gRPC clients as being transient.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found