diff --git a/src/Microsoft.AspNet.Hosting/HostingEnvironment.cs b/src/Microsoft.AspNet.Hosting/HostingEnvironment.cs index acafebc789..7644d5f38b 100644 --- a/src/Microsoft.AspNet.Hosting/HostingEnvironment.cs +++ b/src/Microsoft.AspNet.Hosting/HostingEnvironment.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; +using Microsoft.AspNet.FileSystems; using Microsoft.Framework.Runtime; namespace Microsoft.AspNet.Hosting @@ -10,9 +11,11 @@ namespace Microsoft.AspNet.Hosting { private const string DefaultEnvironmentName = "Development"; - public HostingEnvironment(IEnumerable configures) + public HostingEnvironment(IApplicationEnvironment appEnvironment, IEnumerable configures) { EnvironmentName = DefaultEnvironmentName; + WebRoot = HostingUtilities.GetWebRoot(appEnvironment.ApplicationBasePath); + WebRootFileSystem = new PhysicalFileSystem(WebRoot); foreach (var configure in configures) { configure.Configure(this); @@ -20,5 +23,9 @@ namespace Microsoft.AspNet.Hosting } public string EnvironmentName { get; set; } + + public string WebRoot { get; private set; } + + public IFileSystem WebRootFileSystem { get; private set; } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Hosting/HostingServicesCollectionExtensions.cs b/src/Microsoft.AspNet.Hosting/HostingServicesCollectionExtensions.cs index ee9fea15aa..ac661999b2 100644 --- a/src/Microsoft.AspNet.Hosting/HostingServicesCollectionExtensions.cs +++ b/src/Microsoft.AspNet.Hosting/HostingServicesCollectionExtensions.cs @@ -35,8 +35,6 @@ namespace Microsoft.Framework.DependencyInjection services.TryAdd(describer.Instance(new ApplicationLifetime())); - services.TryAdd(describer.Singleton()); - services.AddTypeActivator(configuration); // TODO: Do we expect this to be provide by the runtime eventually? services.AddLogging(configuration); diff --git a/src/Microsoft.AspNet.Hosting/IHostingEnvironment.cs b/src/Microsoft.AspNet.Hosting/IHostingEnvironment.cs index 380c72550e..6505f1cf57 100644 --- a/src/Microsoft.AspNet.Hosting/IHostingEnvironment.cs +++ b/src/Microsoft.AspNet.Hosting/IHostingEnvironment.cs @@ -1,6 +1,7 @@ // 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.AspNet.FileSystems; using Microsoft.Framework.Runtime; namespace Microsoft.AspNet.Hosting @@ -9,5 +10,9 @@ namespace Microsoft.AspNet.Hosting public interface IHostingEnvironment { string EnvironmentName { get; set; } + + string WebRoot { get; } + + IFileSystem WebRootFileSystem { get; } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Hosting/IWebRootFileSystemProvider.cs b/src/Microsoft.AspNet.Hosting/IWebRootFileSystemProvider.cs deleted file mode 100644 index 8f65cff6e4..0000000000 --- a/src/Microsoft.AspNet.Hosting/IWebRootFileSystemProvider.cs +++ /dev/null @@ -1,18 +0,0 @@ -// 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.AspNet.FileSystems; -using Microsoft.Framework.Runtime; - -namespace Microsoft.AspNet.Hosting -{ - [AssemblyNeutral] - public interface IWebRootFileSystemProvider - { - string WebRoot { get; } - - IFileSystem GetFileSystem(); - - string MapPath(string path); - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Hosting/WebRootFileSystemProvider.cs b/src/Microsoft.AspNet.Hosting/WebRootFileSystemProvider.cs deleted file mode 100644 index 79fefaab5e..0000000000 --- a/src/Microsoft.AspNet.Hosting/WebRootFileSystemProvider.cs +++ /dev/null @@ -1,50 +0,0 @@ -// 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 System.IO; -using Microsoft.AspNet.FileSystems; -using Microsoft.Framework.Runtime; - -namespace Microsoft.AspNet.Hosting -{ - public class WebRootFileSystemProvider : IWebRootFileSystemProvider - { - private readonly IFileSystem _fileSystem; - - public WebRootFileSystemProvider(IApplicationEnvironment appEnvironment) - { - var root = HostingUtilities.GetWebRoot(appEnvironment.ApplicationBasePath); - - if (!string.IsNullOrEmpty(root) && - root[root.Length - 1] != Path.DirectorySeparatorChar) - { - root += Path.DirectorySeparatorChar; - } - - WebRoot = root; - - _fileSystem = new PhysicalFileSystem(WebRoot); - } - - public string WebRoot { get; private set; } - - public IFileSystem GetFileSystem() - { - return _fileSystem; - } - - public string MapPath(string path) - { - var fullPath = Path.GetFullPath(Path.Combine(WebRoot, path)); - - // Don't allow MapPath to escape the base root directory - if (!fullPath.StartsWith(WebRoot, StringComparison.OrdinalIgnoreCase)) - { - throw new ArgumentException("Invalid path: " + path, nameof(path)); - } - - return fullPath; - } - } -} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs b/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs index 3677b8041d..e2e0ba6852 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs @@ -57,9 +57,9 @@ namespace Microsoft.AspNet.Hosting public void WebRootCanBeResolvedFromTheProjectJson() { var services = HostingServices.Create().BuildServiceProvider(); - var provider = services.GetRequiredService(); - Assert.Equal(Path.GetFullPath("testroot") + Path.DirectorySeparatorChar, provider.WebRoot); - Assert.True(provider.GetFileSystem().GetFileInfo("TextFile.txt").Exists); + var env = services.GetRequiredService(); + Assert.Equal(Path.GetFullPath("testroot"), env.WebRoot); + Assert.True(env.WebRootFileSystem.GetFileInfo("TextFile.txt").Exists); } public void Initialize(IApplicationBuilder builder) diff --git a/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs b/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs index c64c0af3fc..9a1a2c7b19 100644 --- a/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs +++ b/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs @@ -57,8 +57,8 @@ namespace Microsoft.AspNet.TestHost { TestServer server = TestServer.Create(app => { - var provider = app.ApplicationServices.GetRequiredService(); - Assert.Equal(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar, provider.WebRoot); + var env = app.ApplicationServices.GetRequiredService(); + Assert.Equal(Directory.GetCurrentDirectory(), env.WebRoot); }); }