.Net Generic Host implementation for gRPC
In .NetCore Microsoft provides us with means for loading configuration file through Options patter GrpcHost support the same feature you can use: "hostsettings.json" and extend it or use additional configuration files which ever suite your needs.
The existing configuration covers:
- Configuring the host
- Configuring logging
- Configure client caching
Here you can setup host address and port, if you decide to ommit these values by default grpc server address will be: 0.0.0.0:80 which is probably fine if you are planning to host your grpc server in docker containers, otherwise you will want to change the default to something more appropriate.
"HostOptions": {
"Host": "localhost",
"Port": 5000
},
TODO: Add supprot for configuring grpc timeouts (deadline).
Provides you with means to filter request/response properties are just allow everything to be logged. GrpcHost uses Mircorosfot.Extension.Logging.ILogger as a logging abstraction but the logging implementation that's actually being used is Serilog Console or Splunk sink. In the future I might revisit this and remove the Serilog dependency, but logging will still be structured, so traditional loggers will not offer you with best results.
TODO - Add more details and examples on how logging configuration works.
Since grpc channels need to be reused as much as possible, grpc host offers client cacheing. In "ChannelOptions" you can specify the list of grpc clients that the server might consume while processing requests.
"ChannelOptions": [{
"ServiceName": "CustomerService",
"Host": "localhost",
"Port": 5000
},
{
"ServiceName": "ProductService",
"Host": "localhost",
"Port": 5005
}]
TODO: Currently there is no expiration for cached client, so in case that you need to update the client addres you will need to restart the server, this will be fixed in future, by adding the flag that will be used to invalidate the cached clients.
In Grpc.Core v1.17 we got the health check api that we can easily implement GrpcHost offer additional means of overriding the healthcheck functionality. If you are satisfied with what happens out of the box then you don't have to do anything, otherwise you can implement: IHealthCheckOverrided interface. The interface conatins only one method: "IsHealthy" that has no input parameters and returns bool value. If return is "true" health status will be "Serving", otherwise it'll be "NotServing". You will need to register the interface in DI configuration and you are free to add any additional dependencies that you might need for you healthcheck logic.
TODO