IConfiguration vs IOptions NET
Synchronous and Asynchronous in .NET Core
Model Binding and Validation in ASP.NET Core
ControllerBase vs Controller in ASP.NET Core
ConfigureServices and Configure methods
IHostedService interface in .NET Core
ASP.NET Core request processing
| Micro-Frontends | GraphQL APIs in .NET Core | |
gRPC with .NET Core |
gRPC (Google Remote Procedure Call) is a high-performance RPC framework that uses HTTP/2 and Protocol Buffers. It's ideal for microservices and real-time communication in .NET Core applications.
gRPC is a modern, high-performance Remote Procedure Call (RPC) framework developed by Google. It is built on HTTP/2 and uses Protocol Buffers (Protobuf) as its Interface Definition Language (IDL) and message serialization format. This approach enables fast, type-safe, and efficient communication between client and server applications, especially in a microservices architecture.
In .NET Core, gRPC is fully integrated and offers features like strong typing, code generation, and streaming capabilities out of the box.
dotnet new grpc -o GrpcService
syntax = "proto3";
option csharp_namespace = "GrpcService";
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply);
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
public class GreeterService : Greeter.GreeterBase {
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context) {
return Task.FromResult(new HelloReply {
Message = $"Hello, {request.Name}"
});
}
}
app.MapGrpcService<GreeterService>();
app.MapGet("/", () => "Use a gRPC client to communicate.");
var channel = GrpcChannel.ForAddress("https://localhost:5001");
var client = new Greeter.GreeterClient(channel);
var reply = await client.SayHelloAsync(new HelloRequest { Name = "Shivshanker" });
Console.WriteLine(reply.Message);
Start by creating a new ASP.NET Core gRPC Service project using Visual Studio or the .NET CLI.
In the Protos folder, the .proto file defines the service methods and message structures. The compiler uses this file to generate the C# server and client code.
Here is an example of a .proto file:
// Protos/greet.proto
syntax = "proto3";
option csharp_namespace = "GrpcGreeter";
package greet;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply);
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
Implement the generated abstract base class Greeter.GreeterBase with your service logic.
Register your service in the application's configuration.
In a separate project, use the same .proto file to generate a client stub and interact with the service.
| Feature | gRPC | REST |
|---|---|---|
| Protocol | HTTP/2, which supports multiplexing, header compression (HPACK), and long-lived connections. | Typically HTTP/1.1, with a single request-response per connection, which can be less efficient. |
| Serialization | Protocol Buffers (Protobuf), a binary format that is faster and more compact than text-based formats. | JSON, a human-readable text format, easier to work with for public APIs but less performant. |
| API Contract | A contract-first approach using .proto files, which auto-generates strongly-typed clients and servers for various languages. |
A resource-oriented approach with no built-in contract, often relying on documentation like OpenAPI/Swagger. |
| Streaming | Built-in support for four types of streaming: unary (request/response), server streaming, client streaming, and bidirectional streaming. | Unary, or one-way streaming via WebSockets, but not natively for standard REST. |
| Browser Support | Requires a proxy layer like gRPC-Web for browser support due to its reliance on HTTP/2 features. | Excellent browser support and tool availability. |
| Developer Experience | Requires tooling to generate code from the .proto file but provides strongly-typed interfaces that prevent runtime errors. |
Easy to learn and debug, with a large ecosystem of tools and libraries. |
async/await. | Micro-Frontends | GraphQL APIs in .NET Core | |