Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit 5ac5893

Browse files
committed
Additional fiels in HostingEnvironment and Renames
- Added ApplicationName, ContentRootPath and ContentRootFileProvider - Removed Configuration - Removed MapPath
1 parent 9ade9da commit 5ac5893

File tree

13 files changed

+109
-209
lines changed

13 files changed

+109
-209
lines changed

samples/SampleStartups/StartupFullControl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static void Main(string[] args)
1515
{
1616
var host = new WebHostBuilder()
1717
.UseServer("Microsoft.AspNetCore.Server.Kestrel") // Set the server manually
18-
.UseApplicationBasePath(Directory.GetCurrentDirectory()) // Override the application base with the current directory
18+
.UseContentRoot(Directory.GetCurrentDirectory()) // Override the content root with the current directory
1919
.UseUrls("http://*:1000", "https://*:902")
2020
.UseEnvironment("Development")
2121
.UseWebRoot("public")

src/Microsoft.AspNetCore.Hosting.Abstractions/HostingEnvironmentExtensions.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,33 +76,5 @@ public static bool IsEnvironment(
7676
environmentName,
7777
StringComparison.OrdinalIgnoreCase);
7878
}
79-
80-
/// <summary>
81-
/// Determines the physical path corresponding to the given virtual path.
82-
/// </summary>
83-
/// <param name="hostingEnvironment">An instance of <see cref="IHostingEnvironment"/>.</param>
84-
/// <param name="virtualPath">Path relative to the application root.</param>
85-
/// <returns>Physical path corresponding to the virtual path.</returns>
86-
public static string MapPath(
87-
this IHostingEnvironment hostingEnvironment,
88-
string virtualPath)
89-
{
90-
if (hostingEnvironment == null)
91-
{
92-
throw new ArgumentNullException(nameof(hostingEnvironment));
93-
}
94-
if (string.IsNullOrEmpty(hostingEnvironment.WebRootPath))
95-
{
96-
throw new InvalidOperationException("Cannot map path because webroot path is not set");
97-
}
98-
if (virtualPath == null)
99-
{
100-
return hostingEnvironment.WebRootPath;
101-
}
102-
103-
// On windows replace / with \.
104-
virtualPath = virtualPath.Replace('/', Path.DirectorySeparatorChar);
105-
return Path.Combine(hostingEnvironment.WebRootPath, virtualPath);
106-
}
10779
}
10880
}

src/Microsoft.AspNetCore.Hosting.Abstractions/IHostingEnvironment.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using Microsoft.Extensions.Configuration;
54
using Microsoft.Extensions.FileProviders;
65

76
namespace Microsoft.AspNetCore.Hosting
@@ -17,6 +16,13 @@ public interface IHostingEnvironment
1716
/// </summary>
1817
// This must be settable!
1918
string EnvironmentName { get; set; }
19+
20+
/// <summary>
21+
/// Gets or sets the name of the application. This property is automatically set by the host to the assembly containing
22+
/// the application entry point.
23+
/// </summary>
24+
// This must be settable!
25+
string ApplicationName { get; set; }
2026

2127
/// <summary>
2228
/// Gets or sets the absolute path to the directory that contains the web-servable application content files.
@@ -31,8 +37,15 @@ public interface IHostingEnvironment
3137
IFileProvider WebRootFileProvider { get; set; }
3238

3339
/// <summary>
34-
/// Gets or sets the configuration object used by hosting environment.
40+
/// Gets or sets the absolute path to the directory that contains the application content files.
41+
/// </summary>
42+
// This must be settable!
43+
string ContentRootPath { get; set; }
44+
45+
/// <summary>
46+
/// Gets or sets an <see cref="IFileProvider"/> pointing at <see cref="ContentRootPath"/>.
3547
/// </summary>
36-
IConfiguration Configuration { get; set; }
48+
// This must be settable!
49+
IFileProvider ContentRootFileProvider { get; set; }
3750
}
3851
}

