Table of Contents1. Introduction
2. Develop gRPC service in .NET 7
3. Test the service
4. Conclusion
Introduction
gRPC (Google Remote Procedure Call) is an open-source high-performance framework developed by Google for building distributed systems. It is a modern and efficient way to connect and communicate between microservices, mobile devices, web browsers, and back-end systems. gRPC uses HTTP/2 as the transport layer for fast and efficient communication.
For developing new gRPC services, .NET Core is the recommended platform. However gRPC clients can be developed in .NET Framework or .NET core or both.
This post will talk about developing a gRPC service in .NET 7.
Lets get started!
Develop gRPC service in .NET 7
Create a new project in Visual Studio using the "ASP.NET Core gRPC Service" template. Alternatively, you can use the .NET CLI command dotnet new grpc
to create a new gRPC project.
Give a name for the service and choose a location to save the project.
From the dropdown in the next screen choose .NET 7.0 and leave all other options with default value.
Define your gRPC service in the .proto file under Protos folder in the solution. That describes the messages and methods for your service. For this example rename the file that comes with the template to health.proto and update the code as in the screen shot below.
Right click on the health.proto file in the solution and open properties window. Set the value for 'Build Action' as 'Protobuf compiler' and value for 'gRPC Stub Classes' as 'Server only'. Now build the project [Ctrl+Shift+B] which will generate C# code for the .proto file which can be used to implement your service.
Implement gRPC service by creating a C# class "HealthService.cs" under Services folder in the solution that inherits from the generated base class for your service. Implement the 'GetHealth' method that returns a response with message as "Hello!!"
using Grpc.Core;
namespace GrpcService.Services
{
public class HealthService : HealthCheck.HealthCheckBase
{
private readonly ILogger<HealthService> _logger;
public HealthService(ILogger<HealthService> logger)
{
_logger = logger;
}
public override Task<HealthResponse> GetHealth(HealthRequest request, ServerCallContext context)
{
return Task.FromResult(new HealthResponse
{
Message = "Hello!!"
});
}
}
}
Make changes to Program.cs file to configure your gRPC service by adding it to the dependency injection container. By using 'AddGrpc' method you can add your gRPC service to the container.
Use 'ConfigureKestrel' method to specify the IP address and port number where the service will be listening to. And by using 'MapGrpcService<HealthService>()' method to map the service to the service class.
using GrpcService.Services;
using System.Net;
using Microsoft.AspNetCore.Server.Kestrel.Core;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGrpc();
builder.WebHost.ConfigureKestrel(x =>
{
x.Listen(IPAddress.Any, 4000, options =>
{
options.Protocols = HttpProtocols.Http2;
});
});
var app = builder.Build();
app.MapGrpcService<HealthService>();
app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client.");
app.Run();
Test the service
We are done implementing the basic gRPC service. Start the gRPC server by pressing F5.
You will notice that a new console window opens up and the service starts listening on the IP Address and port number that was configured.
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://0.0.0.0:4000
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
Conclusion
Developers working in .NET Framework or .NET using WCF might want to consider gRPC in the scenario where they seek to migrate their application to .NET core. gRPC is a modern high performance and lightweight RPC framework which is gaining popularity.
Comments
Post a Comment