Let the .NET core/standard WebHostBuilder use a random port (#417)

* wip

* code reformat
This commit is contained in:
Stef Heyenrath
2020-03-14 08:51:26 +01:00
committed by GitHub
parent aeb95b02d2
commit 68ffcda53b
6 changed files with 105 additions and 51 deletions

View File

@@ -0,0 +1,41 @@
using System.Collections.Generic;
using WireMock.Util;
namespace WireMock.Owin
{
internal class HostUrlOptions
{
public ICollection<string> Urls { get; set; }
public bool UseSSL { get; set; }
public ICollection<(string Url, int Port)> GetDetails()
{
var list = new List<(string Url, int Port)>();
if (Urls == null)
{
int port = FindFreeTcpPort();
list.Add(($"{(UseSSL ? "https" : "http")}://localhost:{port}", port));
}
else
{
foreach (string url in Urls)
{
PortUtils.TryExtract(url, out string protocol, out string host, out int port);
list.Add((url, port));
}
}
return list;
}
private int FindFreeTcpPort()
{
#if USE_ASPNETCORE || NETSTANDARD2_0
return 0;
#else
return PortUtils.FindFreeTcpPort();
#endif
}
}
}