#71 - Create IHostingEnvironment.

This commit is contained in:
Chris Ross 2014-09-11 12:32:24 -07:00
parent 64455620a0
commit 8a66871139
11 changed files with 110 additions and 29 deletions

View File

@ -0,0 +1,12 @@
// 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.
namespace Microsoft.AspNet.Hosting
{
public class HostingEnvironment : IHostingEnvironment
{
public string EnvironmentName { get; set; }
public string WebRoot { get; set; }
}
}

View File

@ -16,10 +16,12 @@
// permissions and limitations under the License. // permissions and limitations under the License.
using System; using System;
using System.IO;
using Microsoft.Framework.ConfigurationModel;
namespace Microsoft.AspNet.Hosting namespace Microsoft.AspNet.Hosting
{ {
internal static class Utilities public static class HostingUtilities
{ {
internal static Tuple<string, string> SplitTypeName(string identifier) internal static Tuple<string, string> SplitTypeName(string identifier)
{ {
@ -33,5 +35,20 @@ namespace Microsoft.AspNet.Hosting
} }
return new Tuple<string, string>(typeName, assemblyName); return new Tuple<string, string>(typeName, assemblyName);
} }
public static string GetWebRoot(string applicationBasePath)
{
try
{
var config = new Configuration();
config.AddJsonFile(Path.Combine(applicationBasePath, "project.json"));
var webroot = config.Get("webroot") ?? string.Empty;
return Path.GetFullPath(Path.Combine(applicationBasePath, webroot));
}
catch (Exception)
{
return applicationBasePath;
}
}
} }
} }

View File

@ -0,0 +1,15 @@
// 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 Microsoft.Framework.Runtime;
namespace Microsoft.AspNet.Hosting
{
[AssemblyNeutral]
public interface IHostingEnvironment
{
string EnvironmentName { get; }
string WebRoot { get; }
}
}

View File

@ -29,6 +29,8 @@ 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;
@ -47,33 +49,31 @@ namespace Microsoft.AspNet.Hosting
config.AddEnvironmentVariables(); config.AddEnvironmentVariables();
config.AddCommandLine(args); config.AddCommandLine(args);
var appEnv = _serviceProvider.GetService<IApplicationEnvironment>();
var hostingEnv = new HostingEnvironment()
{
EnvironmentName = config.Get(EnvironmentKey) ?? DefaultEnvironmentName,
WebRoot = HostingUtilities.GetWebRoot(appEnv.ApplicationBasePath),
};
var serviceCollection = new ServiceCollection(); var serviceCollection = new ServiceCollection();
serviceCollection.Add(HostingServices.GetDefaultServices(config)); serviceCollection.Add(HostingServices.GetDefaultServices(config));
serviceCollection.AddInstance<IHostingEnvironment>(hostingEnv);
var services = serviceCollection.BuildServiceProvider(_serviceProvider); var services = serviceCollection.BuildServiceProvider(_serviceProvider);
var appEnvironment = _serviceProvider.GetService<IApplicationEnvironment>();
var context = new HostingContext() var context = new HostingContext()
{ {
Services = services, Services = services,
Configuration = config, Configuration = config,
ServerName = config.Get("server"), // TODO: Key names ServerName = config.Get("server"), // TODO: Key names
ApplicationName = config.Get("app") // TODO: Key names ApplicationName = config.Get("app") // TODO: Key names
?? appEnvironment.ApplicationName, ?? appEnv.ApplicationName,
EnvironmentName = config.Get("env") ?? "Development" EnvironmentName = hostingEnv.EnvironmentName,
}; };
var engine = services.GetService<IHostingEngine>(); var engine = services.GetService<IHostingEngine>();
if (engine == null)
{
throw new Exception("TODO: IHostingEngine service not available exception");
}
var appShutdownService = _serviceProvider.GetService<IApplicationShutdown>(); var appShutdownService = _serviceProvider.GetService<IApplicationShutdown>();
if (appShutdownService == null)
{
throw new Exception("TODO: IApplicationShutdown service not available");
}
var shutdownHandle = new ManualResetEvent(false); var shutdownHandle = new ManualResetEvent(false);
var serverShutdown = engine.Start(context); var serverShutdown = engine.Start(context);

View File

@ -38,7 +38,7 @@ namespace Microsoft.AspNet.Hosting.Server
throw new ArgumentException(string.Empty, "serverFactoryIdentifier"); throw new ArgumentException(string.Empty, "serverFactoryIdentifier");
} }
var nameParts = Utilities.SplitTypeName(serverFactoryIdentifier); var nameParts = HostingUtilities.SplitTypeName(serverFactoryIdentifier);
string typeName = nameParts.Item1; string typeName = nameParts.Item1;
string assemblyName = nameParts.Item2; string assemblyName = nameParts.Item2;

View File

