ASPCoreGRPCSample
ASPCoreGRPCSample copied to clipboard
Example of creating services using gRPC, ASP.NET Core and Node.js
ASPCoreGRPCSample
Sample solution to explore gRPC integration with .NET Core for both client and server.
- Shared proto files in the Protos folder define the contract for the example services.
- Different examples of client-server and server-server communication
- Client connection examples with the new managed Grpc.Net.Client package that uses
HttpClientandHttpClientFactory, as well as the existingGrpc.Corepackage that users unmanaged C code underneath. - HTTPS certificates for development locally and with docker
- Usage of
Grpc.Corechannels when client certificates are expected by the server in mutual validation scenarios
Created using preview7 of ASP.NET Core 3.0. The code is subject to change before the official 3.0 release, in particular
Grpc.Net.Client(which should hopefully allow client SslCredentials for mutual authentication)
Getting started
Start by cloning the repo. Specific instructions depend on whether you want to use your local machine or docker.
Local machine
- Either build the solution or manually
dotnet restore && dotnet buildeach of the .NET projects - Run
npm installfrom the /Products folder - Run the
generate_certificatesscript from the /Products/scripts folder - Start the Orders .NET service by running
dotnet runfrom the /Orders folder - Start the Shippings .NET service by running
dotnet runfrom the /Shippings folder - Start the Products Node.js service by running
npm startfrom the /Products folder - Start the client .NET console app by running
dotnet runfrom the /Client folder
Docker
- Run
docker-compose up ordersfrom the root folder - Run
docker-compose up shippingsfrom the root folder - Run
docker-compose up productsfrom the root folder - Run
docker-compose build client && docker-compose run --rm clientfrom the root folder- alternatively run the client locally with
dotnet runfrom the Client folder
- alternatively run the client locally with
If you dont care about seeing all the server logs in the same console window, you can also run docker-compose up products, shippings, orders
Client
Sample .NET Core console application that sends requests to the Orders and Products services.
- Requests to the Orders .NET service are sent with the new
HttpClientbased Grpc.Net.Client package. HTTPS development certificates are automatically used (and shared with the docker containers as perdocker-compose.override.yml) - Requests to the Products Node service can be send with:
HttpClientbased client, as long as the server does not expect client certificates to be provided within the request for mutual authentication.Grpc.CoreChannel withSslCredentials
Orders
Sample ASP.NET Core application that implements the service defined by orders.proto.
- Will internally call Shipping as part of the sample implementation using a client managed by
HttpClientFactorty.
Shippings
Sample ASP.NET Core application that implements the service defined by shippings.proto.
Products
Sample Node.js application that implements the service defined by products.proto.
- Provides a script to generate SSL certificates for both server and client inside the /scripts folder.
- When starting the server, client certificates will be enforced or not based on the
checkClientCertificateparameter supplied togrpc.ServerCredentials.createSsl. Client needs to be adjusted accordingly to use theHttpClientbased one or theGrpc.Coreone.