protected override ITestFrameworkExecutor CreateExecutor(AssemblyName assemblyName) { IHost?host = null; try { var startup = StartupLoader.CreateStartup(StartupLoader.GetStartupType(assemblyName)); if (startup == null) { return(new XunitTestFrameworkExecutor(assemblyName, SourceInformationProvider, DiagnosticMessageSink)); } var hostBuilder = StartupLoader.CreateHostBuilder(startup, assemblyName) ?? new HostBuilder() .ConfigureHostConfiguration(builder => builder.AddInMemoryCollection(new Dictionary <string, string> { { HostDefaults.ApplicationKey, assemblyName.Name ! } })); StartupLoader.ConfigureHost(hostBuilder, startup); StartupLoader.ConfigureServices(hostBuilder, startup); host = hostBuilder.ConfigureServices(services => services .AddSingleton(DiagnosticMessageSink) .TryAddSingleton <ITestOutputHelperAccessor, TestOutputHelperAccessor>()) .Build(); StartupLoader.Configure(host.Services, startup); return(new DependencyInjectionTestFrameworkExecutor(host, null, assemblyName, SourceInformationProvider, DiagnosticMessageSink)); }
/// <summary> /// Ensure the server is built. /// </summary> private void EnsureServer() { if (_server != null) { return; } EnsureDepsFile(); var localDebugPath = EntryPointAssembly .GetCustomAttribute <LocalDebugPathAttribute>(); if (localDebugPath == null) { throw new InvalidOperationException("Invalid assembly information."); } if (!Directory.Exists(localDebugPath.Path)) { throw new InvalidOperationException($"Invalid assembly debug path \"{localDebugPath.Path}\"."); } _host = CreateHostBuilder() .UseEnvironment("Testing") .ConfigureWebHost(builder => { builder.UseContentRoot(localDebugPath.Path); builder.UseTestServer(); }) .Build(); PrepareHost(_host); _host.Start(); _server = (TestServer)_host.Services.GetRequiredService <IServer>(); }
protected IHost GetAppHost() { if (_appHost == null) { try { Init(); _appHost = BuildAppHost(); Logger.LogInformation("Check required modules"); foreach (var module in Modules) { CheckRequiredModules(module); } if (_check) { Console.WriteLine("Check run is successful"); System.Environment.Exit(0); } Log.Logger = _loggerConfiguration.CreateLogger(); } catch (Exception e) { Logger.LogError("Host build error: {ErrorText}", e.ToString()); System.Environment.Exit(255); } } return(_appHost !); }
/// <inheritdoc /> public async Task StartAsync(CancellationToken cancellationToken) { this.logger.LogInformation($"Start initializing admin panel for port {this.settings.Port}."); Configuration.Default.MemoryAllocator = ArrayPoolMemoryAllocator.CreateWithMinimalPooling(); // you might need to allow it first with netsh: // netsh http add urlacl http://+:1234/ user=[Username] this.host = Host.CreateDefaultBuilder() .ConfigureServices(serviceCollection => { serviceCollection.AddSingleton(this.servers); serviceCollection.AddSingleton(this.persistenceContextProvider); serviceCollection.AddSingleton(this.changeListener); serviceCollection.AddSingleton(this.loggerFactory); serviceCollection.AddSingleton(this.plugInChangeListener); serviceCollection.AddBlazoredModal(); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup <Startup>(); webBuilder.UseStaticWebAssets(); // For testing purposes, we use http. Later we need to switch to https. // The self-signed certificate would otherwise cause a lot of warnings in the browser. webBuilder.UseUrls($"http://*:{this.settings.Port}"); }) .Build(); await this.host !.StartAsync(cancellationToken); this.logger.LogInformation($"Admin panel initialized, port {this.settings.Port}."); }
private void EnsureServer() { if (_host == null) { var builder = new HostBuilder() .ConfigureServices(services => { services.AddSingleton <ILoggerFactory>(LoggerFactory); // Registers a service for tests to add new methods services.AddSingleton <DynamicGrpcServiceRegistry>(); }) .ConfigureWebHostDefaults(webHost => { webHost .UseTestServer() .UseStartup <TStartup>(); _configureWebHost?.Invoke(webHost); }); _host = builder.Start(); _server = _host.GetTestServer(); _handler = _server.CreateHandler(); _dynamicGrpc = _server.Services.GetRequiredService <DynamicGrpcServiceRegistry>(); } }
public static async Task Main(string[] args) { IHost?host = CreateHostBuilder(args).Build(); await SetupService.Instance.Initialize(); await host.RunAsync(); }
public TestWebsiteHost StartApiServer(RunEnvironment runEnvironment, string?databaseName = null) { _logger.LogInformation($"{nameof(StartApiServer)}: runEnvironment={runEnvironment}, databaseName={databaseName}"); IOption option = GetOption(runEnvironment, databaseName); var host = new HostBuilder() .ConfigureWebHostDefaults(webBuilder => { webBuilder .UseTestServer() .UseStartup <Startup>(); }) .ConfigureLogging(builder => builder.AddDebug()) .ConfigureServices((hostContext, services) => { services .AddSingleton(option) .AddSingleton <ICosmosPathFinderOption>(option.Store); }); _host = host.Start(); _client = _host.GetTestServer().CreateClient(); InitializeDatabase(_host, option).Wait(); return(this); }
public async Task <IServer> StartAsync( ITestConfigurer testConfigurer, CancellationToken cancellationToken = default) { if (_host == null) { _host = Program.CreateHostBuilder(new string[0]) .ConfigureServices( collection => { collection.AddControllers(options => options.Filters.Add(new HttpResponseExceptionFilter())); testConfigurer.Configure( new SimpleInjectorServiceContainer( collection.BuildServiceProvider() .GetService <Container>())); }) .ConfigureWebHost(builder => builder.UseTestServer()) .Build(); await _host.StartAsync(cancellationToken) .ConfigureAwait(false); } return(this); }
public virtual void Dispose() { _host?.Dispose(); _host = null; _client?.Dispose(); _client = null; }
public static async Task Main(string[] args) { host = CreateHostBuilder(args).Build(); using IServiceScope scope = host.Services.CreateScope(); IServiceProvider services = scope.ServiceProvider; ILogger <Program> logger = services.GetRequiredService <ILogger <Program> >(); ApplicationDbContext context = services.GetRequiredService <ApplicationDbContext>(); await context.Database.MigrateAsync(); await host.RunAsync(); }
public override void Initialize(string uri) { base.Initialize(uri); _host = BitWebHost.CreateWebHost(Array.Empty <string>()) .ConfigureWebHostDefaults(webHostBuilder => { webHostBuilder.UseUrls(uri); }) .Start(); }
public override void OnCreate() { if (!(Activator.CreateInstance(typeof(TApplication)) is TApplication app)) { throw new InvalidOperationException($"We weren't able to create the App {typeof(TApplication)}"); } _host = app.CreateBuilder().ConfigureServices(ConfigureNativeServices).Build(app); //_host.Start(); base.OnCreate(); }
public IHost?BuildDefaultHost() { _defaultStartupType = StartupLoader.GetStartupType(_assemblyName); _host = StartupLoader.CreateHost(_defaultStartupType, _assemblyName, _diagnosticMessageSink); if (_host != null) { _hosts.Add(_host); } return(_host); }
public void When_MigratingDatabaseWithNullHost_Expect_ArgumentNullException() { // Arrange IHost?host = null; // Act Exception exception = Record.Exception(() => host !.MigrateDatabase()); // Assert ArgumentNullException argumentNullException = Assert.IsType <ArgumentNullException>(exception); Assert.Equal("host", argumentNullException.ParamName); }
public void When_RegisteringGlobalTracerWithNullHost_Expect_ArgumentNullException() { // Arrange IHost?host = null; // Act Exception exception = Record.Exception(() => host !.RegisterGlobalTracer()); // Assert ArgumentNullException argumentNullException = Assert.IsType <ArgumentNullException>(exception); Assert.Equal("host", argumentNullException.ParamName); }
protected MvcTestFixture() { _url = $"http://localhost:{FreeTcpPort()}"; _host = Host.CreateDefaultBuilder() .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseUrls(_url); webBuilder.ConfigureServices(ConfigureServices); webBuilder.Configure(Configure); }) .Build(); _host.Start(); }
public static App?Init(Action <HostBuilderContext, IServiceCollection> nativeConfigureServices, object?parentActivityOrWindow = null) { var configuration = new ConfigurationBuilder().Build(); var hostBuilder = new ClientHostBuilder().ConfigureHost(configuration, nativeConfigureServices); host?.Dispose(); host = hostBuilder.Build(); host.Start(); // Save our service provider so we can use it later. App.ServiceProvider = host.Services; App.ParentActivityOrWindow = parentActivityOrWindow; return(App.ServiceProvider.GetService <App>()); }
public DependencyInjectionTestFrameworkExecutor(IHost?host, Exception?exception, AssemblyName assemblyName, ISourceInformationProvider sourceInformationProvider, IMessageSink diagnosticMessageSink) : base(assemblyName, sourceInformationProvider, diagnosticMessageSink) { _host = host; _exception = exception; if (_host != null) { DisposalTracker.Add(_host); } }
public override void OnFrameworkInitializationCompleted() { base.OnFrameworkInitializationCompleted(); Host = CreateHost(); Host.Start(); if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { desktop.Exit += Desktop_Exit; desktop.MainWindow = Host.Services.GetService <MainWindow>(); desktop.MainWindow !.Content = Host.Services.GetService <MainViewModel>(); } }
public static void Main(string[] args) { WebHost = CreateHostBuilder(args).Build(); try { WebHost.Run(); } catch (Exception e) { Console.WriteLine("Shutdown from code! " + e); } Console.Write("Play your game!"); }
public async Task <KestrelHost> StartAsync(HttpProtocols protocols = HttpProtocols.Http2) { GrpcChannelExtensions.Http2UnencryptedSupport = true; _host = Host .CreateDefaultBuilder() .ConfigureServices(services => { services.AddGrpc(); services.AddServiceModelGrpc(); _configureServices?.Invoke(services); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.Configure(app => { app.UseRouting(); _configureApp?.Invoke(app); if (_configureEndpoints != null) { app.UseEndpoints(_configureEndpoints); } }); webBuilder.UseKestrel(o => o.ListenLocalhost(_port, l => l.Protocols = protocols)); }) .Build(); try { await _host.StartAsync().ConfigureAwait(false); } catch { await DisposeAsync().ConfigureAwait(false); throw; } ClientFactory = new ClientFactory(_clientFactoryDefaultOptions); Channel = GrpcChannelFactory.CreateChannel(_channelType, "localhost", DefaultPort); return(this); }
/// <inheritdoc /> protected override async void RunTestCases( IEnumerable <IXunitTestCase> testCases, IMessageSink executionMessageSink, ITestFrameworkExecutionOptions executionOptions) { var exceptions = new List <Exception>(); IHost?host = null; try { host = _hostManager.BuildDefaultHost(); } catch (TargetInvocationException tie) { exceptions.Add(tie.InnerException); } catch (Exception ex) { exceptions.Add(ex); } // ReSharper disable once PossibleMultipleEnumeration var hostMap = testCases .GroupBy(tc => tc.TestMethod.TestClass, TestClassComparer.Instance) .ToDictionary(group => group.Key, group => { try { return(_hostManager.GetHost(group.Key.Class.ToRuntimeType())); } catch (TargetInvocationException tie) { exceptions.Add(tie.InnerException); } catch (Exception ex) { exceptions.Add(ex); } return(null); }); try { await _hostManager.StartAsync(default).ConfigureAwait(false);