I would like to get a free port for an application. I do not care which port it is. Ingo Rammer in a 2001 post provided a hacky solution to this issue. I would like to know if there is cleaner (threadsafe etc.) solutions available
Will .Net 2.0 provides a native helper to solve this issue
Thanks in advance,
Joannes

How to get a free port ?
Michiel0099
Also I have an additional question, how can I know on which port the TcpChannel has been instanciated
Joannes
alexgr
What I'm missing from this discussion is if you create a server channel and cause it to pick a port for you, how does the client know how to construct the Activator.GetObject(...) URL parameter which usually contains the port the server object is using in its channel transport. E.g. client URL: "tcp://localhost:7777/RemoteObjURI.rem"
Or do you do the same in client Activator as the server channel instanciation, i.e. use the following URL: "tcp://localhost:0/RemoteObjURI.rem" or "tcp://localhost/RemoteObjURI.rem"
Thanks in advance.
ncj
JulesW
For TcpChannel I think you'd have to use ChannelData to retrieve the channel URI and parse that.
If you aren't using remoting then a TcpListener might be a better class to use.
http://msdn2.microsoft.com/library/c4zw1w3a(en-us,vs.80).aspx With TcpListener, you can still use zero to let the system pick your port number and then use the LocalEndpoint property to find out what port number you got.
mydarlingdarla
tombraider
what you describe is a discovery mechanism. remoting doesn't and will likely never have support for discovery.
WCF doesn't have support for discovery either in the current release, though there is a sample that allows you to use it:
http://wcf.netfx3.com/files/folders/protocol_channels/entry7909.aspx
Louis315
Thank you ranamauro for your response. I do believe you are correct. However, just for grins I'm going to test my theory.
So the CLR remoting channel mechanism can't simply lookup an endpoint by the URI, the "foo.rem" part of the URL, amongst all the registered endpoints on the server designated by the URL It seems to me that unique registered URIs could be enforced for those server channel requests that pick a port on the caller's behalf by having the registration process return an error for a duplicate endpoint of this kind. Is there anybody out there that is definitive about the answer to my question