Add fallback when resolving application name

- Use the name of the EntryAssembly when application name is not specified
This commit is contained in:
John Luo 2017-08-18 21:19:23 -07:00
parent 964b671288
commit b187c1adc4
21 changed files with 236 additions and 94 deletions

View File

@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26524.0
VisualStudioVersion = 15.0.26730.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E0497F39-AFFB-4819-A116-E39E361915AB}"
EndProject
@ -26,16 +26,20 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleStartups", "samples\S
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Hosting.FunctionalTests", "test\Microsoft.AspNetCore.Hosting.FunctionalTests\Microsoft.AspNetCore.Hosting.FunctionalTests.csproj", "{FC578F4E-171C-4F82-B301-3ABF6318D082}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Hosting.TestSites", "test\Microsoft.AspNetCore.Hosting.TestSites\Microsoft.AspNetCore.Hosting.TestSites.csproj", "{542D4600-B232-4B17-A08C-E31EBFA0D74E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestStartupAssembly1", "test\TestStartupAssembly1\TestStartupAssembly1.csproj", "{39D3B138-37DB-4D03-A5A0-3F2B02EFC671}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Hosting.Abstractions", "src\Microsoft.Extensions.Hosting.Abstractions\Microsoft.Extensions.Hosting.Abstractions.csproj", "{96BC7EEA-64D9-4DA5-8E87-1C18CBFE7D12}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Hosting.WindowsServices", "src\Microsoft.AspNetCore.Hosting.WindowsServices\Microsoft.AspNetCore.Hosting.WindowsServices.csproj", "{9C93A93B-270A-4785-8F41-46C38DC33825}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Certificates.Configuration.Tests", "test\Microsoft.AspNetCore.Certificates.Configuration.Tests\Microsoft.AspNetCore.Certificates.Configuration.Tests.csproj", "{AB0B7394-278D-4838-A59C-276ED88D00CC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TestAssets", "TestAssets", "{FA7D2012-C1B4-4AF7-9ADD-381B2004EA16}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Hosting.TestSites", "test\TestAssets\Microsoft.AspNetCore.Hosting.TestSites\Microsoft.AspNetCore.Hosting.TestSites.csproj", "{EDFF02F0-A8A4-4EB1-A179-94D7500FB266}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestStartupAssembly1", "test\TestAssets\TestStartupAssembly1\TestStartupAssembly1.csproj", "{58194285-5891-464A-A96B-0FE043029E8A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IStartupInjectionAssemblyName", "test\TestAssets\IStartupInjectionAssemblyName\IStartupInjectionAssemblyName.csproj", "{F894D8C5-B760-4734-AD31-3CA6FC557CCF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -146,30 +150,6 @@ Global
{FC578F4E-171C-4F82-B301-3ABF6318D082}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{FC578F4E-171C-4F82-B301-3ABF6318D082}.Release|x86.ActiveCfg = Release|Any CPU
{FC578F4E-171C-4F82-B301-3ABF6318D082}.Release|x86.Build.0 = Release|Any CPU
{542D4600-B232-4B17-A08C-E31EBFA0D74E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{542D4600-B232-4B17-A08C-E31EBFA0D74E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{542D4600-B232-4B17-A08C-E31EBFA0D74E}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{542D4600-B232-4B17-A08C-E31EBFA0D74E}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{542D4600-B232-4B17-A08C-E31EBFA0D74E}.Debug|x86.ActiveCfg = Debug|Any CPU
{542D4600-B232-4B17-A08C-E31EBFA0D74E}.Debug|x86.Build.0 = Debug|Any CPU
{542D4600-B232-4B17-A08C-E31EBFA0D74E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{542D4600-B232-4B17-A08C-E31EBFA0D74E}.Release|Any CPU.Build.0 = Release|Any CPU
{542D4600-B232-4B17-A08C-E31EBFA0D74E}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{542D4600-B232-4B17-A08C-E31EBFA0D74E}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{542D4600-B232-4B17-A08C-E31EBFA0D74E}.Release|x86.ActiveCfg = Release|Any CPU
{542D4600-B232-4B17-A08C-E31EBFA0D74E}.Release|x86.Build.0 = Release|Any CPU
{39D3B138-37DB-4D03-A5A0-3F2B02EFC671}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{39D3B138-37DB-4D03-A5A0-3F2B02EFC671}.Debug|Any CPU.Build.0 = Debug|Any CPU
{39D3B138-37DB-4D03-A5A0-3F2B02EFC671}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{39D3B138-37DB-4D03-A5A0-3F2B02EFC671}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{39D3B138-37DB-4D03-A5A0-3F2B02EFC671}.Debug|x86.ActiveCfg = Debug|Any CPU
{39D3B138-37DB-4D03-A5A0-3F2B02EFC671}.Debug|x86.Build.0 = Debug|Any CPU
{39D3B138-37DB-4D03-A5A0-3F2B02EFC671}.Release|Any CPU.ActiveCfg = Release|Any CPU
{39D3B138-37DB-4D03-A5A0-3F2B02EFC671}.Release|Any CPU.Build.0 = Release|Any CPU
{39D3B138-37DB-4D03-A5A0-3F2B02EFC671}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{39D3B138-37DB-4D03-A5A0-3F2B02EFC671}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{39D3B138-37DB-4D03-A5A0-3F2B02EFC671}.Release|x86.ActiveCfg = Release|Any CPU
{39D3B138-37DB-4D03-A5A0-3F2B02EFC671}.Release|x86.Build.0 = Release|Any CPU
{96BC7EEA-64D9-4DA5-8E87-1C18CBFE7D12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{96BC7EEA-64D9-4DA5-8E87-1C18CBFE7D12}.Debug|Any CPU.Build.0 = Debug|Any CPU
{96BC7EEA-64D9-4DA5-8E87-1C18CBFE7D12}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
@ -206,6 +186,42 @@ Global
{AB0B7394-278D-4838-A59C-276ED88D00CC}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{AB0B7394-278D-4838-A59C-276ED88D00CC}.Release|x86.ActiveCfg = Release|Any CPU
{AB0B7394-278D-4838-A59C-276ED88D00CC}.Release|x86.Build.0 = Release|Any CPU
{EDFF02F0-A8A4-4EB1-A179-94D7500FB266}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EDFF02F0-A8A4-4EB1-A179-94D7500FB266}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EDFF02F0-A8A4-4EB1-A179-94D7500FB266}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{EDFF02F0-A8A4-4EB1-A179-94D7500FB266}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{EDFF02F0-A8A4-4EB1-A179-94D7500FB266}.Debug|x86.ActiveCfg = Debug|Any CPU
{EDFF02F0-A8A4-4EB1-A179-94D7500FB266}.Debug|x86.Build.0 = Debug|Any CPU
{EDFF02F0-A8A4-4EB1-A179-94D7500FB266}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EDFF02F0-A8A4-4EB1-A179-94D7500FB266}.Release|Any CPU.Build.0 = Release|Any CPU
{EDFF02F0-A8A4-4EB1-A179-94D7500FB266}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{EDFF02F0-A8A4-4EB1-A179-94D7500FB266}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{EDFF02F0-A8A4-4EB1-A179-94D7500FB266}.Release|x86.ActiveCfg = Release|Any CPU
{EDFF02F0-A8A4-4EB1-A179-94D7500FB266}.Release|x86.Build.0 = Release|Any CPU
{58194285-5891-464A-A96B-0FE043029E8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{58194285-5891-464A-A96B-0FE043029E8A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58194285-5891-464A-A96B-0FE043029E8A}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{58194285-5891-464A-A96B-0FE043029E8A}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{58194285-5891-464A-A96B-0FE043029E8A}.Debug|x86.ActiveCfg = Debug|Any CPU
{58194285-5891-464A-A96B-0FE043029E8A}.Debug|x86.Build.0 = Debug|Any CPU
{58194285-5891-464A-A96B-0FE043029E8A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{58194285-5891-464A-A96B-0FE043029E8A}.Release|Any CPU.Build.0 = Release|Any CPU
{58194285-5891-464A-A96B-0FE043029E8A}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{58194285-5891-464A-A96B-0FE043029E8A}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{58194285-5891-464A-A96B-0FE043029E8A}.Release|x86.ActiveCfg = Release|Any CPU
{58194285-5891-464A-A96B-0FE043029E8A}.Release|x86.Build.0 = Release|Any CPU
{F894D8C5-B760-4734-AD31-3CA6FC557CCF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F894D8C5-B760-4734-AD31-3CA6FC557CCF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F894D8C5-B760-4734-AD31-3CA6FC557CCF}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{F894D8C5-B760-4734-AD31-3CA6FC557CCF}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{F894D8C5-B760-4734-AD31-3CA6FC557CCF}.Debug|x86.ActiveCfg = Debug|Any CPU
{F894D8C5-B760-4734-AD31-3CA6FC557CCF}.Debug|x86.Build.0 = Debug|Any CPU
{F894D8C5-B760-4734-AD31-3CA6FC557CCF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F894D8C5-B760-4734-AD31-3CA6FC557CCF}.Release|Any CPU.Build.0 = Release|Any CPU
{F894D8C5-B760-4734-AD31-3CA6FC557CCF}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{F894D8C5-B760-4734-AD31-3CA6FC557CCF}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{F894D8C5-B760-4734-AD31-3CA6FC557CCF}.Release|x86.ActiveCfg = Release|Any CPU
{F894D8C5-B760-4734-AD31-3CA6FC557CCF}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -220,10 +236,15 @@ Global
{3DA89347-6731-4366-80C4-548F24E8607B} = {E0497F39-AFFB-4819-A116-E39E361915AB}
{485B6745-7648-400A-A969-F68FCF194E46} = {9C7520A0-F2EB-411C-8BB2-80B39C937217}
{FC578F4E-171C-4F82-B301-3ABF6318D082} = {FEB39027-9158-4DE2-997F-7ADAEF8188D0}
{542D4600-B232-4B17-A08C-E31EBFA0D74E} = {FEB39027-9158-4DE2-997F-7ADAEF8188D0}
{39D3B138-37DB-4D03-A5A0-3F2B02EFC671} = {FEB39027-9158-4DE2-997F-7ADAEF8188D0}
{96BC7EEA-64D9-4DA5-8E87-1C18CBFE7D12} = {E0497F39-AFFB-4819-A116-E39E361915AB}
{9C93A93B-270A-4785-8F41-46C38DC33825} = {E0497F39-AFFB-4819-A116-E39E361915AB}
{AB0B7394-278D-4838-A59C-276ED88D00CC} = {FEB39027-9158-4DE2-997F-7ADAEF8188D0}
{FA7D2012-C1B4-4AF7-9ADD-381B2004EA16} = {FEB39027-9158-4DE2-997F-7ADAEF8188D0}
{EDFF02F0-A8A4-4EB1-A179-94D7500FB266} = {FA7D2012-C1B4-4AF7-9ADD-381B2004EA16}
{58194285-5891-464A-A96B-0FE043029E8A} = {FA7D2012-C1B4-4AF7-9ADD-381B2004EA16}
{F894D8C5-B760-4734-AD31-3CA6FC557CCF} = {FA7D2012-C1B4-4AF7-9ADD-381B2004EA16}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AABD536D-E05F-409B-A716-535E0C478076}
EndGlobalSection
EndGlobal

View File

@ -9,16 +9,12 @@ namespace Microsoft.AspNetCore.Hosting.Internal
{
public static class HostingEnvironmentExtensions
{
public static void Initialize(this IHostingEnvironment hostingEnvironment, string applicationName, string contentRootPath, WebHostOptions options)
public static void Initialize(this IHostingEnvironment hostingEnvironment, string contentRootPath, WebHostOptions options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (string.IsNullOrEmpty(applicationName))
{
throw new ArgumentException("A valid non-empty application name must be provided.", nameof(applicationName));
}
if (string.IsNullOrEmpty(contentRootPath))
{
throw new ArgumentException("A valid non-empty content root must be provided.", nameof(contentRootPath));
@ -28,7 +24,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
throw new ArgumentException($"The content root '{contentRootPath}' does not exist.", nameof(contentRootPath));
}
hostingEnvironment.ApplicationName = applicationName;
hostingEnvironment.ApplicationName = options.ApplicationName;
hostingEnvironment.ContentRootPath = contentRootPath;
hostingEnvironment.ContentRootFileProvider = new PhysicalFileProvider(hostingEnvironment.ContentRootPath);

View File

@ -166,7 +166,12 @@ namespace Microsoft.AspNetCore.Hosting.Internal
return;
}
_startup = _hostingServiceProvider.GetRequiredService<IStartup>();
_startup = _hostingServiceProvider.GetService<IStartup>();
if (_startup == null)
{
throw new InvalidOperationException($"No startup configured. Please specify startup via WebHostBuilder.UseStartup, WebHostBuilder.Configure, injecting {nameof(IStartup)} or specifying the startup assembly via {nameof(WebHostDefaults.StartupAssemblyKey)} in the web host configuration.");
}
}
private RequestDelegate BuildApplication()

View File

@ -13,13 +13,16 @@ namespace Microsoft.AspNetCore.Hosting.Internal
public WebHostOptions() { }
public WebHostOptions(IConfiguration configuration)
: this(configuration, string.Empty) { }
public WebHostOptions(IConfiguration configuration, string applicationNameFallback)
{
if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}
ApplicationName = configuration[WebHostDefaults.ApplicationKey];
ApplicationName = configuration[WebHostDefaults.ApplicationKey] ?? applicationNameFallback;
StartupAssembly = configuration[WebHostDefaults.StartupAssemblyKey];
DetailedErrors = WebHostUtilities.ParseBool(configuration, WebHostDefaults.DetailedErrorsKey);
CaptureStartupErrors = WebHostUtilities.ParseBool(configuration, WebHostDefaults.CaptureStartupErrorsKey);

View File

@ -188,7 +188,7 @@ namespace Microsoft.AspNetCore.Hosting
{
hostingStartupErrors = null;
_options = new WebHostOptions(_config);
_options = new WebHostOptions(_config, Assembly.GetEntryAssembly()?.GetName().Name);
if (!_options.PreventHostingStartup)
{
@ -221,10 +221,9 @@ namespace Microsoft.AspNetCore.Hosting
}
var contentRootPath = ResolveContentRootPath(_options.ContentRootPath, AppContext.BaseDirectory);
var applicationName = _options.ApplicationName;
// Initialize the hosting environment
_hostingEnvironment.Initialize(applicationName, contentRootPath, _options);
_hostingEnvironment.Initialize(contentRootPath, _options);
_context.HostingEnvironment = _hostingEnvironment;
var services = new ServiceCollection();

View File

@ -3,8 +3,7 @@
<Import Project="..\..\build\common.props" />
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netcoreapp2.0</TargetFrameworks>
<TargetFrameworks>netcoreapp2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>

View File

@ -29,7 +29,7 @@ namespace Microsoft.AspNetCore.Hosting.FunctionalTests
{
var logger = loggerFactory.CreateLogger(nameof(ShutdownTestRun));
var applicationPath = Path.Combine(TestPathUtilities.GetSolutionRootDirectory("Hosting"), "test",
var applicationPath = Path.Combine(TestPathUtilities.GetSolutionRootDirectory("Hosting"), "test", "TestAssets",
"Microsoft.AspNetCore.Hosting.TestSites");
var deploymentParameters = new DeploymentParameters(
@ -77,7 +77,7 @@ namespace Microsoft.AspNetCore.Hosting.FunctionalTests
{
var logger = loggerFactory.CreateLogger(nameof(ShutdownTestWaitForShutdown));
var applicationPath = Path.Combine(TestPathUtilities.GetSolutionRootDirectory("Hosting"), "test",
var applicationPath = Path.Combine(TestPathUtilities.GetSolutionRootDirectory("Hosting"), "test", "TestAssets",
"Microsoft.AspNetCore.Hosting.TestSites");
var deploymentParameters = new DeploymentParameters(

View File

@ -0,0 +1,72 @@
// 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.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Testing;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.Extensions.Logging.Testing;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.AspNetCore.Hosting.FunctionalTests
{
public class WebHostBuilderTests : LoggedTest
{
public WebHostBuilderTests(ITestOutputHelper output) : base(output)
{
}
[Fact]
public async Task InjectedStartup_DefaultApplicationNameIsEntryAssembly_CoreClr()
=> await InjectedStartup_DefaultApplicationNameIsEntryAssembly(RuntimeFlavor.CoreClr);
[ConditionalFact]
[OSSkipCondition(OperatingSystems.MacOSX)]
[OSSkipCondition(OperatingSystems.Linux)]
public async Task InjectedStartup_DefaultApplicationNameIsEntryAssembly_Clr()
=> await InjectedStartup_DefaultApplicationNameIsEntryAssembly(RuntimeFlavor.Clr);
private async Task InjectedStartup_DefaultApplicationNameIsEntryAssembly(RuntimeFlavor runtimeFlavor)
{
using (StartLog(out var loggerFactory))
{
var logger = loggerFactory.CreateLogger(nameof(InjectedStartup_DefaultApplicationNameIsEntryAssembly));
var applicationPath = Path.Combine(TestPathUtilities.GetSolutionRootDirectory("Hosting"), "test", "TestAssets", "IStartupInjectionAssemblyName");
var deploymentParameters = new DeploymentParameters(
applicationPath,
ServerType.Kestrel,
runtimeFlavor,
RuntimeArchitecture.x64)
{
TargetFramework = runtimeFlavor == RuntimeFlavor.Clr ? "net461" : "netcoreapp2.0",
ApplicationType = ApplicationType.Portable,
PublishApplicationBeforeDeployment = true
};
using (var deployer = new SelfHostDeployer(deploymentParameters, loggerFactory))
{
await deployer.DeployAsync();
string output = string.Empty;
var mre = new ManualResetEventSlim();
deployer.HostProcess.OutputDataReceived += (sender, args) =>
{
output += args.Data + '\n';
mre.Set();
};
mre.Wait(10000);
output = output.Trim('\n');
Assert.Equal($"IStartupInjectionAssemblyName", output);
}
}
}
}
}

View File

@ -1,22 +0,0 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:49570/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Microsoft.AspNetCore.Hosting.TestSites": {
"commandName": "Project"
}
}
}

View File

@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Hosting.Tests
{
var env = new HostingEnvironment();
env.Initialize("DummyApplication", Path.GetFullPath("."), new WebHostOptions(){ WebRoot = "testroot" });
env.Initialize(Path.GetFullPath("."), new WebHostOptions(){ WebRoot = "testroot" });
Assert.Equal(Path.GetFullPath("."), env.ContentRootPath);
Assert.Equal(Path.GetFullPath("testroot"), env.WebRootPath);
@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Hosting.Tests
{
var env = new HostingEnvironment();
env.Initialize("DummyApplication", Path.GetFullPath("testroot"), new WebHostOptions());
env.Initialize(Path.GetFullPath("testroot"), new WebHostOptions());
Assert.Equal(Path.GetFullPath("testroot"), env.ContentRootPath);
Assert.Equal(Path.GetFullPath(Path.Combine("testroot", "wwwroot")), env.WebRootPath);
@ -41,7 +41,7 @@ namespace Microsoft.AspNetCore.Hosting.Tests
{
var env = new HostingEnvironment();
env.Initialize("DummyApplication", Path.GetFullPath(Path.Combine("testroot", "wwwroot")), new WebHostOptions());
env.Initialize(Path.GetFullPath(Path.Combine("testroot", "wwwroot")), new WebHostOptions());
Assert.Equal(Path.GetFullPath(Path.Combine("testroot", "wwwroot")), env.ContentRootPath);
Assert.Null(env.WebRootPath);
@ -55,7 +55,7 @@ namespace Microsoft.AspNetCore.Hosting.Tests
var env = new HostingEnvironment();
env.EnvironmentName = "SomeName";
env.Initialize("DummyApplication", Path.GetFullPath("."), new WebHostOptions(){ Environment = "NewName" });
env.Initialize(Path.GetFullPath("."), new WebHostOptions(){ Environment = "NewName" });
Assert.Equal("NewName", env.EnvironmentName);
}

View File

@ -13,7 +13,7 @@
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Hosting\Microsoft.AspNetCore.Hosting.csproj" />
<ProjectReference Include="..\TestStartupAssembly1\TestStartupAssembly1.csproj" />
<ProjectReference Include="..\TestAssets\TestStartupAssembly1\TestStartupAssembly1.csproj" />
</ItemGroup>
<ItemGroup>

View File

@ -570,17 +570,14 @@ namespace Microsoft.AspNetCore.Hosting
}
[Fact]
public void DefaultApplicationNameWithNoStartupThrows()
public void DefaultWebHostBuilderWithNoStartupThrows()
{
var builder = new ConfigurationBuilder();
var host = new WebHostBuilder()
.UseServer(new TestServer());
var ex = Assert.Throws<ArgumentException>(() => host.Build());
var ex = Assert.Throws<InvalidOperationException>(() => host.Build());
// ArgumentException adds "Parameter name" to the message and this is the cleanest way to make sure we get the right
// expected string
Assert.Equal(new ArgumentException("A valid non-empty application name must be provided.", "applicationName").Message , ex.Message);
Assert.Contains("No startup configured.", ex.Message);
}
[Fact]

View File

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Import Project="..\..\..\build\common.props" />
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netcoreapp2.0</TargetFrameworks>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\Microsoft.AspNetCore.Hosting\Microsoft.AspNetCore.Hosting.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.AspNetCore.TestHost\Microsoft.AspNetCore.TestHost.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(AspNetCoreVersion)" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,26 @@
// 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.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
namespace IStartupInjectionAssemblyName
{
public class Program
{
public static void Main(string[] args)
{
var server = new TestServer(new WebHostBuilder()
.ConfigureServices(services => services.AddSingleton<IStartup, Startup>()));
// Mimic application startup messages so application deployer knows that the application has started
Console.WriteLine("Application started. Press Ctrl+C to shut down.");
Console.WriteLine("Now listening on: http://localhost:5000");
Task.Run(async () => Console.WriteLine(await server.CreateClient().GetStringAsync(""))).GetAwaiter().GetResult();
}
}
}

View File

@ -0,0 +1,26 @@

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
namespace IStartupInjectionAssemblyName
{
public class Startup : IStartup
{
public void Configure(IApplicationBuilder app)
{
var applicationName = app.ApplicationServices.GetRequiredService<IHostingEnvironment>().ApplicationName;
app.Run(context =>
{
return context.Response.WriteAsync(applicationName);
});
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
return services.BuildServiceProvider();
}
}
}

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Import Project="..\..\build\common.props" />
<Import Project="..\..\..\build\common.props" />
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>
@ -9,7 +9,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Hosting\Microsoft.AspNetCore.Hosting.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.AspNetCore.Hosting\Microsoft.AspNetCore.Hosting.csproj" />
</ItemGroup>
<ItemGroup>

View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\..\build\common.props" />
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\Microsoft.AspNetCore.Hosting.Abstractions\Microsoft.AspNetCore.Hosting.Abstractions.csproj" />
</ItemGroup>
</Project>

View File

@ -1,13 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\build\common.props" />
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Hosting.Abstractions\Microsoft.AspNetCore.Hosting.Abstractions.csproj" />
</ItemGroup>
</Project>