Advanced Configuration Options of gRPC Client
Learn the advanced configuration options that can be used on a gRPC client.
We'll cover the following
Advanced configuration gRPC options are available on both the server and client implementations. For example, a client might be placed in a location with low network bandwidth. When this happens, it would be good to limit the amount of data it can send and receive. On the other hand, maybe the opposite happens, and the client is placed onto a very fast network. In this situation, it may make sense not to restrict the amount of data it can handle.
Client-side configuration options can be applied to fine-tune the gRPC client for a specific scenario. They can be used to optimize performance, apply additional security, apply custom logic to the message-processing pipeline, and so on. The full list of the options available in a .NET implementation of a gRPC client is as follows.
HttpHandler
: If a .NET implementation of a gRPC client uses an HTTP handler object that implements theIHttpHandler
interface, this option allows us to override the default implementation of the handler. It should only be used in a narrow range of advanced usage scenarios.HttpClient
: This setting is an alternative to theHttpHandler
setting. It allows us to set a customIHttpClient
implementation for making gRPC calls. It's only used if we need to replace the default HTTP handling provided by the framework.DisposeHttpClient
: This option causes either the HTTP client or HTTP handler to be disposed of when the gRPC channel is disposed of. It's set to true by default.LoggerFactory
: This setting allows us to apply any custom logger factory inside the gRPC framework. We can also configure the logging level by using this setting.MaxSendMessageSize
: This option dictates the maximum size of a message that our gRPC client can generate. The number is provided in kilobytes. The default value is null, which makes the maximum message size unlimited.MaxReceiveMessageSize
: This setting dictates the maximum message size the client can receive from the server. The value is in bytes. The default value is 4 megabytes. Setting it to null removes the size limit.Credentials
: This setting adds channel credentials to the gRPC calls. It's essential for secure endpoints that require authentication.CompressionProviders
: This sets providers for message compression and decompression. Multiple providers can be used.ThrowOperationCanceledOnCancellation
: If this option is set to true, anOperationCanceledException
is thrown when the gRPC call is either canceled or its deadline is exceeded.UnsafeUseInsecureChannelCallCredentials
: This option enables sending authenticated credentials over an unsecured gRPC channel. It's false by default. It shouldn't be used outside of a development environment because it creates a security risk.MaxRetryAttempts
: This denotes the maximum retry attempts that can be made by a gRPC call before it's marked as failed. The default value is 5. Setting it to null removes the limit. It's overridden by theServiceConfig
setting.MaxRetryBufferSize
: This denotes the maximum size of the buffer that can be used to store sent messages during retries. The value is in bytes. The default value is 16 megabytes. If set to null, the buffer size becomes unlimited.MaxRetryBufferPerCallSize
: This denotes the maximum size of the buffer that can be used to store sent messages during retries for a single call. The value is in bytes. The default value is 1 megabyte. If set to null, the buffer size becomes unlimited.ServiceConfig
: This setting can be used to apply the detailed configuration of call retries.
We'll start with the baseline solution outlined in the code widget below. This setup includes a gRPC server and gRPC client without any custom options applied.
Get hands-on with 1200+ tech skills courses.