private static int GetRandomUnusedPort() { try { var listener = new TcpListener(IPAddress.Any, 0); listener.Start(); var port = ((IPEndPoint)listener.LocalEndpoint).Port; listener.Stop(); return port; } catch { throw; } }
/// <summary> /// Configures a random port. /// </summary> /// <returns></returns> public Configuration WithRandomPort() { this.Port = Configuration.GetRandomUnusedPort(); return(this); }