| | | 1 | | #if USE_ASPNETCORE |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using JetBrains.Annotations; |
| | | 8 | | using Microsoft.AspNetCore.Builder; |
| | | 9 | | using Microsoft.AspNetCore.Hosting; |
| | | 10 | | using WireMock.HttpsCertificate; |
| | | 11 | | using WireMock.Logging; |
| | | 12 | | using WireMock.Util; |
| | | 13 | | using WireMock.Validation; |
| | | 14 | | |
| | | 15 | | namespace WireMock.Owin |
| | | 16 | | { |
| | | 17 | | internal class AspNetCoreSelfHost : IOwinSelfHost |
| | | 18 | | { |
| | 55 | 19 | | private readonly CancellationTokenSource _cts = new CancellationTokenSource(); |
| | | 20 | | private readonly WireMockMiddlewareOptions _options; |
| | | 21 | | private readonly string[] _urls; |
| | | 22 | | private readonly IWireMockLogger _logger; |
| | | 23 | | private Exception _runningException; |
| | | 24 | | |
| | | 25 | | private IWebHost _host; |
| | | 26 | | |
| | 510 | 27 | | public bool IsStarted { get; private set; } |
| | | 28 | | |
| | 110 | 29 | | public List<string> Urls { get; } = new List<string>(); |
| | | 30 | | |
| | 165 | 31 | | public List<int> Ports { get; } = new List<int>(); |
| | | 32 | | |
| | 396 | 33 | | public Exception RunningException => _runningException; |
| | | 34 | | |
| | 55 | 35 | | public AspNetCoreSelfHost([NotNull] WireMockMiddlewareOptions options, [NotNull] params string[] uriPrefixes) |
| | 55 | 36 | | { |
| | 55 | 37 | | Check.NotNull(options, nameof(options)); |
| | 55 | 38 | | Check.NotNullOrEmpty(uriPrefixes, nameof(uriPrefixes)); |
| | | 39 | | |
| | 55 | 40 | | _logger = options.Logger ?? new WireMockConsoleLogger(); |
| | | 41 | | |
| | 275 | 42 | | foreach (string uriPrefix in uriPrefixes) |
| | 55 | 43 | | { |
| | 55 | 44 | | Urls.Add(uriPrefix); |
| | | 45 | | |
| | 55 | 46 | | PortUtils.TryExtract(uriPrefix, out string protocol, out string host, out int port); |
| | 55 | 47 | | Ports.Add(port); |
| | 55 | 48 | | } |
| | | 49 | | |
| | 55 | 50 | | _options = options; |
| | 55 | 51 | | _urls = uriPrefixes; |
| | 55 | 52 | | } |
| | | 53 | | |
| | | 54 | | public Task StartAsync() |
| | 55 | 55 | | { |
| | 55 | 56 | | _host = new WebHostBuilder() |
| | 55 | 57 | | .Configure(appBuilder => |
| | 110 | 58 | | { |
| | 110 | 59 | | appBuilder.UseMiddleware<GlobalExceptionMiddleware>(_options); |
| | 55 | 60 | | |
| | 110 | 61 | | _options.PreWireMockMiddlewareInit?.Invoke(appBuilder); |
| | 55 | 62 | | |
| | 110 | 63 | | appBuilder.UseMiddleware<WireMockMiddleware>(_options); |
| | 55 | 64 | | |
| | 110 | 65 | | _options.PostWireMockMiddlewareInit?.Invoke(appBuilder); |
| | 110 | 66 | | }) |
| | 55 | 67 | | .UseKestrel(options => |
| | 110 | 68 | | { |
| | 55 | 69 | | #if NETSTANDARD1_3 |
| | 55 | 70 | | if (_urls.Any(u => u.StartsWith("https://", StringComparison.OrdinalIgnoreCase))) |
| | 55 | 71 | | { |
| | 55 | 72 | | options.UseHttps(PublicCertificateHelper.GetX509Certificate2()); |
| | 55 | 73 | | } |
| | 55 | 74 | | #else |
| | 55 | 75 | | // https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?tabs=aspnetcore2x |
| | 385 | 76 | | foreach (string url in _urls.Where(u => u.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) |
| | 110 | 77 | | { |
| | 110 | 78 | | PortUtils.TryExtract(url, out string protocol, out string host, out int port); |
| | 110 | 79 | | options.Listen(System.Net.IPAddress.Any, port); |
| | 110 | 80 | | } |
| | 55 | 81 | | |
| | 275 | 82 | | foreach (string url in _urls.Where(u => u.StartsWith("https://", StringComparison.OrdinalIgnoreCase) |
| | 55 | 83 | | { |
| | 55 | 84 | | PortUtils.TryExtract(url, out string protocol, out string host, out int port); |
| | 55 | 85 | | options.Listen(System.Net.IPAddress.Any, port, listenOptions => |
| | 55 | 86 | | { |
| | 55 | 87 | | listenOptions.UseHttps(PublicCertificateHelper.GetX509Certificate2()); |
| | 55 | 88 | | }); |
| | 55 | 89 | | } |
| | 55 | 90 | | #endif |
| | 110 | 91 | | }) |
| | 55 | 92 | | #if NETSTANDARD1_3 |
| | 55 | 93 | | .UseUrls(_urls) |
| | 55 | 94 | | #endif |
| | 55 | 95 | | .Build(); |
| | | 96 | | |
| | 55 | 97 | | return Task.Run(() => |
| | 110 | 98 | | { |
| | 110 | 99 | | StartServers(); |
| | 57 | 100 | | }, _cts.Token); |
| | 55 | 101 | | } |
| | | 102 | | |
| | | 103 | | private void StartServers() |
| | 55 | 104 | | { |
| | | 105 | | try |
| | 55 | 106 | | { |
| | 55 | 107 | | var appLifetime = (IApplicationLifetime) _host.Services.GetService(typeof(IApplicationLifetime)); |
| | 110 | 108 | | appLifetime.ApplicationStarted.Register(() => IsStarted = true); |
| | | 109 | | |
| | | 110 | | #if NETSTANDARD1_3 |
| | | 111 | | _logger.Info("WireMock.Net server using netstandard1.3"); |
| | | 112 | | #elif NETSTANDARD2_0 |
| | 55 | 113 | | _logger.Info("WireMock.Net server using netstandard2.0"); |
| | | 114 | | #elif NET46 |
| | | 115 | | _logger.Info("WireMock.Net server using .net 4.6.1 or higher"); |
| | | 116 | | #endif |
| | | 117 | | |
| | | 118 | | #if NETSTANDARD1_3 |
| | | 119 | | _host.Run(_cts.Token); |
| | | 120 | | #else |
| | 55 | 121 | | _host.RunAsync(_cts.Token).Wait(); |
| | | 122 | | #endif |
| | 2 | 123 | | } |
| | 0 | 124 | | catch (Exception e) |
| | 0 | 125 | | { |
| | 0 | 126 | | _runningException = e; |
| | 0 | 127 | | _logger.Error(e.ToString()); |
| | 0 | 128 | | } |
| | | 129 | | finally |
| | 2 | 130 | | { |
| | 2 | 131 | | IsStarted = false; |
| | 2 | 132 | | } |
| | 2 | 133 | | } |
| | | 134 | | |
| | | 135 | | public Task StopAsync() |
| | 2 | 136 | | { |
| | 2 | 137 | | _cts.Cancel(); |
| | | 138 | | |
| | 2 | 139 | | IsStarted = false; |
| | | 140 | | #if NETSTANDARD1_3 |
| | | 141 | | return Task.FromResult(true); |
| | | 142 | | #else |
| | 2 | 143 | | return _host.StopAsync(); |
| | | 144 | | #endif |
| | 2 | 145 | | } |
| | | 146 | | } |
| | | 147 | | } |
| | | 148 | | #endif |