aspnetcore/grpc/code-first.md
By James Newton-King and Marc Gravell
:::moniker range=">= aspnetcore-6.0"
Code-first gRPC uses .NET types to define service and message contracts.
Code-first is a good choice when an entire system uses .NET:
.proto files and code generation.Code-first isn't recommended in polyglot systems with multiple languages. .NET service and data contract types can't be used with non-.NET platforms. To call a gRPC service written using code-first, other platforms must create a .proto contract that matches the service.
[!IMPORTANT] For help with protobuf-net.Grpc, visit the protobuf-net.Grpc website or create an issue on the protobuf-net.Grpc GitHub repository.
protobuf-net.Grpc is a community project and isn't supported by Microsoft. It adds code-first support to Grpc.AspNetCore and Grpc.Net.Client. It uses .NET types annotated with attributes to define an app's gRPC services and messages.
The first step to creating a code-first gRPC service is defining the code contract:
The preceding code:
HelloRequest and HelloReply messages.IGreeterService contract interface with the unary SayHelloAsync gRPC method.The service contract is implemented on the server and called from the client.
Methods defined on service interfaces must match certain signatures depending on whether they're:
For more information on defining service contracts, see the protobuf-net.Grpc getting started documentation.
To add gRPC code-first service to an ASP.NET Core app:
Add a protobuf-net.Grpc.AspNetCore package reference.
Add a reference to the shared code-contract project.
Create a new GreeterService.cs file and implement the IGreeterService service interface:
Update the Program.cs file:
The preceding highlighted code updates the following:
AddCodeFirstGrpc registers services that enable code-first.MapGrpcService<GreeterService> adds the code-first service endpoint.gRPC services implemented with code-first and .proto files can co-exist in the same app. All gRPC services use gRPC service configuration.
A code-first gRPC client uses the service contract to call gRPC services.
In the gRPC client .csproj file:
Update the client program.cs
The preceding gRPC client Program.cs code:
CreateGrpcService<IGreeterService> extension method.SayHelloAsync.A code-first gRPC client is created from a channel. Just like a regular client, a code-first client uses its channel configuration.
View or download sample code (how to download)
:::moniker-end
:::moniker range="< aspnetcore-6.0"
Code-first gRPC uses .NET types to define service and message contracts.
Code-first is a good choice when an entire system uses .NET:
.proto files and code generation.Code-first isn't recommended in polyglot systems with multiple languages. .NET service and data contract types can't be used with non-.NET platforms. To call a gRPC service written using code-first, other platforms must create a .proto contract that matches the service.
[!IMPORTANT] For help with protobuf-net.Grpc, visit the protobuf-net.Grpc website or create an issue on the protobuf-net.Grpc GitHub repository.
protobuf-net.Grpc is a community project and isn't supported by Microsoft. It adds code-first support to Grpc.AspNetCore and Grpc.Net.Client. It uses .NET types annotated with attributes to define an app's gRPC services and messages.
The first step to creating a code-first gRPC service is defining the code contract:
The preceding code:
HelloRequest and HelloReply messages.IGreeterService contract interface with the unary SayHelloAsync gRPC method.The service contract is implemented on the server and called from the client. Methods defined on service interfaces must match certain signatures depending on whether they're unary, server streaming, client streaming, or bidirectional streaming.
For more information on defining service contracts, see the protobuf-net.Grpc getting started documentation.
To add gRPC code-first service to an ASP.NET Core app:
Create a new GreeterService.cs file and implement the IGreeterService service interface:
Update the Startup.cs file:
In the preceding code:
AddCodeFirstGrpc registers services that enable code-first.MapGrpcService<GreeterService> adds the code-first service endpoint.gRPC services implemented with code-first and .proto files can co-exist in the same app. All gRPC services use gRPC service configuration.
A code-first gRPC client uses the service contract to call gRPC services. To call a gRPC service using a code-first client:
The preceding code:
CreateGrpcService<IGreeterService> extension method.SayHelloAsync.A code-first gRPC client is created from a channel. Just like a regular client, a code-first client uses its channel configuration.
View or download sample code (how to download)
:::moniker-end