src/Microsoft.AspNetCore.Hosting.Abstractions/WebHostDefaults.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class WebHostDefaults
1212
public static readonly string WebRootKey = "webroot";
1313
public static readonly string CaptureStartupErrorsKey = "captureStartupErrors";
1414
public static readonly string ServerUrlsKey = "server.urls";
15-
public static readonly string ApplicationBaseKey = "applicationBase";
15+
public static readonly string ContentRootKey = "contentRoot";
1616

1717
public static readonly string HostingJsonFile = "hosting.json";
1818
public static readonly string EnvironmentVariablesPrefix = "ASPNETCORE_";
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using Microsoft.Extensions.Configuration;
54
using Microsoft.Extensions.FileProviders;
65

76
namespace Microsoft.AspNetCore.Hosting.Internal
87
{
98
public class HostingEnvironment : IHostingEnvironment
109
{
11-
public string EnvironmentName { get; set; } = Microsoft.AspNetCore.Hosting.EnvironmentName.Production;
10+
public string EnvironmentName { get; set; } = Hosting.EnvironmentName.Production;
11+
12+
public string ApplicationName { get; set; }
1213

1314
public string WebRootPath { get; set; }
1415

1516
public IFileProvider WebRootFileProvider { get; set; }
1617

17-
public IConfiguration Configuration { get; set; }
18+
public string ContentRootPath { get; set; }
19+
20+
public IFileProvider ContentRootFileProvider { get; set; }
1821
}
1922
}

src/Microsoft.AspNetCore.Hosting/Internal/HostingEnvironmentExtensions.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,50 @@
33

44
using System;
55
using System.IO;
6-
using Microsoft.AspNetCore.Hosting.Internal;
7-
using Microsoft.Extensions.Configuration;
86
using Microsoft.Extensions.FileProviders;
97

