Re-merge IWebRoot & IHostingEnv.

This commit is contained in:
Chris Ross 2014-12-04 17:23:21 -08:00
parent b883968eef
commit 03e4739a3f
7 changed files with 18 additions and 76 deletions

View File

@ -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<IConfigureHostingEnvironment> configures)
public HostingEnvironment(IApplicationEnvironment appEnvironment, IEnumerable<IConfigureHostingEnvironment> 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; }
}
}

View File

@ -35,8 +35,6 @@ namespace Microsoft.Framework.DependencyInjection
services.TryAdd(describer.Instance<IApplicationLifetime>(new ApplicationLifetime()));
services.TryAdd(describer.Singleton<IWebRootFileSystemProvider, WebRootFileSystemProvider>());
services.AddTypeActivator(configuration);
// TODO: Do we expect this to be provide by the runtime eventually?
services.AddLogging(configuration);

View File

@ -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; }
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}
}

View File

@ -57,9 +57,9 @@ namespace Microsoft.AspNet.Hosting
public void WebRootCanBeResolvedFromTheProjectJson()
{
var services = HostingServices.Create().BuildServiceProvider();
var provider = services.GetRequiredService<IWebRootFileSystemProvider>();
Assert.Equal(Path.GetFullPath("testroot") + Path.DirectorySeparatorChar, provider.WebRoot);
Assert.True(provider.GetFileSystem().GetFileInfo("TextFile.txt").Exists);
var env = services.GetRequiredService<IHostingEnvironment>();
Assert.Equal(Path.GetFullPath("testroot"), env.WebRoot);
Assert.True(env.WebRootFileSystem.GetFileInfo("TextFile.txt").Exists);
}
public void Initialize(IApplicationBuilder builder)

View File

@ -57,8 +57,8 @@ namespace Microsoft.AspNet.TestHost
{
TestServer server = TestServer.Create(app =>
{
var provider = app.ApplicationServices.GetRequiredService<IWebRootFileSystemProvider>();
Assert.Equal(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar, provider.WebRoot);
var env = app.ApplicationServices.GetRequiredService<IHostingEnvironment>();
Assert.Equal(Directory.GetCurrentDirectory(), env.WebRoot);
});
}