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