2

I have an ASP.NET Core 6 Web Api application.

I have a Grpc Client and a Grpc Server.

I have a proto file:

service FooService {
   rpc GetFirstData (FirstRequest) returns (FirstResult);
   rpc GetAnotherData (AnotherRequest) returns (AnotherResult);
}

In the Grpc Client I have 2 services which take FooClient as a parameter. The first service sends GetFirstData request and the second Service sends GetAnotherData request to the Grpc Server. There are no issues here.

With the server, however, if I try to add 2 services, I get a Server Error 500.

public class FirstServerService : Protos.FooService.FooServiceBase
{
    public override Task<FirstResult> GetFirstData (FirstRequest request, ServerCallContext context)
    {
    }
}

public class AnotherServerService : Protos.FooService.FooServiceBase
{
    public override Task<AnotherResult> GetAnotherData (AnotherRequest request, ServerCallContext context)
    {
    }
}

The question is: Is it possible to have multiple services on the backend implementing methods from the same Proto file?

MiBuena
  • 451
  • 3
  • 22
  • Lol I was just looking for the answer and you asked it 6 hours ago. Exactly what I'd like to achieve and group them somehow so they have different endpoints for gRPC Web like /impl1/FooService and /impl2/FooService – Konrad Aug 29 '22 at 15:52
  • Maybe using `RequireHost` and listening on different ports for each implementation e.g. FirstService listens on 5500 and the other on 5501... would work... – Konrad Aug 29 '22 at 16:01
  • https://github.com/grpc/grpc-dotnet/issues/110 – Konrad Aug 29 '22 at 16:04
  • https://github.com/grpc/grpc/issues/14900 – Konrad Aug 29 '22 at 16:05
  • 1
    The only way to possibly achieve this would be some sort of custom server implementation I think. Either listen on different ports like Konrad said, or map the endpoints manually. As soon as you register them with *MapGrpcService* you essentially register the same service base twice on the same endpoint, which causes the error – dan-kli Sep 01 '22 at 13:48

1 Answers1

0

In the Grpc Client you must have two server endpoints defined in appsettins.json or hardcoded as parameters when adding client services in dependency injection in Program.cs such as:

builder.Services.AddSingleton<IFirstClient>(p => new FirstClient(builder.Configuration["GrpcServerUrl1"]));
builder.Services.AddSingleton<ISecondClient>(p => new SecondClient(builder.Configuration["GrpcServerUrl2"]));

now each client service in your Grpc Client program will be reached to different grpc server.

Majid Shahabfar
  • 4,010
  • 2
  • 28
  • 36