.NET › gRPC Services

How do you implement a gRPC server method in ASP.NET Core, and how does it integrate with the standard hosting model?

Intermediate

Answer

A gRPC service class extends the code-generated base class (from the .proto file) and overrides its virtual RPC methods, registered via MapGrpcService<T>() in the endpoint routing configuration -- gRPC services run on top of the same Kestrel/ASP.NET Core hosting infrastructure as regular web APIs, meaning they benefit from the same middleware pipeline, dependency injection, logging, and configuration systems.
public class GreeterService : Greeter.GreeterBase {
    public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context) {
        return Task.FromResult(new HelloReply { Message = $"Hello, {request.Name}" });
    }
}

// Program.cs
builder.Services.AddGrpc();
app.MapGrpcService<GreeterService>();
Real-world example A team adds a gRPC service alongside their existing REST controllers in the same ASP.NET Core application, both benefiting from the same registered authentication middleware and dependency-injected services without any duplicated configuration.

Common follow-ups: Can a single ASP.NET Core app serve both REST and gRPC endpoints simultaneously?;How does dependency injection work within a gRPC service method?