.Net Core 3.1 has been launched last month and one of the major features of it is gRPC. Let’s have a detailed discussion on how to get started with gRPC using C# and .Net Core. Let’s divide our article into the following sections,
What is gRPC
According to official blog “GRPC is a open source high performance RPC framework that can run in any environment, it can efficiently connect services across data centers with support of load balancing, tracing and authentication.” Mostly gRPC is used for connecting polyglot services in microservices architecture, connecting mobile devices, browser clients to the services
Grpc has two parts the — gRPC protocol and data serialization. By default gRPC uses Protobuf for the serialization and protocol is based on Http2 which provides a lot of advantages over traditional Http1.x
GRPC vs REST
REST has been one of the concrete pillars of the web programming recently, but the emergence of the gRPC has put some challenge before REST. The below tables explain the differences between them,
|
Parameter
|
REST
|
gRPC
|
|
Serialization
|
JSON/XML
|
protobuf
|
|
Protocol
|
HTTP/1.1
|
HTTP/2.0
|
|
Browser Support
|
Yes
|
No
|
|
Data Exchange
|
Resources and Verbs
|
Messages
|
|
Request-Response Model
|
Only supports Request Response as based on Http/1.1
|
Supports All Types Of Streaming as based on Http/2
|
|
Payload Exchange Format
|
Serialization like JSON/XML
|
Strong Typing
|
Creating GRPC Service
As we are good with the basics, for now, let’s move and see how we can create the Service, and later let’s see how we can consume that service into the client. To get started with the service we need to have a few initial things as follows
- Visual Studio 2019
- .NET Core SDK 3.1
WCF to GRPC
One of the alternatives as recommended by Microsoft is to use the Grpc instead of the WCF services in the future projects. In this section, let us see how we can relate our understanding of the WCF to the Grpc concepts that we have right now,
|
WCF
|
Grpc
|
|
ServiceContract
|
Service in ProtoFile
|
|
OperationContract
|
RPC Method in ProtoFile
|
|
DataContract
|
Message in the ProtoFile
|
|
FaultContract
|
Richer error Model
|
|
Request-Reply
|
Unary Streaming
|
|
Duplex
|
Bidirectional Streaming
|
Performance difference between HTTP2 and HTTP1.1
Protocol Buffers/gRPC Integration Into .NET Build
Understanding ProtoBuff
Everything about Protocol Buffers
By Default, GRPC uses Protocol buffers for structuring and serializing data let’s try to explore What, Why, and How we are going to use the Protobuff for the development of the GRPC services.
What are Protocol Buffers?
Protocol buffers are a flexible, efficient, and structured mechanism for serializing the data when compared to XML it is smaller, faster, and simpler, We structure how we want our data and then which is then converted specially generated code which can be read and write using data streams and by using a variety of language like C#, Java or Python.
Why Protocol Buffers?
Now time for why protocol buffers are needed despite having various things like XML or JSON. ProtoBuff provides many advantages over these traditional data exchange methods like below:
- Simpler
- 3 to 10 times smaller
- 20 to 100 times faster
- Less ambiguous
- Generates stub which are normal classes and easy to understand
When we compare the Protobuff payload and XML Payload we can see the significant changes and advantages of using protobuff over XML
Once we are done with the Message and data structure that will be transferred to and from in the network now it’s time to design an interface that will be called by clients to access these message types.
- service MoviesService{
- rpc GetMovie(queryparams) returns (Movie){};
- }
Grpc is Mostly based on the idea of defining the service which will accept the parameter and sends back the data in for the response, By Default Grpc uses Protobuff IDL which is an Interface definition language to specify the service definition as well the message formats which will be sent over to the network.
There are four ways we can define our Service methods
1. Unary RPC
rpc GetMovie(queryparams) returns (Movie){};
It’s a typical client-server call where a request is sent to the server and the server respond with the Response
GitHub link GrpcServiceCore
2. Server Streaming RPC
rpc GetMovie(queryparams) returns (stream Movie){};
Here client sends a request to the server and the server responds by sending streams of the data back client needs to make sure it reads the data till the server streams it back.
GitHub link grpcServerSideStreaming
3. Client streaming RPC
rpc GetMovie(Stream Data) returns (Movie){};
In this client sends the stream of the data to the server and waits for the server to respond to it, In this sever reads all the data and returns the response accordingly, Grpc guarantees the ordering of the data which will be sent to the server
GitHub link grpcClientSideStreaming
4.Bidirectional Streaming
rpc GetMovie(Stream Data) returns (stream Movie){};
In this, both client and server sends the stream of the data to each other using Read-write stream
GitHub Link grpcBiDirectionalService
Be First to Comment