Ejemplo n.º 1
0
        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());
            }
        }