private void StartServers() { try { IsStarted = true; #if NETSTANDARD1_3 _logger.Info("WireMock.Net server using netstandard1.3"); #elif NETSTANDARD2_0 _logger.Info("WireMock.Net server using netstandard2.0"); #elif NET46 _logger.Info("WireMock.Net server using .net 4.6.1 or higher"); #endif #if NETSTANDARD1_3 _host.Run(_cts.Token); #else _host.RunAsync(_cts.Token).Wait(); #endif } catch (Exception e) { _runningException = e; _logger.Error(e.ToString()); } finally { IsStarted = false; } }
private void StartServers() { try { var appLifetime = (IApplicationLifetime)_host.Services.GetService(typeof(IApplicationLifetime)); appLifetime.ApplicationStarted.Register(() => IsStarted = true); #if NETSTANDARD1_3 _logger.Info("WireMock.Net server using netstandard1.3"); #elif NETSTANDARD2_0 _logger.Info("WireMock.Net server using netstandard2.0"); #elif NET46 _logger.Info("WireMock.Net server using .net 4.6.1 or higher"); #endif #if NETSTANDARD1_3 _host.Run(_cts.Token); #else _host.RunAsync(_cts.Token).Wait(); #endif } catch (Exception e) { _runningException = e; _logger.Error(e.ToString()); } finally { IsStarted = false; } }
private void StartServers() { #if NET46 _logger.Info("WireMock.Net server using .net 4.6.1 or higher"); #else _logger.Info("WireMock.Net server using .net 4.5.x"); #endif var servers = new List <IDisposable>(); try { var requestMapper = new OwinRequestMapper(); var responseMapper = new OwinResponseMapper(_options); var matcher = new MappingMatcher(_options); /* * Action<IAppBuilder> startup = app => * { * app.Use<GlobalExceptionMiddleware>(_options, responseMapper); * _options.PreWireMockMiddlewareInit?.Invoke(app); * app.Use<WireMockMiddleware>(_options, requestMapper, responseMapper, matcher); * _options.PostWireMockMiddlewareInit?.Invoke(app); * };*/ m_appBuilder.Use <GlobalExceptionMiddleware>(_options, responseMapper); m_appBuilder.Use <IgnorePrefixesMiddleware>(_options); _options.PreWireMockMiddlewareInit?.Invoke(m_appBuilder); m_appBuilder.Use <WireMockMiddleware>(_options, requestMapper, responseMapper, matcher); _options.PostWireMockMiddlewareInit?.Invoke(m_appBuilder); /*foreach (var url in Urls) * { * servers.Add(WebApp.Start(url, startup)); * }*/ IsStarted = true; // WaitHandle is signaled when the token is cancelled, // which will be more efficient than Thread.Sleep in while loop _cts.Token.WaitHandle.WaitOne(); } catch (Exception e) { // Expose exception of starting host, otherwise it's hard to be troubleshooting if keeping quiet // For example, WebApp.Start will fail with System.MissingMemberException if Microsoft.Owin.Host.HttpListener.dll is being located // https://stackoverflow.com/questions/25090211/owin-httplistener-not-located/31369857 _runningException = e; _logger.Error(e.ToString()); } finally { IsStarted = false; // Dispose all servers in finally block to make sure clean up allocated resource on error happening servers.ForEach(s => s.Dispose()); } }
private Task RunHost(CancellationToken token) { try { var appLifetime = (IApplicationLifetime)_host.Services.GetService(typeof(IApplicationLifetime)); appLifetime.ApplicationStarted.Register(() => { var addresses = _host.ServerFeatures .Get <Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature>() .Addresses; foreach (string address in addresses) { Urls.Add(address.Replace("0.0.0.0", "localhost").Replace("[::]", "localhost")); PortUtils.TryExtract(address, out bool isHttps, out string protocol, out string host, out int port); Ports.Add(port); } IsStarted = true; }); #if NETSTANDARD1_3 _logger.Info("WireMock.Net server using netstandard1.3"); #elif NETSTANDARD2_0 _logger.Info("WireMock.Net server using netstandard2.0"); #elif NETSTANDARD2_1 _logger.Info("WireMock.Net server using netstandard2.1"); #elif NETCOREAPP3_1 _logger.Info("WireMock.Net server using .NET Core 3.1"); #elif NET5_0 _logger.Info("WireMock.Net server using .NET 5.0"); #elif NET46 _logger.Info("WireMock.Net server using .NET Framework 4.6.1 or higher"); #endif #if NETSTANDARD1_3 return(Task.Run(() => { _host.Run(token); })); #else return(_host.RunAsync(token)); #endif } catch (Exception e) { _runningException = e; _logger.Error(e.ToString()); IsStarted = false; return(Task.CompletedTask); } }
private Task RunHost(CancellationToken token) { try { var appLifetime = (IApplicationLifetime)_host.Services.GetService(typeof(IApplicationLifetime)); appLifetime.ApplicationStarted.Register(() => { IsStarted = true; }); #if NETSTANDARD1_3 _logger.Info("WireMock.Net server using netstandard1.3"); #elif NETSTANDARD2_0 _logger.Info("WireMock.Net server using netstandard2.0"); #elif NET46 _logger.Info("WireMock.Net server using .net 4.6.1 or higher"); #endif #if NETSTANDARD1_3 return(Task.Run(() => { _host.Run(token); })); #else return(_host.RunAsync(token)); #endif } catch (Exception e) { _runningException = e; _logger.Error(e.ToString()); IsStarted = false; return(Task.CompletedTask); } }