mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-30 05:41:46 +02:00
Fix unit-tests
This commit is contained in:
34
src/WireMock.Net/Http/PortUtil.cs
Normal file
34
src/WireMock.Net/Http/PortUtil.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace WireMock.Http
|
||||
{
|
||||
/// <summary>
|
||||
/// The ports.
|
||||
/// </summary>
|
||||
public static class PortUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// The find free TCP port.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The <see cref="int"/>.
|
||||
/// </returns>
|
||||
/// <remarks>see http://stackoverflow.com/questions/138043/find-the-next-tcp-port-in-net.</remarks>
|
||||
public static int FindFreeTcpPort()
|
||||
{
|
||||
TcpListener tcpListener = null;
|
||||
try
|
||||
{
|
||||
tcpListener = new TcpListener(IPAddress.Loopback, 0);
|
||||
tcpListener.Start();
|
||||
|
||||
return ((IPEndPoint)tcpListener.LocalEndpoint).Port;
|
||||
}
|
||||
finally
|
||||
{
|
||||
tcpListener?.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user