evhfla
evhfla

Reputation: 151

Strategies for calling grpc service from a webapi in .net Core

I have a rest/web API in .Net Core that I am using as an aggregation service to call some underlying gRPC services also in .Net core. For the WEB API client, I am creating the channel once as a singleton on startup and injecting it where I need it using dependency injection. I am also thinking of creating the clients to the grpc services on startup and then injecting them. Is creating the channel and the clients on Startup the correct strategy or should I create and close the channel and clients on every requests of the web API? Furthermore, if creating the channel once is the correct strategy how do I ensure that the channel is closed on shutdown....Both the web API/rest service and the grpc services will be running on a Kubernetes Cluster as docker containers.

Upvotes: 1

Views: 1002

Answers (1)

Jan Tattermusch
Jan Tattermusch

Reputation: 1663

You definitely shouldn't create a new channel for every call you make. gRPC channels are supposed to be long-lived and you'll generally get the best performance and resource utilization if you create a channel once and then keep invoking subsequent calls on that channel. The exact scope at which you create the channel depends on your application - but creating the channel only once at application startup is one of the possible approaches (also note that TCP/IP connections are created lazily - a new connection won't be created until you start a first call on that channel).

Upvotes: 1

Related Questions