gRPC in .NET Core
Source: https://docs.microsoft.com/en-us/aspnet/core/tutorials/grpc/grpc-start?view=aspnetcore-5.0&tabs=visual-studio-code
Create a gRPC service
dotnet new grpc -o GrpcGreetercode -r GrpcGreeter
dotnet dev-certs https --trust
CTRL+F5
Create a gRPC client
dotnet add GrpcGreeterClient.csproj package Grpc.Net.Clientdotnet add GrpcGreeterClient.csproj package Google.Protobuf
dotnet add GrpcGreeterClient.csproj package Grpc.Tools
Copy the Protos\greet.proto from the server.
edit: option csharp_namespace = "GrpcGreeterClient";
Edit: GrpcGreeterClient.csproj
<ItemGroup>
Copy the Protos\greet.proto from the server.
edit: option csharp_namespace = "GrpcGreeterClient";
Edit: GrpcGreeterClient.csproj
<ItemGroup>
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
</ItemGroup>
Edit: Program.cs
Edit: Program.cs
using System;
using System.Threading.Tasks;
using Grpc.Net.Client;
namespace GrpcGreeterClient
{
class Program
{
static async Task Main(string[] args)
{
// The port number(5001) must match the port of the gRPC server.
using var channel = GrpcChannel.ForAddress("https://localhost:5001");
var client = new Greeter.GreeterClient(channel);
var reply = await client.SayHelloAsync(new HelloRequest { Name = "GreeterClient" });
Console.WriteLine("Greeting: " + reply.Message); Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
Comments
Post a Comment