- gRPC & ASP.NET Core 3.1: What’s gRPC ? (introduction)
- gRPC & ASP.NET Core 3.1: How to create a gRPC service ?
- gRPC & ASP.NET Core 3.1: How to create a gRPC client ?
- gRPC & ASP.NET Core 3.1: What’s gRPC-web & how to create a gRPC-web service ?
- gRPC & ASP.NET Core 3.1: How to create a gRPC-web client ? Examples with Angular 8 and HttpClient
- gRPC & ASP.NET Core 3.1: Global error Handling in gRPC & gRPC status codes
- gRPC & ASP.NET Core 3.1: Resiliency with Polly
- gRPC & ASP.NET Core 3.1: Audit, logging, tracing & metrics
- gRPC & ASP.NET Core 3.1: Application Insights
- gRPC & ASP.NET Core 3.1: Manage Authentication and Authorization
- gRPC & ASP.NET Core 3.1: Inputs validation
- gRPC & ASP.NET Core 3.1: CRUD operations with Entity framework
- gRPC & ASP.NET Core 3.1: Healthchecks
- gRPC & ASP.NET Core 3.1: Versionning
- gRPC & ASP.NET Core 3.1: Unit testing
- gRPC & ASP.NET Core 3.1: Integration testing
- gRPC & ASP.NET Core 3.1: Upload / download files
- gRPC & ASP.NET Core 3.1: Caching
- gRPC & ASP.NET Core 3.1: Building streaming services with gRPC & Worker Services
Creating a gRPC service
You’ll need to open Visual Studio 2019 and select “gRPC service” in “Create a new project” screen:
And then name your project:
Anatomy of a gRPC service
We find the usual components of an ASP.NET Core project like:
- The launchSettings
- The appsettings.json files
- The Program.cs
- The Startup.cs
In a gRPC service project we discover:
- A Services directory with a gRPC service,that contains a sample named GreeterService inheriting from a self-generated class named Greeter.GreeterBase, with its constructor and its method (s). Note that the built-in dependency injection of ASP.NET Core is available with a gRPC service.
- A Protos directory containing the Protocol Buffers, in other words the service contract.
The startup.cs looks like this by default:
How does a gRPC service look like?
As mentioned above, it is above all a C# file with its service class inheriting from a self-generated class, a constructor and its service methods. Each service method takes as parameter a message type input model as defined in the .proto file and a ServerCallContext type context.
Example of a unary service method:
The .csproj looks like:
How to create a new grpc service after creating the project?
First download the Grpc.Tools package:
Install-Package Grpc.Tools -Version 2.27.0
This will allow you to generate the classes of your gRPC service defined in your Protocol Buffers files that you will create manually. These classes will be autogenerated when you build your solution in Visual Studio 2019.
Then create a new .proto file:
You can now create your operations and contracts (message):
Once done, you need to compile your app, autogenerated classes will be available and you will be able to write your service. Behind the scene, the Protocol buffer compiler (protoc) will generate C# classes.
protoc is a compiler for protocol buffers definitions files. It can generate C++, Java, Python and many other sources code for the classes defined in a proto file.
If all goes well classes will be generated in this directory: ..\obj\Debug\netcoreapp3.1:
Now you are able to write your services methods:
Don’t forget to register your new service in the endpoints middleware:
Before running your service
dotnet dev-certs https --trust
After all these steps you should be able to run your app:
gRPC & Azure App Service
gRPC in ASP.NET Core is currently not supported on Azure App Service or IIS. The HTTP/2 implementation of Http.Sys does not support the HTTP response end headers on which gRPC relies. The original announcement from Microsoft can be found here: https://docs.microsoft.com/en-us/aspnet/core/tutorials/grpc/grpc-start?view=aspnetcore-3.0&tabs=visual-studio#grpc-not-supported-on-azure-app-service
gRPC doesn’t support browsers. The grpc-web client library for SPAs such as Angular, React etc… is not compatible because at the moment browsers do not support the level of control required on web requests to support a gRPC client. For more information, you can refer to this page here: https://docs.microsoft.com/en-us/aspnet/core/grpc/comparison?view=aspnetcore-3.0#limited-browser-support
If you still want to use grpc-web, this is still possible but you will need to use a proxy such as Envoy to transform HTTP/1 requests into HTTP/2 and vice versa: