diff --git a/src/Microsoft.AspNet.Hosting/HostingEnvironment.cs b/src/Microsoft.AspNet.Hosting/HostingEnvironment.cs new file mode 100644 index 00000000..3881faa3 --- /dev/null +++ b/src/Microsoft.AspNet.Hosting/HostingEnvironment.cs @@ -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; } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Hosting/Utilities.cs b/src/Microsoft.AspNet.Hosting/HostingUtilities.cs similarity index 66% rename from src/Microsoft.AspNet.Hosting/Utilities.cs rename to src/Microsoft.AspNet.Hosting/HostingUtilities.cs index c1795d0a..873fd87f 100644 --- a/src/Microsoft.AspNet.Hosting/Utilities.cs +++ b/src/Microsoft.AspNet.Hosting/HostingUtilities.cs @@ -16,10 +16,12 @@ // permissions and limitations under the License. using System; +using System.IO; +using Microsoft.Framework.ConfigurationModel; namespace Microsoft.AspNet.Hosting { - internal static class Utilities + public static class HostingUtilities { internal static Tuple SplitTypeName(string identifier) { @@ -33,5 +35,20 @@ internal static Tuple SplitTypeName(string identifier) } return new Tuple(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; + } + } } } diff --git a/src/Microsoft.AspNet.Hosting/IHostingEnvironment.cs b/src/Microsoft.AspNet.Hosting/IHostingEnvironment.cs new file mode 100644 index 00000000..d510d0ba --- /dev/null +++ b/src/Microsoft.AspNet.Hosting/IHostingEnvironment.cs @@ -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; } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Hosting/Program.cs b/src/Microsoft.AspNet.Hosting/Program.cs index b1f694e0..6afeacd0 100644 --- a/src/Microsoft.AspNet.Hosting/Program.cs +++ b/src/Microsoft.AspNet.Hosting/Program.cs @@ -29,6 +29,8 @@ 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; @@ -47,33 +49,31 @@ public void Main(string[] args) config.AddEnvironmentVariables(); config.AddCommandLine(args); + var appEnv = _serviceProvider.GetService(); + + 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(hostingEnv); var services = serviceCollection.BuildServiceProvider(_serviceProvider); - var appEnvironment = _serviceProvider.GetService(); - 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" + ?? appEnv.ApplicationName, + EnvironmentName = hostingEnv.EnvironmentName, }; var engine = services.GetService(); - if (engine == null) - { - throw new Exception("TODO: IHostingEngine service not available exception"); - } - var appShutdownService = _serviceProvider.GetService(); - if (appShutdownService == null) - { - throw new Exception("TODO: IApplicationShutdown service not available"); - } var shutdownHandle = new ManualResetEvent(false); var serverShutdown = engine.Start(context); diff --git a/src/Microsoft.AspNet.Hosting/Server/ServerManager.cs b/src/Microsoft.AspNet.Hosting/Server/ServerManager.cs index 899e695f..726e6746 100644 --- a/src/Microsoft.AspNet.Hosting/Server/ServerManager.cs +++ b/src/Microsoft.AspNet.Hosting/Server/ServerManager.cs @@ -38,7 +38,7 @@ public IServerFactory GetServerFactory(string serverFactoryIdentifier) throw new ArgumentException(string.Empty, "serverFactoryIdentifier"); } - var nameParts = Utilities.SplitTypeName(serverFactoryIdentifier); + var nameParts = HostingUtilities.SplitTypeName(serverFactoryIdentifier); string typeName = nameParts.Item1; string assemblyName = nameParts.Item2; diff --git a/src/Microsoft.AspNet.Hosting/project.json b/src/Microsoft.AspNet.Hosting/project.json index 34fc3ef4..de0e1d53 100644 --- a/src/Microsoft.AspNet.Hosting/project.json +++ b/src/Microsoft.AspNet.Hosting/project.json @@ -6,6 +6,7 @@ "Microsoft.AspNet.PipelineCore": "1.0.0-*", "Microsoft.AspNet.Security.DataProtection": "1.0.0-*", "Microsoft.Framework.ConfigurationModel": "1.0.0-*", + "Microsoft.Framework.ConfigurationModel.Json": "1.0.0-*", "Microsoft.Framework.DependencyInjection": "1.0.0-*", "Microsoft.Framework.Logging": "1.0.0-*", "Microsoft.Framework.Runtime.Interfaces": "1.0.0-*" diff --git a/src/Microsoft.AspNet.TestHost/TestServer.cs b/src/Microsoft.AspNet.TestHost/TestServer.cs index e1e3ac37..dee516dd 100644 --- a/src/Microsoft.AspNet.TestHost/TestServer.cs +++ b/src/Microsoft.AspNet.TestHost/TestServer.cs @@ -17,7 +17,8 @@ namespace Microsoft.AspNet.TestHost { 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 Func _appDelegate; private IDisposable _appInstance; @@ -25,15 +26,11 @@ public class TestServer : IServerFactory, IDisposable public TestServer(IConfiguration config, IServiceProvider serviceProvider, Action appStartup) { - var env = serviceProvider.GetService(); - if (env == null) - { - throw new ArgumentException("IApplicationEnvironment couldn't be resolved.", "serviceProvider"); - } + var appEnv = serviceProvider.GetService(); HostingContext hostContext = new HostingContext() { - ApplicationName = env.ApplicationName, + ApplicationName = appEnv.ApplicationName, Configuration = config, ServerFactory = this, Services = serviceProvider, @@ -51,14 +48,22 @@ public static TestServer Create(Action app) public static TestServer Create(IServiceProvider provider, Action app) { + var appEnv = provider.GetService(); + + var hostingEnv = new HostingEnvironment() + { + EnvironmentName = DefaultEnvironmentName, + WebRoot = HostingUtilities.GetWebRoot(appEnv.ApplicationBasePath), + }; + var collection = new ServiceCollection(); - var hostingServices = HostingServices.GetDefaultServices(); + collection.Add(HostingServices.GetDefaultServices()); + collection.AddInstance(hostingEnv); - var config = new Configuration(); - collection.Add(hostingServices); + var appServices = collection.BuildServiceProvider(provider); - var serviceProvider = collection.BuildServiceProvider(provider); - return new TestServer(config, serviceProvider, app); + var config = new Configuration(); + return new TestServer(config, appServices, app); } public HttpMessageHandler CreateHandler() diff --git a/test/Microsoft.AspNet.Hosting.Tests/HostingUtilitiesTests.cs b/test/Microsoft.AspNet.Hosting.Tests/HostingUtilitiesTests.cs new file mode 100644 index 00000000..08daee6c --- /dev/null +++ b/test/Microsoft.AspNet.Hosting.Tests/HostingUtilitiesTests.cs @@ -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")); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Hosting.Tests/project.json b/test/Microsoft.AspNet.Hosting.Tests/project.json index 7ec2873f..2233a0fa 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/project.json +++ b/test/Microsoft.AspNet.Hosting.Tests/project.json @@ -13,5 +13,6 @@ }, "commands": { "test": "Xunit.KRunner" - } + }, + "webroot" : "testroot" } diff --git a/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs b/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs index 2a5ee973..025d3a7a 100644 --- a/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs +++ b/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs @@ -2,10 +2,12 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.IO; using System.Net; using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection.Fallback; @@ -54,6 +56,16 @@ public async Task CreateInvokesApp() Assert.Equal("CreateInvokesApp", result); } + [Fact] + public void WebRootCanBeResolvedFromProjectJson() + { + TestServer server = TestServer.Create(app => + { + var env = app.ApplicationServices.GetService(); + Assert.Equal(Path.GetFullPath("testroot"), env.WebRoot); + }); + } + [Fact] public async Task DisposeStreamIgnored() { diff --git a/test/Microsoft.AspNet.TestHost.Tests/project.json b/test/Microsoft.AspNet.TestHost.Tests/project.json index 61635fd5..d071d148 100644 --- a/test/Microsoft.AspNet.TestHost.Tests/project.json +++ b/test/Microsoft.AspNet.TestHost.Tests/project.json @@ -14,5 +14,6 @@ "System.Runtime": "" } } - } + }, + "webroot" : "testroot" }