Exemple #1
0
        public Task StartAsync()
        {
            _host = new WebHostBuilder()
                    .Configure(appBuilder =>
            {
                appBuilder.UseMiddleware <GlobalExceptionMiddleware>(_options);

                _options.PreWireMockMiddlewareInit?.Invoke(appBuilder);

                appBuilder.UseMiddleware <WireMockMiddleware>(_options);

                _options.PostWireMockMiddlewareInit?.Invoke(appBuilder);
            })
                    .UseKestrel(options =>
            {
#if NETSTANDARD1_3
                if (_urls.Any(u => u.StartsWith("https://", StringComparison.OrdinalIgnoreCase)))
                {
                    options.UseHttps(PublicCertificateHelper.GetX509Certificate2());
                }
#else
                // https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?tabs=aspnetcore2x
                foreach (string url in _urls.Where(u => u.StartsWith("http://", StringComparison.OrdinalIgnoreCase)))
                {
                    PortUtil.TryExtractProtocolAndPort(url, out string host, out int port);
                    options.Listen(System.Net.IPAddress.Loopback, port);
                }

                foreach (string url in _urls.Where(u => u.StartsWith("https://", StringComparison.OrdinalIgnoreCase)))
                {
                    PortUtil.TryExtractProtocolAndPort(url, out string host, out int port);
                    options.Listen(System.Net.IPAddress.Loopback, port, listenOptions =>
                    {
                        listenOptions.UseHttps(PublicCertificateHelper.GetX509Certificate2());
                    });
                }
#endif
            })
#if NETSTANDARD1_3
                    .UseUrls(_urls)
#endif
                    .Build();

            IsStarted = true;

#if NETSTANDARD1_3
            Console.WriteLine("WireMock.Net server using netstandard1.3");
            return(Task.Run(() =>
            {
                _host.Run(_cts.Token);
            }, _cts.Token));
#else
            System.Console.WriteLine("WireMock.Net server using netstandard2.0");

            return(Task.Run(() =>
            {
                _host.Run();
            }, _cts.Token));
#endif
        }
Exemple #2
0
        public OwinSelfHost([NotNull] WireMockMiddlewareOptions options, [NotNull] params string[] uriPrefixes)
        {
            Check.NotNull(options, nameof(options));
            Check.NotNullOrEmpty(uriPrefixes, nameof(uriPrefixes));

            foreach (string uriPrefix in uriPrefixes)
            {
                Urls.Add(uriPrefix);

                PortUtil.TryExtractProtocolAndPort(uriPrefix, out string host, out int port);
                Ports.Add(port);
            }

            _options = options;
        }
        public AspNetCoreSelfHost([NotNull] WireMockMiddlewareOptions options, [NotNull] params string[] uriPrefixes)
        {
            Check.NotNull(options, nameof(options));
            Check.NotNullOrEmpty(uriPrefixes, nameof(uriPrefixes));

            _logger = options.Logger ?? new WireMockConsoleLogger();

            foreach (string uriPrefix in uriPrefixes)
            {
                Urls.Add(uriPrefix);

                PortUtil.TryExtractProtocolAndPort(uriPrefix, out string host, out int port);
                Ports.Add(port);
            }

            _options = options;
            _urls    = uriPrefixes;
        }