gRPC & ASP.NET Core 3.1: What’s gRPC ? (introduction)
What’s gRPC?
gRPC is an RPC (Remote procedure call) framework and not a Microsoft framework. It runs on HTTP/2 and it uses Protocol Buffers serialization format. gRPC provides bi-directional data transport and allows flow control (cancellation and waiting times). gRPC has been created by Google around 2003/2004 and they open sourced it in 2015. Many languages had implemented it except .NET, Finally Microsoft released a .NET version with ASP.NET Core 3.0 in September 2019.
In gRPC, a client application can directly call a method on a server application on a different machine as if it were a local object, making it easier for you to create distributed applications and services.
What are Protocol Buffers?
Protocol Buffer is a data structuring language and contract descriptor (services and entities), like WSDL for WCF. Current version of Protocol Buffer is proto 3, (proto 2 is obsolete) and files are named “.proto”.
A Protocol Buffer looks like this:
Protocol Buffer support many types (with their related type in C#)
Type | C# Type |
double | double |
float | float |
int32 | int |
int64 | long |
uint32 | uint |
uint64 | ulong |
sint32 | int |
sint64 | long |
fixed32 | uint |
fixed64 | ulong |
sfixed32 | int |
sfixed64 | long |
string | string |
bytes | ByteString |
Source: https://developers.google.com/protocol-buffers/docs/csharptutorial
What type of services does gRPC provide ?
gRPC provide four service types:
- Unary services: A client sends a single request to the server and gets a single response back
- Server streaming services: A client sends a request to the server and gets a stream to read a sequence of messages back
- Client streaming services: A client writes a sequence of messages and sends them to the server, again using a provided stream. Once the client has finished writing the messages, it waits for the server to read them and return its response
- Bidirectionnal streaming services: Both sides send a sequence of messages using a read-write stream. The two streams operate independently, so clients and servers can read and write in whatever order they like
Source: https://grpc.io/docs/guides/concepts/#overview
How does gRPC work ?
gRPC breaks REST API “codes”, there is no longer debates on how to write an Url, what verb to use what HttpStatus to manage. gRPC works like this:
- Uses always POST verb
- Return always (when the request is handled by gRPC) HTTP 200 OK
- Uses Content-Type “application/grpc”
- Uses classical Headers (like any HTTP Request)
- Uses Trailers (custom metadata)
- Return a grpc-status
- Data are transported in binary,then it’s hsard to debug
To learn more about gRPC over HTTP/2 you can go here: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md
gRPC is also not compatible with a browser, there are some workarounds, we’ll see that further is this serie of articles.
Implementation in .NET Core
There are a lot of packages, gRPC team has develop packages in C# to be able to consume gRPC services in C#, Microsoft reused them to make a .NET Core / ASP.NET Core version. The role of each packages is described on the picture below:
Conclusion
This article was only a quick introduction to gRPC and its concept. The Because the topic is huge I provided you many links to learn more about it. gRPC has also its strenghs and weaknesses, to learn more about it you read this article: https://docs.microsoft.com/en-us/aspnet/core/grpc/comparison?view=aspnetcore-3.1, Anyway in this serie of articles we’ll see what we can do with, what we can’t do with.
In the next article we’ll see how to create a gRPC service in ASP.NET Core 3.1