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.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.Framework.ConfigurationModel;
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)
{
_action.Invoke(hostingEnv);
hostingEnv.EnvironmentName = _config.Get(EnvironmentKey) ?? hostingEnv.EnvironmentName;
}
}
}

View File

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

View File

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

View File

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

View File

@ -54,10 +54,7 @@ namespace Microsoft.AspNet.TestHost
public static TestServer Create(IServiceProvider serviceProvider, Action<IApplicationBuilder> app)
{
var services = HostingServices.Create(serviceProvider);
services.AddSingleton<IConfigureHostingEnvironment, ConfigureTestHostingEnvironment>();
var appServices = services.BuildServiceProvider();
var appServices = HostingServices.Create(serviceProvider).BuildServiceProvider();
var config = new Configuration();
return new TestServer(config, appServices, app);
}
@ -127,10 +124,5 @@ namespace Microsoft.AspNet.TestHost
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"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>d4f18d58-52b1-435d-a012-10f2cdf158c4</ProjectGuid>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<DevelopmentServerPort>50390</DevelopmentServerPort>
</PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>d4f18d58-52b1-435d-a012-10f2cdf158c4</ProjectGuid>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

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