public HostingEnvironment(IApplicationEnvironment appEnvironment, IEnumerable<IConfigureHostingEnvironment> configures) { EnvironmentName = DefaultEnvironmentName; WebRoot = HostingUtilities.GetWebRoot(appEnvironment.ApplicationBasePath); WebRootFileSystem = new PhysicalFileSystem(WebRoot); foreach (var configure in configures) { configure.Configure(this); } }
public static void Initialize(this IHostingEnvironment hostingEnvironment, string applicationBasePath, string environmentName) { hostingEnvironment.WebRootPath = HostingUtilities.GetWebRoot(applicationBasePath); if (!Directory.Exists(hostingEnvironment.WebRootPath)) { Directory.CreateDirectory(hostingEnvironment.WebRootPath); } hostingEnvironment.WebRootFileProvider = new PhysicalFileProvider(hostingEnvironment.WebRootPath); hostingEnvironment.EnvironmentName = environmentName ?? hostingEnvironment.EnvironmentName; }
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(); }