Fix ConcurrentDictionary (#129) (#144)

This commit is contained in:
Stef Heyenrath
2018-05-28 08:08:18 +02:00
committed by GitHub
parent dc39f91205
commit 0640c88bcd
6 changed files with 19 additions and 27 deletions

View File

@@ -9,6 +9,7 @@ namespace WireMock.Http
/// </summary>
public static class PortUtil
{
private static readonly IPEndPoint DefaultLoopbackEndpoint = new IPEndPoint(IPAddress.Loopback, port: 0);
private static readonly Regex UrlDetailsRegex = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>\d+)?/", RegexOptions.Compiled);
/// <summary>
@@ -17,17 +18,10 @@ namespace WireMock.Http
/// <remarks>see http://stackoverflow.com/questions/138043/find-the-next-tcp-port-in-net.</remarks>
public static int FindFreeTcpPort()
{
TcpListener tcpListener = null;
try
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
tcpListener = new TcpListener(IPAddress.Loopback, 0);
tcpListener.Start();
return ((IPEndPoint)tcpListener.LocalEndpoint).Port;
}
finally
{
tcpListener?.Stop();
socket.Bind(DefaultLoopbackEndpoint);
return ((IPEndPoint)socket.LocalEndPoint).Port;
}
}