Professional Documents
Culture Documents
proto files
gRPC uses a contract-first approach to API development. Services and messages are
defined in *.proto files:
ProtoBuf
syntax = "proto3";
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply);
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
.NET types for services, clients and messages are automatically generated by including
*.proto files in a project:
XML
<ItemGroup>
<Protobuf Include="Protos\greet.proto" />
</ItemGroup>
For more information on gRPC tooling support, see gRPC services with C#.
C#
public class GreeterService : Greeter.GreeterBase
{
private readonly ILogger<GreeterService> _logger;
GreeterService inherits from the GreeterBase type, which is generated from the
Greeter service in the *.proto file. The service is made accessible to clients in
Startup.cs:
C#
app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<GreeterService>();
});
To learn more about gRPC services on ASP.NET Core, see gRPC services with
ASP.NET Core.
C#
var channel = GrpcChannel.ForAddress("https://localhost:5001");
var client = new Greeter.GreeterClient(channel);
Console.WriteLine(response.Message);
For more information on creating clients, and calling different service methods, see Call
gRPC services with the .NET client.