diff --git a/src/WireMock.Net/Owin/AspNetCoreSelfHost.NETStandard.cs b/src/WireMock.Net/Owin/AspNetCoreSelfHost.NETStandard.cs new file mode 100644 index 000000000..43f003c0f --- /dev/null +++ b/src/WireMock.Net/Owin/AspNetCoreSelfHost.NETStandard.cs @@ -0,0 +1,40 @@ +#if USE_ASPNETCORE && !NETSTANDARD1_3 +using System; +using System.Collections.Generic; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Server.Kestrel.Core; + +namespace WireMock.Owin +{ + internal partial class AspNetCoreSelfHost + { + private static void SetKestrelOptionsLimits(KestrelServerOptions options) + { + options.Limits.KeepAliveTimeout = TimeSpan.MaxValue; + options.Limits.MaxRequestBodySize = null; // https://stackoverflow.com/questions/46738364/increase-upload-request-length-limit-in-kestrel + options.Limits.MaxRequestBufferSize = null; + options.Limits.MaxRequestHeaderCount = 100; + options.Limits.MaxResponseBufferSize = null; + options.Limits.RequestHeadersTimeout = TimeSpan.MaxValue; + } + + private static void SetHttpsAndUrls(KestrelServerOptions options, ICollection<(string Url, int Port)> urlDetails) + { + foreach (var detail in urlDetails) + { + if (detail.Url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) + { + options.Listen(System.Net.IPAddress.Any, detail.Port, listenOptions => + { + listenOptions.UseHttps(); + }); + } + else + { + options.Listen(System.Net.IPAddress.Any, detail.Port); + } + } + } + } +} +#endif \ No newline at end of file diff --git a/src/WireMock.Net/Owin/AspNetCoreSelfHost.NETStandard13.cs b/src/WireMock.Net/Owin/AspNetCoreSelfHost.NETStandard13.cs new file mode 100644 index 000000000..1e6133ff1 --- /dev/null +++ b/src/WireMock.Net/Owin/AspNetCoreSelfHost.NETStandard13.cs @@ -0,0 +1,32 @@ +#if USE_ASPNETCORE && NETSTANDARD1_3 +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Server.Kestrel; +using WireMock.HttpsCertificate; + +namespace WireMock.Owin +{ + internal partial class AspNetCoreSelfHost + { + private static void SetKestrelOptionsLimits(KestrelServerOptions options) + { + options.Limits.KeepAliveTimeout = TimeSpan.MaxValue; + options.Limits.MaxRequestBufferSize = null; + options.Limits.MaxRequestHeaderCount = 100; + options.Limits.MaxResponseBufferSize = null; + options.Limits.RequestHeadersTimeout = TimeSpan.MaxValue; + } + + private static void SetHttpsAndUrls(KestrelServerOptions options, ICollection<(string Url, int Port)> urlDetails) + { + var urls = urlDetails.Select(u => u.Url); + if (urls.Any(u => u.StartsWith("https://", StringComparison.OrdinalIgnoreCase))) + { + options.UseHttps(PublicCertificateHelper.GetX509Certificate2()); + } + } + } +} +#endif \ No newline at end of file diff --git a/src/WireMock.Net/Owin/AspNetCoreSelfHost.cs b/src/WireMock.Net/Owin/AspNetCoreSelfHost.cs index 0732462d8..dc3a09a7d 100644 --- a/src/WireMock.Net/Owin/AspNetCoreSelfHost.cs +++ b/src/WireMock.Net/Owin/AspNetCoreSelfHost.cs @@ -8,7 +8,6 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; -using WireMock.HttpsCertificate; using WireMock.Logging; using WireMock.Owin.Mappers; using WireMock.Util; @@ -16,7 +15,7 @@ namespace WireMock.Owin { - internal class AspNetCoreSelfHost : IOwinSelfHost + internal partial class AspNetCoreSelfHost : IOwinSelfHost { private readonly CancellationTokenSource _cts = new CancellationTokenSource(); private readonly IWireMockMiddlewareOptions _options; @@ -78,31 +77,9 @@ public Task StartAsync() }) .UseKestrel(options => { - var urlDetails = _urlOptions.GetDetails(); + SetKestrelOptionsLimits(options); -#if NETSTANDARD1_3 - - var urls = urlDetails.Select(u => u.Url); - if (urls.Any(u => u.StartsWith("https://", StringComparison.OrdinalIgnoreCase))) - { - options.UseHttps(PublicCertificateHelper.GetX509Certificate2()); - } -#else - foreach (var detail in urlDetails) - { - if (detail.Url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) - { - options.Listen(System.Net.IPAddress.Any, detail.Port, listenOptions => - { - listenOptions.UseHttps(); // PublicCertificateHelper.GetX509Certificate2() - }); - } - else - { - options.Listen(System.Net.IPAddress.Any, detail.Port); - } - } -#endif + SetHttpsAndUrls(options, _urlOptions.GetDetails()); }) #if NETSTANDARD1_3 @@ -144,6 +121,7 @@ private Task RunHost(CancellationToken token) #elif NET46 _logger.Info("WireMock.Net server using .net 4.6.1 or higher"); #endif + #if NETSTANDARD1_3 return Task.Run(() => {