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 :
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
There is no argument given that corresponds to the required formal parameter 'context' of 'MapRoute.MapRouteBase.Gps(Location, ServerCallContext)'
I tried to add context
to the parameter but I got another error
cannot convert from 'MapPB.LocationName' to '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.