In this article, we will learn about the basics of gRPC, its usage, and finally, implement the Server and Client using .NET Core 6.0.
gRPC (Google Remote Procedure Calls) was initially created by Google, which has used a single general-purpose RPC infrastructure called Stubby to connect the large number of microservices running within and across its data centers for over a decade.
In March 2015, Google decided to build the next version of Stubby and make it open source and the result was gRPC.
- gRPC is a modern open-source high-performance Remote Procedure Call (RPC) framework.
- It implements APIs using HTTP/2 which can run in any environment
- gRPC has two parts
- gRPC Protocol - As mentioned above it uses HTTP/2 which provides a lot of advantages over traditional Http1.x
- Data Serialization - by default gRPC uses Protobuf for the serialization and as an intermediator between client and server.
- gRPC clients and servers intercommunicate using a variety of environments and machines.
- It supports many languages like Java, C#, Go, Ruby, and Python, check the full list here
- It supports pluggable auth, tracing, load balancing, and health checking.
- When we use microservice architecture and we use that for internal communication from one or more servers.
- gRPC is handy when performance is on high priority with low latency.
- It is useful when we require duplex communication between services with different types of data.
- gRPC is useful in the last mile of distributed computing to connect devices, mobile applications, and browsers to backend services.
REST has been one of the highly used API frameworks and with gRPC grabbing the limelight, let's see the comparison on various parameters.
|Request-Response Model||Only supports Request Response as based on HTTP/1.1||Supports All Types Of Streaming as based on HTTP/2|
|Payload Format||Serialization like JSON/XML||Strong Typing|
- With duplex communication, it supports bi-directional streaming with HTTP/2-based transport
- Performance is one of the talking points of gRPC and it is faster than REST and SOAP.
- gRPC services are highly efficient on wire and with a simple service definition framework.
- gRPC messages are lightweight compared to other types such as JSON.
- Client libraries supporting the 11 languages
- Browser support is very limited.
- Since it uses binary data which is not easily readable compared to JSON or XML.
- Visual Studio 2022
- .NET 6.0
- Open Visual Studio 2022 and create a new gRPC project with the name GrpcCoreService and select
.NET 6.0under the Framework option.
Review the default project folder structure.
Protos: contains all the gRPC server asset files, such as
- Services: Contains the implementation of the gRPC services.
appSettings.jsoncontains service the configuration and
Program.cscontains code that configures app behavior.
- Protos: contains all the gRPC server asset files, such as
- Add a Console App Project with the name GrpcClientApp and select the required configurations.
- Add the required packages to the client app project.
Install-Package Grpc.Net.Client Install-Package Google.Protobuf Install-Package Grpc.Tools
- Create a Protos folder and copy the
Protos\greet.protofile from the GrpcCoreService under this folder.
- Update the namespace inside the
greet.protofile to the project's namespace:
option csharp_namespace = "GrpcClientApp";
Finally, edit the GrpcClientApp.csproj project file:
<ItemGroup> <Protobuf Include="Protos\greet.proto" GrpcServices="Client" /> </ItemGroup>
- Once this is completed update the
Program.csfile to call the greeter service.
Make sure to use the port number mentioned in the
launchSettings.jsonfiles of the GrpcCoreService project.
- Set Up StartUp Project: Configure both projects as startup projects in the proper order.
- Run the project and review the output of both the gRPC service and the client in the Console window.
- gRPC Service Window:
- gRPC Client Window:
So far we have tested the default greeter service and client.
Now let's move ahead and set up a new service and client which consumes this new service.
- To start with add the
employee.protofile under the
Protosfolder and add the following code.
- Under the
Servicesfolder add the
EmployeeService.csand implement it as shown below.
- Inject this new service under the
- Copy the
employee.protofile from the GrpcCoreService and update the namespace:
option csharp_namespace = "GrpcClientApp";
- After that right click on the
employee.protoand click on Properties and set the gRPC Stub Classes to Client only
- And make sure the proto file is added in the GrpcClientApp.csproj project file:
<ItemGroup> <Protobuf Include="Protos\greet.proto" GrpcServices="Client" /> <Protobuf Include="Protos\employee.proto" GrpcServices="Client" /> </ItemGroup>
- Finally, update the
Program.csfile to call the
- Once you are done with changes, you can verify the folders and files under Solution Explorer.
- Run the project and search for the employee by passing the Employee ID in the Console Window
Check the entire source code here.
If you have any comments or suggestions, please leave them behind in the comments section below and If you've found a typo, a sentence that could be improved or anything else that should be updated on this blog post, please go directly to the blog repository and open a new pull request with your changes.