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

@@ -202,14 +202,20 @@ namespace WireMock.Server
_settings.Logger.Info("WireMock.Net by Stef Heyenrath (https://github.com/WireMock-Net/WireMock.Net)");
_settings.Logger.Debug("WireMock.Net server settings {0}", JsonConvert.SerializeObject(settings, Formatting.Indented));
HostUrlOptions urlOptions;
if (settings.Urls != null)
{
Urls = settings.Urls.ToArray();
urlOptions = new HostUrlOptions
{
Urls = settings.Urls
};
}
else
{
int port = settings.Port > 0 ? settings.Port.Value : PortUtils.FindFreeTcpPort();
Urls = new[] { $"{(settings.UseSSL == true ? "https" : "http")}://localhost:{port}" };
urlOptions = new HostUrlOptions
{
UseSSL = settings.UseSSL == true
};
}
_options.FileSystemHandler = _settings.FileSystemHandler;
@@ -222,12 +228,10 @@ namespace WireMock.Server
_mappingConverter = new MappingConverter(_matcherMapper);
#if USE_ASPNETCORE
_httpServer = new AspNetCoreSelfHost(_options, Urls);
_httpServer = new AspNetCoreSelfHost(_options, urlOptions);
#else
_httpServer = new OwinSelfHost(_options, Urls);
_httpServer = new OwinSelfHost(_options, urlOptions);
#endif
Ports = _httpServer.Ports;
var startTask = _httpServer.StartAsync();
using (var ctsStartTimeout = new CancellationTokenSource(settings.StartTimeout))
@@ -254,6 +258,9 @@ namespace WireMock.Server
ctsStartTimeout.Token.WaitHandle.WaitOne(ServerStartDelayInMs);
}
Urls = _httpServer.Urls.ToArray();
Ports = _httpServer.Ports;
}
if (settings.AllowBodyForAllHttpMethods == true)