@ -6,6 +6,7 @@
"Microsoft.AspNet.PipelineCore": "1.0.0-*", "Microsoft.AspNet.PipelineCore": "1.0.0-*",
"Microsoft.AspNet.Security.DataProtection": "1.0.0-*", "Microsoft.AspNet.Security.DataProtection": "1.0.0-*",
"Microsoft.Framework.ConfigurationModel": "1.0.0-*", "Microsoft.Framework.ConfigurationModel": "1.0.0-*",
"Microsoft.Framework.ConfigurationModel.Json": "1.0.0-*",
"Microsoft.Framework.DependencyInjection": "1.0.0-*", "Microsoft.Framework.DependencyInjection": "1.0.0-*",
"Microsoft.Framework.Logging": "1.0.0-*", "Microsoft.Framework.Logging": "1.0.0-*",
"Microsoft.Framework.Runtime.Interfaces": "1.0.0-*" "Microsoft.Framework.Runtime.Interfaces": "1.0.0-*"

View File

@ -17,7 +17,8 @@ namespace Microsoft.AspNet.TestHost
{ {
public class TestServer : IServerFactory, IDisposable public class TestServer : IServerFactory, IDisposable
{ {
private static readonly string ServerName = typeof(TestServer).FullName; private const string DefaultEnvironmentName = "Development";
private const string ServerName = nameof(TestServer);
private static readonly ServerInformation ServerInfo = new ServerInformation(); private static readonly ServerInformation ServerInfo = new ServerInformation();
private Func<object, Task> _appDelegate; private Func<object, Task> _appDelegate;
private IDisposable _appInstance; private IDisposable _appInstance;
@ -25,15 +26,11 @@ namespace Microsoft.AspNet.TestHost
public TestServer(IConfiguration config, IServiceProvider serviceProvider, Action<IApplicationBuilder> appStartup) public TestServer(IConfiguration config, IServiceProvider serviceProvider, Action<IApplicationBuilder> appStartup)
{ {
var env = serviceProvider.GetService<IApplicationEnvironment>(); var appEnv = serviceProvider.GetService<IApplicationEnvironment>();
if (env == null)
{
throw new ArgumentException("IApplicationEnvironment couldn't be resolved.", "serviceProvider");
}
HostingContext hostContext = new HostingContext() HostingContext hostContext = new HostingContext()
{ {
ApplicationName = env.ApplicationName, ApplicationName = appEnv.ApplicationName,
Configuration = config, Configuration = config,
ServerFactory = this, ServerFactory = this,
Services = serviceProvider, Services = serviceProvider,
@ -51,14 +48,22 @@ namespace Microsoft.AspNet.TestHost
public static TestServer Create(IServiceProvider provider, Action<IApplicationBuilder> app) public static TestServer Create(IServiceProvider provider, Action<IApplicationBuilder> app)
{ {
var appEnv = provider.GetService<IApplicationEnvironment>();
var hostingEnv = new HostingEnvironment()
{
EnvironmentName = DefaultEnvironmentName,
WebRoot = HostingUtilities.GetWebRoot(appEnv.ApplicationBasePath),
};
var collection = new ServiceCollection(); var collection = new ServiceCollection();
var hostingServices = HostingServices.GetDefaultServices(); collection.Add(HostingServices.GetDefaultServices());
collection.AddInstance<IHostingEnvironment>(hostingEnv);
var appServices = collection.BuildServiceProvider(provider);
var config = new Configuration(); var config = new Configuration();
collection.Add(hostingServices); return new TestServer(config, appServices, app);
var serviceProvider = collection.BuildServiceProvider(provider);
return new TestServer(config, serviceProvider, app);
} }
public HttpMessageHandler CreateHandler() public HttpMessageHandler CreateHandler()

View File

@ -0,0 +1,17 @@
// 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 Xunit;
namespace Microsoft.AspNet.Hosting.Tests
{
public class HostingUtilitiesTests
{
[Fact]
public void ReadWebRootFromProjectJson()
{
var root = HostingUtilities.GetWebRoot(".");
Assert.True(root.EndsWith("testroot"));
}
}
}

View File

@ -13,5 +13,6 @@
}, },
"commands": { "commands": {
"test": "Xunit.KRunner" "test": "Xunit.KRunner"
} },
"webroot" : "testroot"
} }

View File

@ -2,10 +2,12 @@
// 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 System.IO;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.DependencyInjection.Fallback; using Microsoft.Framework.DependencyInjection.Fallback;
@ -54,6 +56,16 @@ namespace Microsoft.AspNet.TestHost
Assert.Equal("CreateInvokesApp", result); Assert.Equal("CreateInvokesApp", result);
} }
[Fact]
public void WebRootCanBeResolvedFromProjectJson()
{
TestServer server = TestServer.Create(app =>
{
var env = app.ApplicationServices.GetService<IHostingEnvironment>();
Assert.Equal(Path.GetFullPath("testroot"), env.WebRoot);
});
}
[Fact] [Fact]
public async Task DisposeStreamIgnored() public async Task DisposeStreamIgnored()
{ {

View File

@ -14,5 +14,6 @@
"System.Runtime": "" "System.Runtime": ""
} }
} }
} },
"webroot" : "testroot"
} }