Server server = new Server
{
Ports = { new ServerPort("localhost", 10042, ServerCredentials.Insecure) },
Services = {
Calculator.BindService(calc),
TimeService.BindService(clock),
}
};
server.Start();
Console.WriteLine("Server running... press any key");
Console.ReadKey();
await server.ShutdownAsync();
客户会更像:
var channel = new Channel("localhost", 10042, ChannelCredentials.Insecure);
try
{
var calc = new Calculator.CalculatorClient(channel);
for (int i = 0; i < 5; i++)
{
using var ma = calc.MultiplyAsync(new MultiplyRequest { X = i, Y = i });
var calcResult = await ma.ResponseAsync;
Console.WriteLine(calcResult.Result);
}
var clock = new TimeService.TimeServiceClient(channel);
using var subResult = clock.Subscribe(new Empty());
var reader = subResult.ResponseStream;
while (await reader.MoveNext(default))
{
var time = reader.Current.Time;
Console.WriteLine($"The time is now {time}");
}
}
finally
{
await channel.ShutdownAsync();
}
该
Grpc.Core
包包含(旧的)非托管客户端和服务器代码,可在 .NET Framework 上使用;典型用法是:客户会更像:
所以:是的。