How can I find a TCP port that is free with NetTcpBinding (WCF) (so a server can bind to it)
Asked Answered
S

5

9

Find the next TCP port in .Net says how to do this in raw .net, but not how to safely to this with WCF.

In my unit tests, I need to use the NetTcpBinding, I do not wish to hard code the port it is using.

Therefore how can I get the NetTcpBinding to automatically choose a free port when used in my ServiceHost?

How can I get it to tell me the port (or full endpoint address) it has chosen?

Or how can I using .NET find a few port that is valid for a server to bind to?


Given that my bounty did not lead to any new answers, I think we can assume there is no good answer.

Synclastic answered 26/11, 2009 at 11:12 Comment(1)
Please mark Matt's post as answer. It does definitely work so make it clear for new visitors.Cerulean
A
11

You don't need to roll your own port-finding logic - Windows will choose a free port if you specify it as 0. Then you can find out which port was assigned by interrogating the dispatchers, like so:

// Specify port 0, this will cause Windows to choose a free port
var baseUri = new Uri("net.tcp://" + Dns.GetHostEntry("").HostName + ":0");
host = new WebServiceHost(typeof(MyService));
var endPoint = host.AddServiceEndpoint(typeof(IMyService), new NetTcpBinding(), baseUri);

 // Tell WCF to actually bind to a free port instead of 0
 endPoint.ListenUriMode = ListenUriMode.Unique;

 host.Open();

 // Now that the host has bound to a specific port, we can find out which one it chose
 return host.ChannelDispatchers.First().Listener.Uri;
Aphrodisiac answered 28/7, 2014 at 14:55 Comment(0)
H
4

Set the port to zero. That allows the OS to pick an available port for you. If you need to determine which port was used, you can query that from the socket after it has been bound locally.

Hildegaard answered 5/1, 2010 at 21:8 Comment(1)
how to I get the socket from the NetTcpBinding WCF, if I pass in a port of 0?Synclastic
B
2

Here's what I do: start with a random port in range 1025-2000 (range chosen arbitrarily). I try to bind it and if it fails I catch the exception. Then I go one port up (port = port % 2000 + 1025) until I wrap. I no port is bound, I give up failing the test.

Bradeord answered 26/11, 2009 at 13:27 Comment(3)
how do you stop the windows firewall openning up it's UK each time you try a port?Synclastic
uh, I dunno about this firewall thingy; my devel machine has no firewall on it, as it is within firewalled networkBradeord
Ian, I don't have a firewall configured on the machines that I develop network applications on or on my build machines, so it's not an issue.Gymnasiarch
P
2

4 years later.. at the present day we have a solution, setting ListenUriMode to Unique, as stated from MSDN

Here a little example of an endpoint configuration

<endpoint address="service"
          binding="netTcpBinding"
          contract="Iservice"
          name="TcpBinding"
          bindingConfiguration="netTcpBindingNoSec"
          listenUriMode="Unique"
          />

And now the next problem: If the port is dynamic how can clients know it? As stated in THIS ANSWER you can use WCF DISCOVERY: it requires a little configuration on both server and client side but it works well (with the only problem that it takes some seconds to get it, by default 20 seconds but i have no problems forcing it to 5)

Polish answered 24/6, 2014 at 9:23 Comment(0)
P
1

when using a random high port you can generate collisions with other server processes that want to start after your program. just use zero as the port number and let the os care about reservations (/etc/services on unix, don´t know how windows handles it).

Pervade answered 1/12, 2009 at 9:53 Comment(1)
sorry I only care about windowsSynclastic

© 2022 - 2024 — McMap. All rights reserved.