Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/DefaultBuilder/samples/SampleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -15,13 +14,14 @@ namespace SampleApp
{
public class Program
{
public static async Task Main(string[] args)
public static void Main(string[] args)
{
await using var webApp = WebApplication.Create(args);
var app = WebApplication.Create(args);

webApp.MapGet("/", (Func<string>)(() => "Hello, World!"));
app.UseStaticFiles();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you manually tested this?

Copy link
Copy Markdown
Member Author

@halter73 halter73 May 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I can write a manual automated test. There aren't any e2e tests in DefaultBuilder that don't hit the network stack so I was a little lazy and just went with the unit tests and the sample.

app.MapGet("/", (Func<string>)(() => "Hello, World!"));

await webApp.RunAsync();
app.Run();
}

private static void HelloWorld()
Expand Down
2 changes: 1 addition & 1 deletion src/DefaultBuilder/src/ConfigureHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public IHostBuilder ConfigureHostConfiguration(Action<IConfigurationBuilder> con
configureDelegate(_hostConfiguration);

_environment.ApplyConfigurationSettings(_hostConfiguration.Build());
Configuration.ChangeBasePath(_environment.ContentRootPath);
Configuration.ChangeFileProvider(_environment.ContentRootFileProvider);

_operations += b => b.ConfigureHostConfiguration(configureDelegate);
return this;
Expand Down
6 changes: 6 additions & 0 deletions src/DefaultBuilder/src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have a better way to do this from the csproj now?

25 changes: 20 additions & 5 deletions src/DefaultBuilder/src/WebHostEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,29 @@ public WebHostEnvironment(Assembly? callingAssembly)
ResolveFileProviders(new Configuration());
}

// For testing
internal WebHostEnvironment()
{
ApplicationName = default!;
EnvironmentName = default!;
ContentRootPath = default!;
WebRootPath = default!;
ContentRootFileProvider = default!;
WebRootFileProvider = default!;
}

public void ApplyConfigurationSettings(IConfiguration configuration)
{
ReadConfigurationSettings(configuration);
ResolveFileProviders(configuration);
}

internal void ReadConfigurationSettings(IConfiguration configuration)
{
ApplicationName = configuration[WebHostDefaults.ApplicationKey] ?? ApplicationName;
ContentRootPath = configuration[WebHostDefaults.ContentRootKey] ?? ContentRootPath;
EnvironmentName = configuration[WebHostDefaults.EnvironmentKey] ?? EnvironmentName;
WebRootPath = configuration[WebHostDefaults.ContentRootKey] ?? WebRootPath;

ResolveFileProviders(configuration);
ContentRootPath = configuration[WebHostDefaults.ContentRootKey] ?? ContentRootPath;
WebRootPath = configuration[WebHostDefaults.WebRootKey] ?? WebRootPath;
}

public void ApplyEnvironmentSettings(IWebHostBuilder genericWebHostBuilder)
Expand All @@ -65,7 +80,7 @@ public void ResolveFileProviders(IConfiguration configuration)

if (Directory.Exists(WebRootPath))
{
WebRootFileProvider = new PhysicalFileProvider(Path.Combine(ContentRootPath, WebRootPath));
WebRootFileProvider = new PhysicalFileProvider(WebRootPath);
}

if (this.IsDevelopment())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright (c) .NET Foundation. 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.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Microsoft.AspNetCore.Tests
{
public class WebHostEnvironmentTests
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a test making sure static files works?

{
[Fact]
public void ApplyConfigurationSettingsUsesTheCorrectKeys()
{
var configBuilder = new ConfigurationBuilder();
configBuilder.AddInMemoryCollection(new[]
{
new KeyValuePair<string, string>(WebHostDefaults.ApplicationKey, WebHostDefaults.ApplicationKey),
new KeyValuePair<string, string>(WebHostDefaults.EnvironmentKey, WebHostDefaults.EnvironmentKey),
new KeyValuePair<string, string>(WebHostDefaults.ContentRootKey, WebHostDefaults.ContentRootKey),
new KeyValuePair<string, string>(WebHostDefaults.WebRootKey, WebHostDefaults.WebRootKey),
});

var env = new WebHostEnvironment();

// Basically call ApplyConfigurationSettings(config) but without creating PhysicalFileProviders.
env.ReadConfigurationSettings(configBuilder.Build());

Assert.Equal(WebHostDefaults.ApplicationKey, env.ApplicationName);
Assert.Equal(WebHostDefaults.EnvironmentKey, env.EnvironmentName);
Assert.Equal(WebHostDefaults.ContentRootKey, env.ContentRootPath);
Assert.Equal(WebHostDefaults.WebRootKey, env.WebRootPath);
}

[Fact]
public void ApplyEnvironmentSettingsUsesTheCorrectKeys()
{
var env = new WebHostEnvironment
{
ApplicationName = WebHostDefaults.ApplicationKey,
EnvironmentName = WebHostDefaults.EnvironmentKey,
ContentRootPath = WebHostDefaults.ContentRootKey,
WebRootPath = WebHostDefaults.WebRootKey,
};

var settings = new Dictionary<string, string>();

env.ApplyEnvironmentSettings(new TestWebHostBuilder(settings));

Assert.Equal(WebHostDefaults.ApplicationKey, settings[WebHostDefaults.ApplicationKey]);
Assert.Equal(WebHostDefaults.EnvironmentKey, settings[WebHostDefaults.EnvironmentKey]);
Assert.Equal(WebHostDefaults.ContentRootKey, settings[WebHostDefaults.ContentRootKey]);
Assert.Equal(WebHostDefaults.WebRootKey, settings[WebHostDefaults.WebRootKey]);
}

private class TestWebHostBuilder : IWebHostBuilder
{
private readonly Dictionary<string, string> _settings;

public TestWebHostBuilder(Dictionary<string, string> settingsDictionary)
{
_settings = settingsDictionary;
}

public IWebHost Build()
{
throw new NotImplementedException();
}

public IWebHostBuilder ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureDelegate)
{
throw new NotImplementedException();
}

public IWebHostBuilder ConfigureServices(Action<IServiceCollection> configureServices)
{
throw new NotImplementedException();
}

public IWebHostBuilder ConfigureServices(Action<WebHostBuilderContext, IServiceCollection> configureServices)
{
throw new NotImplementedException();
}

public string GetSetting(string key)
{
_settings.TryGetValue(key, out var value);
return value;
}

public IWebHostBuilder UseSetting(string key, string value)
{
_settings[key] = value;
return this;
}
}
}
}