grpc / grpc/grpc-dotnet

Header value duplication when double-registering?

Open
#2,557 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C#
Stars
4.5k
Forks
836
Avg merge
6d 3h
Merged PRs (30d)
7

Description

### What version of gRPC and what language are you using?

Grpc.Net.ClientFactory 2.66.0
Google.Protobuf 3.25.2

### What operating system (Linux, Windows,...) and version?

Windows

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

.net8

### What did you do?
If possible, provide a recipe for reproducing the error. Try being specific and include code snippets if helpful.

We have the following `IServiceCollection` extension methods:

```
AddWrappedStreamGrpcClient(IServiceCollection serviceCollection) {
serviceCollection
.AddGrpcClient()
.AddCustomCallCredentials();
serviceCollection.AddTransient
}
----
AddWrappedQeuryGrpcClient(IServiceCollection serviceCollection) {
serviceCollection
.AddGrpcClient()
.AddCustomCallCredentials();
serviceCollection.AddTransient();
}
```

Where both wrappers take `MyService.MyClient` into their constructor.

### What did you expect to see?

When doing the following:

```
builder.Services.AddWrappedStreamGrpcClient().AddWrappedQeuryGrpcClient()
```

I'd expect both of my wrapper classes to get separate instances of grpc client, as far as the classes are concerned.

### What did you see instead?

Due to double-instantiation, request header is somehow doubled. And not two headers, but two values in a single header:

```
Authorization: Bearer ey<...>, Bearer ey<...>
```
### Anything else we should know about your project / environment?

`.AddCustomCallCredentials()` doesn't do anything outlandish, and I even tried to prevent it from adding metadata again:

```
foreach (var entry in metadata)
{
if (entry.Key == "Authorization") return;
}

var token = (await tokenHelper.GetTokens()).AccessToken.Value;
if (string.IsNullOrEmpty(token))
{
throw new ArgumentNullException(token);
}

metadata.Add("Authorization", $"Bearer {token}");
}

```

I suppose I could redo the interface into:
```
AddSystemPrerequisites(); // register client + auth here
AddWrappedStreamGrpcClient();
AddWrappedQeuryGrpcClient();
```

but I was wondering if this behavior is intented.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the two IServiceCollection extension registrations on .NET 8 with Grpc.Net.ClientFactory 2.66.0 and the shown AddCustomCallCredentials callback. Inspect the AddGrpcClient and AddCustomCallCredentials behavior when MyService.MyClient is registered twice; done means establishing whether duplicate registration and combined Authorization values are intended and documenting the result or identifying the relevant fix location.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.