gRPC - 一元调用 C#

gRPC - Unary Call C#

我正在尝试通过制作一个简单的项目来学习 gRPC server/client,客户端发送 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;
}

服务器

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();

        }
    }
}

客户

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();

        }
    }
}

.Gps

下的服务器代码出现红色波浪线

There is no argument given that corresponds to the required formal parameter 'context' of 'MapRoute.MapRouteBase.Gps(Location, ServerCallContext)'

我尝试将 context 添加到参数中,但出现另一个错误

cannot convert from 'MapPB.LocationName' to 'MapPB.Location'

谁能解释一下我做错了什么?

您的服务功能实现应如下所示:

public override async Task<LocationName> Gps(Location request, ServerCallContext context)
{
    return new LocationName { Name = "Your Location is " + request.La + ":" + request.Lo };
}

这个想法是每当客户端调用它时自动调用该函数。它向您传递定义的参数(位置在这里)。您应该 return 结果 - 这是此处的位置名称。

您在这里做错的是将处理委托给基础 class 而不是自己处理请求。

作为对 Matthias247 回答的更新,我更愿意这样看(即使他的回答也是正确的):

public override Task<LocationName> Gps(Location request, ServerCallContext context)
    {
        return Task.FromResult(new LocationName
        {
            Name = "Your Location is " + request.La + ":" + request.Lo
        });
    }

并将您的客户端更改为异步:

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();

    }