繁体   English   中英

gRPC - 一元调用 C#

[英]gRPC - Unary Call C#

我试图通过制作一个简单的项目服务器/客户端来学习 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;
}

服务器

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下的服务器代码处出现红色波浪.Gps

没有给出对应于“MapRoute.MapRouteBase.Gps(Location, ServerCallContext)”的所需形式参数“context”的参数

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

无法从“MapPB.LocationName”转换为“MapPB.Location”

有人可以解释一下我在做什么吗?

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

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

这个想法是每当客户端调用该函数时都会自动调用它。 它传递给你定义的参数(位置在这里)。 您应该返回结果 - 这是这里的位置名称。

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

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

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM