Registering a “typed” gRPC client with the ASP.NET Core DI container
I have a GrpcService class the takes in gRPC client in its constructor, like so:
public class GrpcService : IService
{
private readonly GrpcClient grpcClient;
public GrpcService(GrpcClient grpcClient)
{
this.grpcClient = grpcClient;
}
//...
}
I have registered the gRPC client in the Startup class by using the generic AddGrpcClient extension method:
services.AddGrpcClient<GrpcClient>(o =>
{
o.Address = new Uri("https://localhost:5001");
});
Now I want to inject the GrpcService class into a API controller. My question is; what's is the correct way to register the GrpcService in the Startup class?
I did try the following, which did not work:
services.AddGrpcClient<GrpcService>();
At the moment I have the following code, which works fine:
services.AddSingleton<IService, GrpcService>();
However, I'm not certain using a singleton is the correct lifetime to use, in particular as the gRPC client uses HttpClient internally?
from Recent Questions - Stack Overflow https://ift.tt/3g8PmUX
https://ift.tt/eA8V8J
Comments
Post a Comment