[英]gRPC - Unary Call C#
I am trying to learn gRPC by making a simple project server/client which the client send 2 Int Variable and the server will responed with a string and the 2 variable that the client sent The Protocol Buffer code is :我试图通过制作一个简单的项目服务器/客户端来学习 gRPC,客户端发送 2 个 Int 变量,服务器将响应一个字符串和客户端发送的 2 个变量,协议缓冲区代码是:
syntax = "proto3";
option csharp_namespace = "MapPB";
service MapRoute {
rpc Gps(Location) returns (LocationName) {}
}
message Location {
int32 la = 1;
int32 lo = 2;
}
message LocationName {
string Name = 1;
}
Server服务器
using Grpc.Core;
using static MapPB.MapRoute;
using MapPB;
namespace gServer
{
public class gS : MapRouteBase
{
public override async Task<LocationName> Gps(Location request, ServerCallContext context)
{
return await base.Gps(new LocationName { Name = "Your Location is " + request.La + ":" + request.Lo } );
}
}
class Program
{
const int Port = 50051;
static void Main(string[] args)
{
Server server = new Server
{
Services = { MapRoute.BindService(new gS()) },
Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) }
};
server.Start();
Console.WriteLine("Map server listening on port " + Port);
Console.WriteLine("Press any key to stop the server...");
Console.ReadKey();
server.ShutdownAsync().Wait();
}
}
}
Client客户
using Grpc.Core;
using Map;
using static MapPB.MapRoute;
namespace Map
{
class Program : MapRouteClient
{
static void Main(string[] args)
{
Channel channel = new Channel("127.0.0.1:50021", ChannelCredentials.Insecure);
var client = new MapRouteClient(channel);
int la = 1;
int lo = 2;
var reply = client.Gps(new MapPB.Location { La = la , Lo = lo });
Console.WriteLine(reply.Name);
channel.ShutdownAsync().Wait();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
Am getting an red squiggle at the server code under .Gps
在
.Gps
下的服务器代码处出现红色波浪.Gps
There is no argument given that corresponds to the required formal parameter 'context' of 'MapRoute.MapRouteBase.Gps(Location, ServerCallContext)'
没有给出对应于“MapRoute.MapRouteBase.Gps(Location, ServerCallContext)”的所需形式参数“context”的参数
I tried to add context
to the parameter but I got another error我尝试向参数添加
context
,但出现另一个错误
cannot convert from 'MapPB.LocationName' to 'MapPB.Location'
无法从“MapPB.LocationName”转换为“MapPB.Location”
can someone explain what wrong am doing Please ?有人可以解释一下我在做什么吗?
Your service function implementation should look like:您的服务功能实现应如下所示:
public override async Task<LocationName> Gps(Location request, ServerCallContext context)
{
return new LocationName { Name = "Your Location is " + request.La + ":" + request.Lo };
}
The idea is that the function is automatically invoked whenever a client calls it.这个想法是每当客户端调用该函数时都会自动调用它。 It passes you the defined parameters (location here).
它传递给你定义的参数(位置在这里)。 You are expected to return the result - which is the location name here.
您应该返回结果 - 这是这里的位置名称。
What you did wrong here was delegating the processing to the base class instead of handling the request yourself.您在这里做错的是将处理委托给基类而不是自己处理请求。
As an update on Matthias247's answer, I would prefer to see it like this (even tho his answer is also correct):作为 Matthias247 答案的更新,我更愿意看到它(即使他的答案也是正确的):
public override Task<LocationName> Gps(Location request, ServerCallContext context)
{
return Task.FromResult(new LocationName
{
Name = "Your Location is " + request.La + ":" + request.Lo
});
}
And change your client to async:并将您的客户端更改为异步:
static async Task Main(string[] args)
{
Channel channel = new Channel("127.0.0.1:50021", ChannelCredentials.Insecure);
var client = new MapRouteClient(channel);
int la = 1;
int lo = 2;
var reply = await client.GpsAsync(new MapPB.Location { La = la , Lo = lo });
Console.WriteLine(reply.Name);
channel.ShutdownAsync().Wait();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.