public void Main(string[] args) { var config = new Configuration(); if (File.Exists(HostingIniFile)) { config.AddIniFile(HostingIniFile); } config.AddEnvironmentVariables(); config.AddCommandLine(args); var serviceCollection = new ServiceCollection(); serviceCollection.Add(HostingServices.GetDefaultServices(config)); var services = serviceCollection.BuildServiceProvider(_serviceProvider); var appEnvironment = _serviceProvider.GetService <IApplicationEnvironment>(); var context = new HostingContext() { Services = services, Configuration = config, ServerName = config.Get("server"), // TODO: Key names ApplicationName = config.Get("app") // TODO: Key names ?? appEnvironment.ApplicationName, EnvironmentName = config.Get("env") ?? "Development" }; var engine = services.GetService <IHostingEngine>(); if (engine == null) { throw new Exception("TODO: IHostingEngine service not available exception"); } var appShutdownService = _serviceProvider.GetService <IApplicationShutdown>(); if (appShutdownService == null) { throw new Exception("TODO: IApplicationShutdown service not available"); } var shutdownHandle = new ManualResetEvent(false); var serverShutdown = engine.Start(context); appShutdownService.ShutdownRequested.Register(() => { serverShutdown.Dispose(); shutdownHandle.Set(); }); var ignored = Task.Run(() => { Console.WriteLine("Started"); Console.ReadLine(); appShutdownService.RequestShutdown(); }); shutdownHandle.WaitOne(); }
public void HostingEngineCanBeStarted() { var serviceCollection = new ServiceCollection(); serviceCollection.Add(HostingServices.GetDefaultServices()); var services = serviceCollection.BuildServiceProvider(); var engine = services.GetRequiredService <IHostingEngine>(); var context = new HostingContext { ServerFactory = this, Services = services, ApplicationName = "Microsoft.AspNet.Hosting.Tests" }; var engineStart = engine.Start(context); Assert.NotNull(engineStart); Assert.Equal(1, _startInstances.Count); Assert.Equal(0, _startInstances[0].DisposeCalls); engineStart.Dispose(); Assert.Equal(1, _startInstances[0].DisposeCalls); }
public void HostingEngineCanBeResolvedWithDefaultServices() { var serviceCollection = new ServiceCollection(); serviceCollection.Add(HostingServices.GetDefaultServices()); var services = serviceCollection.BuildServiceProvider(); var engine = services.GetRequiredService <IHostingEngine>(); Assert.NotNull(engine); }
public void StartupWithNoConfigureThrows() { var serviceCollection = new ServiceCollection(); serviceCollection.Add(HostingServices.GetDefaultServices()); serviceCollection.AddInstance <IFakeStartupCallback>(this); var services = serviceCollection.BuildServiceProvider(); var manager = services.GetRequiredService <IStartupManager>(); var ex = Assert.Throws <Exception>(() => manager.LoadStartup("Microsoft.AspNet.Hosting.Tests", "Boom")); Assert.True(ex.Message.Contains("ConfigureBoom or Configure method not found")); }
public void StartupClassConfigureServicesThatFallsbackToApplicationServices(string env) { var serviceCollection = new ServiceCollection(); serviceCollection.Add(HostingServices.GetDefaultServices()); var services = serviceCollection.BuildServiceProvider(); var manager = services.GetRequiredService <IStartupManager>(); var startup = manager.LoadStartup("Microsoft.AspNet.Hosting.Tests", env); var app = new ApplicationBuilder(services); startup.Invoke(app); Assert.Equal(services, app.ApplicationServices); }
public void StartupClassMayHaveHostingServicesInjected() { var serviceCollection = new ServiceCollection(); serviceCollection.Add(HostingServices.GetDefaultServices()); serviceCollection.AddInstance <IFakeStartupCallback>(this); var services = serviceCollection.BuildServiceProvider(); var manager = services.GetRequiredService <IStartupManager>(); var startup = manager.LoadStartup("Microsoft.AspNet.Hosting.Tests", "WithServices"); startup.Invoke(null); Assert.Equal(2, _configurationMethodCalledList.Count); }
public void StartupClassAddsConfigureServicesToApplicationServices(string environment) { var serviceCollection = new ServiceCollection(); serviceCollection.Add(HostingServices.GetDefaultServices()); var services = serviceCollection.BuildServiceProvider(); var manager = services.GetRequiredService <IStartupManager>(); var startup = manager.LoadStartup("Microsoft.AspNet.Hosting.Tests", environment ?? ""); var app = new ApplicationBuilder(services); startup.Invoke(app); var options = app.ApplicationServices.GetRequiredService <IOptions <FakeOptions> >().Options; Assert.NotNull(options); Assert.True(options.Configured); Assert.Equal(environment, options.Environment); }
public static IDisposable Start() { var serviceCollection = new ServiceCollection(); serviceCollection.Add(HostingServices.GetDefaultServices()); var context = new HostingContext { Services = serviceCollection.BuildServiceProvider() }; var engine = context.Services.GetService <IHostingEngine>(); if (engine == null) { throw new Exception("TODO: IHostingEngine service not available exception"); } return(engine.Start(context)); }
public void StartupClassWithConfigureServicesAndUseServicesAddsBothToServices() { var serviceCollection = new ServiceCollection(); serviceCollection.Add(HostingServices.GetDefaultServices()); var services = serviceCollection.BuildServiceProvider(); var manager = services.GetRequiredService <IStartupManager>(); var startup = manager.LoadStartup("Microsoft.AspNet.Hosting.Tests", "UseServices"); var app = new ApplicationBuilder(services); startup.Invoke(app); Assert.NotNull(app.ApplicationServices.GetRequiredService <FakeService>()); Assert.NotNull(app.ApplicationServices.GetRequiredService <IFakeService>()); var options = app.ApplicationServices.GetRequiredService <IOptions <FakeOptions> >().Options; Assert.NotNull(options); Assert.Equal("Configured", options.Message); Assert.False(options.Configured); // REVIEW: why doesn't the ConfigureServices ConfigureOptions get run? }
public void Main(string[] args) { var config = new Configuration(); if (File.Exists(HostingIniFile)) { config.AddIniFile(HostingIniFile); } config.AddEnvironmentVariables(); config.AddCommandLine(args); var appEnv = _serviceProvider.GetRequiredService <IApplicationEnvironment>(); var hostingEnv = new HostingEnvironment() { EnvironmentName = config.Get(EnvironmentKey) ?? DefaultEnvironmentName, WebRoot = HostingUtilities.GetWebRoot(appEnv.ApplicationBasePath), }; var serviceCollection = new ServiceCollection(); serviceCollection.Add(HostingServices.GetDefaultServices(config)); serviceCollection.AddInstance <IHostingEnvironment>(hostingEnv); // The application name is a "good enough" mechanism to identify this application // on the machine and to prevent subkeys from being shared across multiple applications // by default. serviceCollection.Configure <DataProtectionOptions>(options => { options.ApplicationDiscriminator = appEnv.ApplicationName; }); var services = serviceCollection.BuildServiceProvider(_serviceProvider); var context = new HostingContext() { Services = services, Configuration = config, ServerName = config.Get("server"), // TODO: Key names ApplicationName = config.Get("app") // TODO: Key names ?? appEnv.ApplicationName, EnvironmentName = hostingEnv.EnvironmentName, }; var engine = services.GetRequiredService <IHostingEngine>(); var appShutdownService = _serviceProvider.GetRequiredService <IApplicationShutdown>(); var shutdownHandle = new ManualResetEvent(false); var serverShutdown = engine.Start(context); appShutdownService.ShutdownRequested.Register(() => { serverShutdown.Dispose(); shutdownHandle.Set(); }); var ignored = Task.Run(() => { Console.WriteLine("Started"); Console.ReadLine(); appShutdownService.RequestShutdown(); }); shutdownHandle.WaitOne(); }