108
namespace Microsoft.AspNetCore.Hosting.Internal
119
{
1210
public static class HostingEnvironmentExtensions
1311
{
14-
public static void Initialize(this IHostingEnvironment hostingEnvironment, string applicationBasePath, WebHostOptions options, IConfiguration configuration)
12+
public static void Initialize(this IHostingEnvironment hostingEnvironment, string applicationName, string contentRootPath, WebHostOptions options)
1513
{
1614
if (options == null)
1715
{
1816
throw new ArgumentNullException(nameof(options));
1917
}
18+
if (string.IsNullOrEmpty(applicationName))
19+
{
20+
throw new ArgumentException("A valid non-empty application name must be provided.", nameof(applicationName));
21+
}
22+
if (string.IsNullOrEmpty(contentRootPath))
23+
{
24+
throw new ArgumentException("A valid non-empty content root must be provided.", nameof(contentRootPath));
25+
}
26+
if (!Directory.Exists(contentRootPath))
27+
{
28+
throw new ArgumentException("The provided content root does not exist.", nameof(contentRootPath));
29+
}
30+
31+
hostingEnvironment.ApplicationName = applicationName;
32+
hostingEnvironment.ContentRootPath = contentRootPath;
33+
hostingEnvironment.ContentRootFileProvider = new PhysicalFileProvider(hostingEnvironment.ContentRootPath);
2034

2135
var webRoot = options.WebRoot;
2236
if (webRoot == null)
2337
{
2438
// Default to /wwwroot if it exists.
25-
var wwwroot = Path.Combine(applicationBasePath, "wwwroot");
39+
var wwwroot = Path.Combine(hostingEnvironment.ContentRootPath, "wwwroot");
2640
if (Directory.Exists(wwwroot))
2741
{
2842
hostingEnvironment.WebRootPath = wwwroot;
2943
}
3044
}
3145
else
3246
{
33-
hostingEnvironment.WebRootPath = Path.Combine(applicationBasePath, webRoot);
47+
hostingEnvironment.WebRootPath = Path.Combine(hostingEnvironment.ContentRootPath, webRoot);
3448
}
49+
3550
if (!string.IsNullOrEmpty(hostingEnvironment.WebRootPath))
3651
{
3752
hostingEnvironment.WebRootPath = Path.GetFullPath(hostingEnvironment.WebRootPath);
@@ -45,10 +60,9 @@ public static void Initialize(this IHostingEnvironment hostingEnvironment, strin
4560
{
4661
hostingEnvironment.WebRootFileProvider = new NullFileProvider();
4762
}
63+
4864
var environmentName = options.Environment;
4965
hostingEnvironment.EnvironmentName = environmentName ?? hostingEnvironment.EnvironmentName;
50-
51-
hostingEnvironment.Configuration = configuration;
5266
}
5367
}
5468
}

src/Microsoft.AspNetCore.Hosting/Internal/WebHostOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public WebHostOptions(IConfiguration configuration)
2525
Environment = configuration[WebHostDefaults.EnvironmentKey];
2626
ServerFactoryLocation = configuration[WebHostDefaults.ServerKey];
2727
WebRoot = configuration[WebHostDefaults.WebRootKey];
28-
ApplicationBasePath = configuration[WebHostDefaults.ApplicationBaseKey];
28+
ContentRootPath = configuration[WebHostDefaults.ContentRootKey];
2929
}
3030

3131
public string Application { get; set; }
@@ -40,7 +40,7 @@ public WebHostOptions(IConfiguration configuration)
4040

4141
public string WebRoot { get; set; }
4242

43-
public string ApplicationBasePath { get; set; }
43+
public string ContentRootPath { get; set; }
4444

4545
private static bool ParseBool(IConfiguration configuration, string key)
4646
{

src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Diagnostics;
77
using System.IO;
88
using System.Reflection;
9-
using System.Runtime.Versioning;
109
using Microsoft.AspNetCore.Builder;
1110
using Microsoft.AspNetCore.Hosting.Builder;
1211
using Microsoft.AspNetCore.Hosting.Internal;
@@ -152,14 +151,16 @@ public IWebHostBuilder ConfigureLogging(Action<ILoggerFactory> configureLogging)
152151
public IWebHost Build()
153152
{
154153
var hostingServices = BuildHostingServices();
155-
156154
var hostingContainer = hostingServices.BuildServiceProvider();
157155

158156
var appEnvironment = hostingContainer.GetRequiredService<IApplicationEnvironment>();
159157
var startupLoader = hostingContainer.GetRequiredService<IStartupLoader>();
158+
159+
var contentRootPath = ResolveContentRootPath(_options.ContentRootPath, appEnvironment.ApplicationBasePath);
160+
var applicationName = ResolveApplicationName() ?? appEnvironment.ApplicationName;
160161

161162
// Initialize the hosting environment
162-
_hostingEnvironment.Initialize(appEnvironment.ApplicationBasePath, _options, _config);
163+
_hostingEnvironment.Initialize(applicationName, contentRootPath, _options);
163164

164165
var host = new WebHost(hostingServices, startupLoader, _options, _config);
165166

@@ -201,23 +202,13 @@ private IServiceCollection BuildHostingServices()
201202
services.AddTransient<IStartupFilter, AutoRequestServicesStartupFilter>();
202203

203204
var defaultPlatformServices = PlatformServices.Default;
204-
205-
if (defaultPlatformServices != null)
205+
if (defaultPlatformServices.Application != null)
206+
{
207+
services.TryAddSingleton(defaultPlatformServices.Application);
208+
}
209+
if (defaultPlatformServices.Runtime != null)
206210
{
207-
if (defaultPlatformServices.Application != null)
208-
{
209-
var appEnv = defaultPlatformServices.Application;
210-
var applicationBasePath = ResolveApplicationBasePath(_options.ApplicationBasePath, appEnv.ApplicationBasePath);
211-
var startupAssemblyName = ResolveStartupAssemblyName() ?? appEnv.ApplicationName;
212-
appEnv = new WrappedApplicationEnvironment(applicationBasePath, startupAssemblyName, defaultPlatformServices.Application);
213-
214-
services.TryAddSingleton(appEnv);
215-
}
216-
217-
if (defaultPlatformServices.Runtime != null)
218-
{
219-
services.TryAddSingleton(defaultPlatformServices.Runtime);
220-
}
211+
services.TryAddSingleton(defaultPlatformServices.Runtime);
221212
}
222213

223214
foreach (var configureServices in _configureServicesDelegates)
@@ -228,20 +219,20 @@ private IServiceCollection BuildHostingServices()
228219
return services;
229220
}
230221

231-
private string ResolveApplicationBasePath(string applicationBasePath, string basePath)
222+
private string ResolveContentRootPath(string contentRootPath, string basePath)
232223
{
233-
if (string.IsNullOrEmpty(applicationBasePath))
224+
if (string.IsNullOrEmpty(contentRootPath))
234225
{
235226
return basePath;
236227
}
237-
if (Path.IsPathRooted(applicationBasePath))
228+
if (Path.IsPathRooted(contentRootPath))
238229
{
239-
return applicationBasePath;
230+
return contentRootPath;
240231
}
241-
return Path.Combine(Path.GetFullPath(basePath), applicationBasePath);
232+
return Path.Combine(Path.GetFullPath(basePath), contentRootPath);
242233
}
243234

244-
private string ResolveStartupAssemblyName()
235+
private string ResolveApplicationName()
245236
{
246237
if (_startup != null)
247238
{
@@ -257,24 +248,5 @@ private string ResolveStartupAssemblyName()
257248
}
258249
return null;
259250
}
260-
261-
private class WrappedApplicationEnvironment : IApplicationEnvironment
262-
{
263-
public WrappedApplicationEnvironment(string applicationBasePath, string applicationName, IApplicationEnvironment env)
264-
{
265-
ApplicationBasePath = applicationBasePath;
266-
ApplicationName = applicationName;
267-
ApplicationVersion = env.ApplicationVersion;
268-
RuntimeFramework = env.RuntimeFramework;
269-
}
270-
271-
public string ApplicationBasePath { get; }
272-
273-
public string ApplicationName { get; }
274-
275-
public string ApplicationVersion { get; }
276-
277-
public FrameworkName RuntimeFramework { get; }
278-
}
279251
}
280252
}

