ServiceStack.WebHost.Endpoints.Tests.AppHostListenerBaseTests.GetBaseAddressWithFreePort C# (CSharp) Method

GetBaseAddressWithFreePort() private static method

private static GetBaseAddressWithFreePort ( ) : string
return string
        private static string GetBaseAddressWithFreePort()
        {
            TcpListener listener = new TcpListener(IPAddress.Loopback, 0);
            listener.Start();
            IPEndPoint endPoint = listener.LocalEndpoint as IPEndPoint;

            if (endPoint != null)
            {
                string address = endPoint.Address.ToString();
                int port = endPoint.Port;
                Uri uri = new UriBuilder("http://", address, port).Uri;

                listener.Stop();

                return uri.ToString();
            }

            throw new InvalidOperationException("Can not find a port to start the WpcsServer!");
        }
    }