Skip to content

InvalidOperationException when configuring gRPC client (Grpc.Net.ClientFactory) #2158

@jviau

Description

@jviau

What version of gRPC and what language are you using?

C# / .NET: Grpc.Net.ClientFactory/2.53.0

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

Applicable to all OS/versions

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

dotnet 7

What did you do?

If possible, provide a recipe for reproducing the error. Try being specific and include code snippets if helpful.

It appears any extension method from GrpcHttpClientBuilderExtensions do not work when using .AddGrpcClient<T>() (and NOT .AddGrpcClient<T>(Action<GrpcClientFactoryOptions>)). These methods will always throw InvalidOperationException from ValidateGrpcClient.

The reason being without using the overload which includes Action<GrpcClientFactoryOptions>, the service ConfigureNamedOptions<GrpcClientFactoryOptions> is never added, and so the validation check will always fail.

The fix should be as simple as changing .AddGrpcClient<T>() to call the action-accepting overload. IE: .AddGrpcClient(_ => { }).

What did you expect to see?

Able to use .AddGrpcClient<T>().AddInterceptor(...)

What did you see instead?

InvalidOperationException thrown from AddInterceptor call.

Make sure you include information that can help us debug (full error message, exception listing, stack trace, logs).

See TROUBLESHOOTING.md for how to diagnose problems better.

Anything else we should know about your project / environment?

This is easily worked around for end users by using the other overload. However, my situation is I am adding an SDK extension to the Azure Functions isolated dotnet worker, and so this workaround needs to be passed on to our customers, which is not ideal.

Side note

Also consider updating ValidateGrpcClient to use [CallerMemberName] string? caller = null instead of hardcoding nameof(AddInteceptor)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions