是否可以使用Asp.Net Core grpc中的不同端口添加grpc服务



我有一个现有的.net核心应用程序,它包含两个grpc服务(使用grpc.core(。我有两个服务器

Server serverA = new Server(channelOptions)
{
Ports = { new ServerPort("0.0.0.0", 8090, credentials) },
};
serverA.services.add(serviceA);
serverA.Start()
Server serverB = new Server(channelOptions)
{
Ports = { new ServerPort("0.0.0.0", 8091, credentials) },
};
serverB.services.add(serviceB);
serverB.Start()

现在,我正试图从grp.core迁移到asp.net核心grpc,但在添加服务时,我找不到添加两个具有两个不同端口的服务的方法:

services.AddSingleton<ServiceA>();
services.AddSingleton<ServiceB>();

两者都在同一个端口,有没有办法在asp.net核心grpc中为每个服务设置一个端口?

从3.1模板项目ASP.NET Core gRPC Service,您可以通过启动设置在两个端口上侦听:

"applicationUrl": "https://localhost:5001;https://localhost:5002",

并添加基于端口的分支路由,如下所示:

app.UseRouting();
app.MapWhen(context => context.Connection.LocalPort == 5001,
iab => iab.UseRouting().UseEndpoints(endpoints => endpoints.MapGrpcService<GreeterService>()));
app.MapWhen(context => context.Connection.LocalPort == 5002,
iab => iab.UseRouting().UseEndpoints(endpoints => endpoints.MapGrpcService<GreeterServiceV2>()));

最新更新