diff --git a/src/Microsoft.AspNet.Hosting/Properties/AssemblyInfo.cs b/src/Microsoft.AspNet.Hosting/Properties/AssemblyInfo.cs index 025a94598c..6145905b3a 100644 --- a/src/Microsoft.AspNet.Hosting/Properties/AssemblyInfo.cs +++ b/src/Microsoft.AspNet.Hosting/Properties/AssemblyInfo.cs @@ -2,5 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Reflection; +using System.Runtime.CompilerServices; -[assembly: AssemblyMetadata("Serviceable", "True")] \ No newline at end of file +[assembly: AssemblyMetadata("Serviceable", "True")] +[assembly: InternalsVisibleTo("Microsoft.AspNet.Hosting.Tests")] diff --git a/src/Microsoft.AspNet.Hosting/WebHostBuilder.cs b/src/Microsoft.AspNet.Hosting/WebHostBuilder.cs index 4ae42e9c46..052f4740c1 100644 --- a/src/Microsoft.AspNet.Hosting/WebHostBuilder.cs +++ b/src/Microsoft.AspNet.Hosting/WebHostBuilder.cs @@ -42,8 +42,10 @@ namespace Microsoft.AspNet.Hosting private string _serverFactoryLocation; private IServerFactory _serverFactory; - public WebHostBuilder([NotNull] IServiceProvider services) - : this(services, config: new ConfigurationBuilder().Build()) { } + public WebHostBuilder([NotNull] IServiceProvider services) + : this(services, config: new ConfigurationBuilder().Build()) + { + } public WebHostBuilder([NotNull] IServiceProvider services, [NotNull] IConfiguration config) { @@ -109,7 +111,7 @@ namespace Microsoft.AspNet.Hosting // Only one of these should be set, but they are used in priority engine.Startup = _startup; engine.StartupType = _startupType; - engine.StartupAssemblyName = _config.Get(ApplicationKey) ?? _config.Get(OldApplicationKey) ?? appEnvironment.ApplicationName; + engine.StartupAssemblyName = _startupAssemblyName ?? _config.Get(ApplicationKey) ?? _config.Get(OldApplicationKey) ?? appEnvironment.ApplicationName; return engine; } @@ -177,7 +179,8 @@ namespace Microsoft.AspNet.Hosting public WebHostBuilder UseStartup([NotNull] Action configureApp, Action configureServices) { _startup = new StartupMethods(configureApp, - services => { + services => + { if (configureServices != null) { configureServices(services); @@ -187,4 +190,4 @@ namespace Microsoft.AspNet.Hosting return this; } } -} \ No newline at end of file +} diff --git a/test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs b/test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs new file mode 100644 index 0000000000..94cf636d9c --- /dev/null +++ b/test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs @@ -0,0 +1,34 @@ +// 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 Microsoft.AspNet.Hosting.Internal; +using Microsoft.Framework.Runtime.Infrastructure; +using Xunit; + +namespace Microsoft.AspNet.Hosting +{ + public class WebHostBuilderTests + { + [Fact] + public void Build_uses_application_for_startup_assembly_by_default() + { + var builder = CreateWebHostBuilder(); + + var engine = (HostingEngine)builder.Build(); + + Assert.Equal("Microsoft.AspNet.Hosting.Tests", engine.StartupAssemblyName); + } + + [Fact] + public void Build_honors_UseStartup_with_string() + { + var builder = CreateWebHostBuilder(); + + var engine = (HostingEngine)builder.UseStartup("MyStartupAssembly").Build(); + + Assert.Equal("MyStartupAssembly", engine.StartupAssemblyName); + } + + private WebHostBuilder CreateWebHostBuilder() => new WebHostBuilder(CallContextServiceLocator.Locator.ServiceProvider); + } +}