0

Looking at the DI supported for gRPC clients, I was having troubles figuring out how the implementation for named gRPC clients should be made.

The Grpc.Net.ClientFactory package offers such a functionality to register a named gRPC client. All examples I could find, however, directly inject a gRPC client into some class instead of creating it by a specified name with a factory.

There is a GrpcClientFactory class from the same package that contains such a function to create a gRPC client based on a given name, however, it is abstract and the default implementation is internal. Unlike the IHttpClientFactory it cannot be injected. My question is if it is necessary to create a GrpcClientFactory implementation myself in order to use the named gRPC clients functionality or perhaps am I missing something.

vsir
  • 349
  • 2
  • 12
  • 1
    *Every* type can be injected, not just interfaces. The `AddGrpcClient` extension method has overloads that accept a `name` parameter. If you check the [source code](https://github.com/grpc/grpc-dotnet/blob/master/src/Grpc.Net.ClientFactory/GrpcClientServiceExtensions.cs) you'll see that calling `AddGrpcClient` also registers a `GrpcClientFactory` – Panagiotis Kanavos Jun 02 '20 at 13:59
  • I did check that source code and that is how I expected it to work but I was getting some other DI error. Now that you pointed it out I checked it again and turned out that I was using some mistaken type instead of the correct client. Indeed, it works as expected. Thank you! – vsir Jun 02 '20 at 14:20

0 Answers0