src/Microsoft.AspNetCore.Hosting/WebHostBuilderExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ public static IWebHostBuilder UseServer(this IWebHostBuilder hostBuilder, IServe
6262
return hostBuilder.UseServer(new ServerFactory(server));
6363
}
6464

65-
public static IWebHostBuilder UseApplicationBasePath(this IWebHostBuilder hostBuilder, string applicationBasePath)
65+
public static IWebHostBuilder UseContentRoot(this IWebHostBuilder hostBuilder, string contentRootPath)
6666
{
67-
if (applicationBasePath == null)
67+
if (contentRootPath == null)
6868
{
69-
throw new ArgumentNullException(nameof(applicationBasePath));
69+
throw new ArgumentNullException(nameof(contentRootPath));
7070
}
7171

72-
return hostBuilder.UseSetting(WebHostDefaults.ApplicationBaseKey, applicationBasePath);
72+
return hostBuilder.UseSetting(WebHostDefaults.ContentRootKey, contentRootPath);
7373
}
7474

7575
public static IWebHostBuilder UseEnvironment(this IWebHostBuilder hostBuilder, string environment)

src/Microsoft.AspNetCore.Hosting/WebHostExtensions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Threading;
66
using Microsoft.AspNetCore.Server.Features;
77
using Microsoft.Extensions.DependencyInjection;
8-
using Microsoft.Extensions.PlatformAbstractions;
98

109
namespace Microsoft.AspNetCore.Hosting
1110
{
@@ -49,10 +48,9 @@ private static void Run(this IWebHost host, CancellationToken token, string shut
4948

5049
var hostingEnvironment = host.Services.GetService<IHostingEnvironment>();
5150
var applicationLifetime = host.Services.GetService<IApplicationLifetime>();
52-
var applicationEnvironment = host.Services.GetService<IApplicationEnvironment>();
5351

5452
Console.WriteLine($"Hosting environment: {hostingEnvironment.EnvironmentName}");
55-
Console.WriteLine($"Application base path: {applicationEnvironment.ApplicationBasePath}");
53+
Console.WriteLine($"Content root path: {hostingEnvironment.ContentRootPath}");
5654

5755
var serverAddresses = host.ServerFeatures.Get<IServerAddressesFeature>()?.Addresses;
5856
if (serverAddresses != null)

0 commit comments

Comments
 (0)