Move default hosting environment code

This commit is contained in:
Glenn Condron 2014-11-21 17:04:04 -08:00
parent 2f02fc6091
commit bd5c07d66a
7 changed files with 44 additions and 51 deletions

View File

@ -1,22 +1,23 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using Microsoft.Framework.ConfigurationModel;
namespace Microsoft.AspNet.Hosting namespace Microsoft.AspNet.Hosting
{ {
public class ConfigureHostingEnvironment : IConfigureHostingEnvironment internal class ConfigureHostingEnvironment : IConfigureHostingEnvironment
{ {
private readonly Action<IHostingEnvironment> _action; private IConfiguration _config;
private const string EnvironmentKey = "KRE_ENV";
public ConfigureHostingEnvironment(Action<IHostingEnvironment> configure) public ConfigureHostingEnvironment(IConfiguration config)
{ {
_action = configure; _config = config;
} }
public void Configure(IHostingEnvironment hostingEnv) public void Configure(IHostingEnvironment hostingEnv)
{ {
_action.Invoke(hostingEnv); hostingEnv.EnvironmentName = _config.Get(EnvironmentKey) ?? hostingEnv.EnvironmentName;
} }
} }
} }

View File

@ -8,9 +8,12 @@ namespace Microsoft.AspNet.Hosting
{ {
public class HostingEnvironment : IHostingEnvironment public class HostingEnvironment : IHostingEnvironment
{ {
private const string DefaultEnvironmentName = "Development";
public HostingEnvironment(IApplicationEnvironment appEnv, IEnumerable<IConfigureHostingEnvironment> configures) public HostingEnvironment(IApplicationEnvironment appEnv, IEnumerable<IConfigureHostingEnvironment> configures)
{ {
WebRoot = HostingUtilities.GetWebRoot(appEnv.ApplicationBasePath); WebRoot = HostingUtilities.GetWebRoot(appEnv.ApplicationBasePath);
EnvironmentName = DefaultEnvironmentName;
foreach (var configure in configures) foreach (var configure in configures)
{ {
configure.Configure(this); configure.Configure(this);

View File

@ -40,6 +40,7 @@ namespace Microsoft.AspNet.Hosting
var services = Import(fallbackServices); var services = Import(fallbackServices);
services.Add(GetDefaultServices(configuration)); services.Add(GetDefaultServices(configuration));
services.AddSingleton<IServiceManifest>(sp => new HostingManifest(fallbackServices)); services.AddSingleton<IServiceManifest>(sp => new HostingManifest(fallbackServices));
services.AddInstance<IConfigureHostingEnvironment>(new ConfigureHostingEnvironment(configuration));
return services; return services;
} }

View File

@ -15,8 +15,6 @@ namespace Microsoft.AspNet.Hosting
public class Program public class Program
{ {
private const string HostingIniFile = "Microsoft.AspNet.Hosting.ini"; private const string HostingIniFile = "Microsoft.AspNet.Hosting.ini";
private const string DefaultEnvironmentName = "Development";
private const string EnvironmentKey = "KRE_ENV";
private readonly IServiceProvider _serviceProvider; private readonly IServiceProvider _serviceProvider;
@ -37,12 +35,8 @@ namespace Microsoft.AspNet.Hosting
config.AddEnvironmentVariables(); config.AddEnvironmentVariables();
config.AddCommandLine(args); config.AddCommandLine(args);
var serviceCollection = HostingServices.Create(_serviceProvider, config); var services = HostingServices.Create(_serviceProvider, config)
serviceCollection.AddInstance<IConfigureHostingEnvironment>(new ConfigureHostingEnvironment(env => .BuildServiceProvider();
{
env.EnvironmentName = config.Get(EnvironmentKey) ?? DefaultEnvironmentName;
}));
var services = serviceCollection.BuildServiceProvider();
var appEnv = services.GetRequiredService<IApplicationEnvironment>(); var appEnv = services.GetRequiredService<IApplicationEnvironment>();
var hostingEnv = services.GetRequiredService<IHostingEnvironment>(); var hostingEnv = services.GetRequiredService<IHostingEnvironment>();

View File

@ -54,10 +54,7 @@ namespace Microsoft.AspNet.TestHost
public static TestServer Create(IServiceProvider serviceProvider, Action<IApplicationBuilder> app) public static TestServer Create(IServiceProvider serviceProvider, Action<IApplicationBuilder> app)
{ {
var services = HostingServices.Create(serviceProvider); var appServices = HostingServices.Create(serviceProvider).BuildServiceProvider();
services.AddSingleton<IConfigureHostingEnvironment, ConfigureTestHostingEnvironment>();
var appServices = services.BuildServiceProvider();
var config = new Configuration(); var config = new Configuration();
return new TestServer(config, appServices, app); return new TestServer(config, appServices, app);
} }
@ -127,10 +124,5 @@ namespace Microsoft.AspNet.TestHost
get { return TestServer.ServerName; } get { return TestServer.ServerName; }
} }
} }
private class ConfigureTestHostingEnvironment : ConfigureHostingEnvironment
{
public ConfigureTestHostingEnvironment() : base(env => env.EnvironmentName = DefaultEnvironmentName) { }
}
} }
} }

View File

@ -1,14 +1,15 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup> <DevelopmentServerPort>50390</DevelopmentServerPort>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" /> </PropertyGroup>
<PropertyGroup Label="Globals"> <Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
<ProjectGuid>d4f18d58-52b1-435d-a012-10f2cdf158c4</ProjectGuid> <PropertyGroup Label="Globals">
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath> <ProjectGuid>d4f18d58-52b1-435d-a012-10f2cdf158c4</ProjectGuid>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath> <BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
</PropertyGroup> <OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" /> </PropertyGroup>
</Project> <Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@ -1,14 +1,15 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup> <DevelopmentServerPort>50389</DevelopmentServerPort>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" /> </PropertyGroup>
<PropertyGroup Label="Globals"> <Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
<ProjectGuid>0acb2719-9484-49b5-b8e3-117091192511</ProjectGuid> <PropertyGroup Label="Globals">
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath> <ProjectGuid>0acb2719-9484-49b5-b8e3-117091192511</ProjectGuid>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath> <BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
</PropertyGroup> <OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" /> </PropertyGroup>
</Project> <Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>