Exemple #1
0
        public TestServer(RequestDelegate app, TestServiceContext context, Action <KestrelServerOptions> configureKestrel, Action <IServiceCollection> configureServices)
        {
            _app              = app;
            Context           = context;
            _memoryPool       = context.MemoryPoolFactory();
            _transportFactory = new InMemoryTransportFactory();
            HttpClientSlim    = new InMemoryHttpClientSlim(this);

            var hostBuilder = new HostBuilder()
                              .ConfigureWebHost(webHostBuilder =>
            {
                webHostBuilder
                .UseSetting(WebHostDefaults.ShutdownTimeoutKey, TestConstants.DefaultTimeout.TotalSeconds.ToString(CultureInfo.InvariantCulture))
                .Configure(app => { app.Run(_app); });
            })
                              .ConfigureServices(services =>
            {
                configureServices(services);

                services.AddSingleton <IStartup>(this);
                services.AddSingleton(context.LoggerFactory);

                services.AddSingleton <IServer>(sp =>
                {
                    context.ServerOptions.ApplicationServices = sp;
                    configureKestrel(context.ServerOptions);
                    return(new KestrelServerImpl(_transportFactory, context));
                });
            });

            _host = hostBuilder.Build();
            _host.Start();
        }
Exemple #2
0
        public TestServer(RequestDelegate app, TestServiceContext context, Action <KestrelServerOptions> configureKestrel, Action <IServiceCollection> configureServices)
        {
            _app    = app;
            Context = context;

            _host = TransportSelector.GetHostBuilder(context.MemoryPoolFactory, context.ServerOptions.Limits.MaxRequestBufferSize)
                    .ConfigureWebHost(webHostBuilder =>
            {
                webHostBuilder
                .UseKestrel(options =>
                {
                    configureKestrel(options);
                    _listenOptions = options.ListenOptions.First();
                })
                .ConfigureServices(services =>
                {
                    services.AddSingleton <IStartup>(this);
                    services.AddSingleton(context.LoggerFactory);
                    services.AddSingleton <IServer>(sp =>
                    {
                        // Manually configure options on the TestServiceContext.
                        // We're doing this so we can use the same instance that was passed in
                        var configureOptions = sp.GetServices <IConfigureOptions <KestrelServerOptions> >();
                        foreach (var c in configureOptions)
                        {
                            c.Configure(context.ServerOptions);
                        }

                        return(new KestrelServer(new List <IConnectionListenerFactory>()
                        {
                            sp.GetRequiredService <IConnectionListenerFactory>()
                        }, context));
                    });
                    configureServices(services);
                })
                .UseSetting(WebHostDefaults.ApplicationKey, typeof(TestServer).GetTypeInfo().Assembly.FullName)
                .UseSetting(WebHostDefaults.ShutdownTimeoutKey, TestConstants.DefaultTimeout.TotalSeconds.ToString())
                .Configure(app => { app.Run(_app); });
            })
                    .ConfigureServices(services =>
            {
                services.Configure <HostOptions>(option =>
                {
                    option.ShutdownTimeout = TestConstants.DefaultTimeout;
                });
            })
                    .Build();

            _host.Start();

            Context.Log.LogDebug($"TestServer is listening on port {Port}");
        }
Exemple #3
0
        public TestServer(RequestDelegate app, TestServiceContext context, Action <KestrelServerOptions> configureKestrel, Action <IServiceCollection> configureServices)
        {
            _app              = app;
            Context           = context;
            _memoryPool       = context.MemoryPoolFactory();
            _transportFactory = new InMemoryTransportFactory();
            HttpClientSlim    = new InMemoryHttpClientSlim(this);

            var hostBuilder = new HostBuilder()
                              .ConfigureWebHost(webHostBuilder =>
            {
                webHostBuilder
                .UseSetting(WebHostDefaults.ShutdownTimeoutKey, TestConstants.DefaultTimeout.TotalSeconds.ToString(CultureInfo.InvariantCulture))
                .Configure(app => { app.Run(_app); });
            })
                              .ConfigureServices(services =>
            {
                configureServices(services);

                // Ensure there is at least one multiplexed connection lister factory if none was added to services.
                if (!services.Any(d => d.ServiceType == typeof(IMultiplexedConnectionListenerFactory)))
                {
                    // Mock multiplexed connection listner is added so Kestrel doesn't error
                    // when a HTTP/3 endpoint is configured.
                    services.AddSingleton <IMultiplexedConnectionListenerFactory>(new MockMultiplexedConnectionListenerFactory());
                }

                services.AddSingleton <IStartup>(this);
                services.AddSingleton(context.LoggerFactory);

                services.AddSingleton <IServer>(sp =>
                {
                    context.ServerOptions.ApplicationServices = sp;
                    configureKestrel(context.ServerOptions);
                    return(new KestrelServerImpl(
                               new IConnectionListenerFactory[] { _transportFactory },
                               sp.GetServices <IMultiplexedConnectionListenerFactory>(),
                               context));
                });
            });

            _host = hostBuilder.Build();
            _host.Start();
        }