This content originally appeared on DEV Community and was authored by Prasanna
As developers, while building an API applications we find ourself between various choices of frameworks like REST, SOAP, GraphQL, HTTP API and more. It can be difficult to for a developer to choose the correct one that caters the needs of the application.
In this post, I'd like to introduce you to gRPC, which is one of the newer approach. gRPC supports many languages like C#/.Net, C++, Dart, Go, Java and other popular languages. Today we are focusing on how well gRPC integrates with ASP.Net Core.
By the end of this post, you'll have a basic understanding of what gRPC is, how it works with ASP.Net Core and whether it is right for your application or not. We will implement a Client/Server application, you can find the full working code in the Git repository, linked below.
Let's get started...
What is gRPC?
gRPC stands for google Remote Procedure Calls. It was initially designed by Google to make distributed applications more manageable for both Client
and Server
. gRPC takes a refreshed look on the old RPC design by making it interoperable and efficient using Protocol Buffers and HTTP/2. The gRPC with Protocol Buffers is equivalent of JSON or XML with REST APIs. The HTTP/2 specification was published in 2015 and improved on the 20 year old HTTP/1.1 design.
What is Protocol Buffers?
gRPC supports multiple serialization formats, but the most commonly used is Protocol Buffers. It is an open-source serialization structure specifically designed for efficiency and optimization. Data parsing with Protocol Buffers uses less CPU as the data is represented in a binary format which allows to minimize the size of encoded messages.
gRPC & .Net Core
From the Microsoft side the .NET team have been working for bringing gRPC to the .NET community. Earlier you had a basic library which wasn't good as other libraries nor you had any project template helping you to set up your application with gRPC. Now we have better library and better support.
We’ll now create a gRPC application for both a Server
and a Client
. While creating the gRPC application we will try to cater real life requirements as many as possible. For that we will create two independent applications one for the Client & one for the Server. We will send nested data from the Client
including integer, string and datetime and also receive the response from the Server
and process it.
gRPC Setup
For the Server
we will create a new AspNetCore Web App and add following packages. You can use any other template as you wish.
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.22.1" />
<PackageReference Include="Grpc.AspNetCore" Version="2.51.0" />
<PackageReference Include="Grpc.AspNetCore.Web" Version="2.51.0" />
<PackageReference Include="Grpc.Tools" Version="2.52.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
For the Client
we will create a console application and add following packages.
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.22.1" />
<PackageReference Include="Grpc.Net.Client" Version="2.51.0" />
<PackageReference Include="Grpc.Net.Client.Web" Version="2.51.0" />
<PackageReference Include="Grpc.Tools" Version="2.52.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
For the both projects add a new Protos
folder and create a new empty file named company.proto.
We’ll use the Proto Buffers to map out our gRPC service, a request, and a response. We will create a Company & Employee nested structure and a container for the response.
syntax = "proto3";
import "google/protobuf/timestamp.proto";
option csharp_namespace = "GrpcServer";
// The Company service definition.
service Company {
// Sends a greeting
rpc PostCompanyWithEmployees (CompanyModel) returns (Response);
}
// The request message containing the companies's details with employees.
message CompanyModel {
int32 companyId = 1;
string comapnyName = 2;
repeated EmployeeModel employees = 3;
}
// The request message containing the employee's details.
message EmployeeModel {
int32 empId = 1;
string empName = 2;
int32 companyId = 3;
google.protobuf.Timestamp birthDate = 4;
}
// The response message containing the response status.
message Response {
int32 status = 1;
}
Now, just save and build the application. Following line will get added .csproj
file.
<ItemGroup>
<Protobuf Include="Protos\company.proto" GrpcServices="Server" />
</ItemGroup>
We should notice the GrpcServices attribute. It tells which side the application will be on, Server
or Client
. In above case, it is showing as Server
where as in the other application it will be as Client
. In case of where the Server
and Client
both are in same project it will be as Server,Client
.
gRPC Service
In the Client
application, open the program.cs
file and create a method for adding the Company & Employee details. We will call this method from the Main()
function.
Here we will create a gRPC channel with gRPC Server's remote URL that will communicate with the gRPC server. After that we will have to create Employee
and Company
objects to pass it with the service.
var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions
{
HttpHandler = new GrpcWebHandler(new HttpClientHandler())
});
var readerClient = new Company.CompanyClient(channel);
//Creating new employees
var employees = new EmployeeModel[]
{
new EmployeeModel
{
EmpId = 1,
EmpName = "Rama Bapat",
BirthDate = Timestamp.FromDateTime(DateTime.UtcNow.AddYears(-24).AddMonths(-4)),
CompanyId = 1
},
new EmployeeModel
{
EmpId = 2,
EmpName = "Krishna Nene",
BirthDate = Timestamp.FromDateTime(DateTime.UtcNow.AddYears(-22).AddMonths(-7)),
CompanyId = 1
}
};
//Creating new company
var company = new CompanyModel
{
CompanyId = 1,
ComapnyName = "Patankar Khauwale"
};
//Adding employees to company model
company.Employees.Add(employees);
//Calling the server channel
var responseData = await readerClient.PostCompanyWithEmployeesAsync(company);
if (responseData.Status == 1)
{
Console.WriteLine("Company & Employees added Successfully.");
}
else
{
Console.WriteLine("Company & Employees could not be added, please try again.");
}
Now in the Server
application, create a method with name PostCompanyWithEmployees
and expecting CompanyModel
as input with gRPC.Respose
as return type.
public override Task<Response> PostCompanyWithEmployees(CompanyModel request, ServerCallContext context)
{
try
{
//Add your database/API call here to save the Employee & Company details.
//After successful post call return the Status as 1 else 0.
return Task.FromResult(new Response
{
Status = 1
});
}
catch
{
return Task.FromResult(new Response
{
Status = 0
});
}
}
Once all is done, we will run both the applications and call the Server and we should get a response from the it. In our case the Server
will respond with an integer. We’ll see the results in console output window.
Congratulations! We just successfully built a Client
and Server
gRPC application.
References
- Git repository for gRPC Client application
- Git repository gRPC Server application
- Official gRPC site
- Microsoft's documentation for gRPC on .Net Core
Cover photo by Karim MANJRA on Unsplash
This content originally appeared on DEV Community and was authored by Prasanna
Prasanna | Sciencx (2023-03-09T07:35:09+00:00) gRPC and .Net Core. Retrieved from https://www.scien.cx/2023/03/09/grpc-and-net-core/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.