I'm digging out to grpc as a replacement for WCF. I'm using .Net 5 and looking for a stand-alone server solution (No ASP involved --> Concole application). That already limits the available hints and tutorials a lot :-(
Using packages grpc.Core (2.34.0), grpc.Tools (2.34.0), grpc.Net.Common (2.34.0) and Google.Protobuf (3.14.0) it is quite easy to spawn a basic server (GreeterImpl comes from some tutorial):
Server server = new Server();
server.Services.Add(Greeter.BindService(new GreeterImpl()));
server.Ports.Add(new ServerPort("localhost", 5001, ServerCredentials.Insecure));
server.Start();
Console.ReadKey();
This works fine. Server listens on port 5001.
In WCF self-hosted (based uppon http.sys) it was quite easy to host multiple WCF servers just by specifying a different directory (minified code):
1st assembly
List<Uri> baseAddresses = new List<Uri>();
baseAddresses.Add(new Uri("http://localhost:5001/Service1.svc"));
host = new ServiceHost(typeof(GenericService), baseAddresses.ToArray());
host.Open();
2nd assembly
List<Uri> baseAddresses = new List<Uri>();
baseAddresses.Add(new Uri("http://localhost:5001/Service2.svc"));
host = new ServiceHost(typeof(GenericService), baseAddresses.ToArray());
host.Open();
It even worked with a present IIS serving other stuff on port 5001
How do I start a second server (different assembly) using the same port but different directory?
I could not find any suggestion on how to achieve that.
To be running on the same port, it needs to be part of the same Server instance; this effectively means multiple calls to server.Services.Add(...) (on the same server instance) before calling .Start(), with different service instances. To do this, you might need to orchestrate your code such that you can identify all the services before trying to create the server. Partly, this difference is because Grpc.Core does not use Http.Sys - it is a wrapper around chttp2, which does not provide active port sharing. But in reality: this is a very common limitation even in systems that might use OS assistance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With