【发布时间】:2021-01-15 09:15:52
【问题描述】:
我正在挖掘 grpc 作为 WCF 的替代品。我正在使用 .Net 5 并寻找独立的 服务器解决方案(不涉及 ASP --> Concole 应用程序)。这已经在很大程度上限制了可用的提示和教程:-(
使用包 grpc.Core (2.34.0)、grpc.Tools (2.34.0)、grpc.Net.Common (2.34.0) 和 Google.Protobuf (3.14.0) 很容易生成一个基本的服务器(GreeterImpl 来自一些教程):
Server server = new Server();
server.Services.Add(Greeter.BindService(new GreeterImpl()));
server.Ports.Add(new ServerPort("localhost", 5001, ServerCredentials.Insecure));
server.Start();
Console.ReadKey();
这很好用。服务器监听 5001 端口。
在 WCF 自托管(基于 http.sys)中,托管多个 WCF 服务器非常容易 通过指定不同的目录(精简代码):
第一次大会
List<Uri> baseAddresses = new List<Uri>();
baseAddresses.Add(new Uri("http://localhost:5001/Service1.svc"));
host = new ServiceHost(typeof(GenericService), baseAddresses.ToArray());
host.Open();
第二次组装
List<Uri> baseAddresses = new List<Uri>();
baseAddresses.Add(new Uri("http://localhost:5001/Service2.svc"));
host = new ServiceHost(typeof(GenericService), baseAddresses.ToArray());
host.Open();
它甚至可以与在端口 5001 上为其他东西提供服务的现有 IIS 一起工作
如何使用相同的端口但不同的目录启动第二台服务器(不同的程序集)?
我找不到任何关于如何实现这一目标的建议。
【问题讨论】:
-
附带说明:如果您从 WCF 迁移,protobuf-net.Grpc 可能对您有用;见:github.com/protobuf-net/protobuf-net.Grpc/tree/main/examples/…; protobuf-net.Grpc 在客户端和服务器上都可以与 Google (Grpc.Core) 和 Microsoft 传输一起使用
-
@MarcGravell 谢谢你会更深入地了解它
-
WCF中有端口共享功能。查了微软文档中关于gRPC的介绍,gRPC中好像没有这个功能。