|
6 | 6 | using System.IO;
|
7 | 7 | using System.Linq;
|
8 | 8 | using System.Reflection;
|
| 9 | +using System.Runtime.ExceptionServices; |
9 | 10 | using System.Threading;
|
10 | 11 | using System.Threading.Tasks;
|
| 12 | +using System.Web; |
11 | 13 | using Microsoft.AspNetCore.Builder;
|
12 | 14 | using Microsoft.AspNetCore.Hosting;
|
13 | 15 | using Microsoft.AspNetCore.Hosting.Fakes;
|
14 | 16 | using Microsoft.AspNetCore.Hosting.Server;
|
15 | 17 | using Microsoft.AspNetCore.Hosting.Tests.Fakes;
|
16 | 18 | using Microsoft.AspNetCore.Http;
|
| 19 | +using Microsoft.AspNetCore.Http.Extensions; |
17 | 20 | using Microsoft.AspNetCore.Http.Features;
|
| 21 | +using Microsoft.AspNetCore.WebUtilities; |
18 | 22 | using Microsoft.Extensions.Configuration;
|
19 | 23 | using Microsoft.Extensions.DependencyInjection;
|
20 | 24 | using Microsoft.Extensions.Hosting;
|
@@ -68,6 +72,54 @@ public async Task StartupStaticCtorThrows_Fallback(IWebHostBuilder builder)
|
68 | 72 | }
|
69 | 73 | }
|
70 | 74 |
|
| 75 | + [Theory] |
| 76 | + [MemberData(nameof(DefaultWebHostBuildersWithConfig))] |
| 77 | + public async Task MultipleUseStartupCallsLastWins(IWebHostBuilder builder) |
| 78 | + { |
| 79 | + var server = new TestServer(); |
| 80 | + var host = builder.UseServer(server) |
| 81 | + .UseStartup<StartupCtorThrows>() |
| 82 | + .UseStartup(context => throw new InvalidOperationException("This doesn't run")) |
| 83 | + .Configure(app => |
| 84 | + { |
| 85 | + throw new InvalidOperationException("This doesn't run"); |
| 86 | + }) |
| 87 | + .Configure(app => |
| 88 | + { |
| 89 | + app.Run(context => |
| 90 | + { |
| 91 | + return context.Response.WriteAsync("This wins"); |
| 92 | + }); |
| 93 | + }) |
| 94 | + .Build(); |
| 95 | + using (host) |
| 96 | + { |
| 97 | + await host.StartAsync(); |
| 98 | + await AssertResponseContains(server.RequestDelegate, "This wins"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + [Theory] |
| 103 | + [MemberData(nameof(DefaultWebHostBuildersWithConfig))] |
| 104 | + public async Task UseStartupFactoryWorks(IWebHostBuilder builder) |
| 105 | + { |
| 106 | + void ConfigureServices(IServiceCollection services) { } |
| 107 | + void Configure(IApplicationBuilder app) |
| 108 | + { |
| 109 | + app.Run(context => context.Response.WriteAsync("UseStartupFactoryWorks")); |
| 110 | + } |
| 111 | + |
| 112 | + var server = new TestServer(); |
| 113 | + var host = builder.UseServer(server) |
| 114 | + .UseStartup(context => new DelegatingStartup(ConfigureServices, Configure)) |
| 115 | + .Build(); |
| 116 | + using (host) |
| 117 | + { |
| 118 | + await host.StartAsync(); |
| 119 | + await AssertResponseContains(server.RequestDelegate, "UseStartupFactoryWorks"); |
| 120 | + } |
| 121 | + } |
| 122 | + |
71 | 123 | [Theory]
|
72 | 124 | [MemberData(nameof(DefaultWebHostBuildersWithConfig))]
|
73 | 125 | public async Task StartupCtorThrows_Fallback(IWebHostBuilder builder)
|
@@ -199,7 +251,7 @@ public void ConfigureDefaultServiceProviderWithContext(IWebHostBuilder builder)
|
199 | 251 | options.ValidateScopes = true;
|
200 | 252 | });
|
201 | 253 |
|
202 |
| - using var host = hostBuilder.Build(); |
| 254 | + using var host = hostBuilder.Build(); |
203 | 255 | Assert.Throws<InvalidOperationException>(() => host.Start());
|
204 | 256 | Assert.True(configurationCallbackCalled);
|
205 | 257 | }
|
@@ -728,6 +780,22 @@ public void DefaultApplicationNameWithConfigure(IWebHostBuilder builder)
|
728 | 780 | }
|
729 | 781 | }
|
730 | 782 |
|
| 783 | + [Theory] |
| 784 | + [MemberData(nameof(DefaultWebHostBuilders))] |
| 785 | + public void DefaultApplicationNameWithUseStartupFactory(IWebHostBuilder builder) |
| 786 | + { |
| 787 | + using (var host = builder |
| 788 | + .UseServer(new TestServer()) |
| 789 | + .UseStartup(context => new DelegatingStartup(s => { }, app => { })) |
| 790 | + .Build()) |
| 791 | + { |
| 792 | + var hostingEnv = host.Services.GetService<IHostEnvironment>(); |
| 793 | + |
| 794 | + // Should be the assembly containing this test, because that's where the delegate comes from |
| 795 | + Assert.Equal(typeof(WebHostBuilderTests).Assembly.GetName().Name, hostingEnv.ApplicationName); |
| 796 | + } |
| 797 | + } |
| 798 | + |
731 | 799 | [Theory]
|
732 | 800 | [MemberData(nameof(DefaultWebHostBuilders))]
|
733 | 801 | public void Configure_SupportsNonStaticMethodDelegate(IWebHostBuilder builder)
|
@@ -1218,7 +1286,7 @@ public void UseConfigurationWithSectionAddsSubKeys(IWebHostBuilder builder)
|
1218 | 1286 |
|
1219 | 1287 | Assert.Equal("nestedvalue", builder.GetSetting("key"));
|
1220 | 1288 |
|
1221 |
| - using var host = builder.Build(); |
| 1289 | + using var host = builder.Build(); |
1222 | 1290 | var appConfig = host.Services.GetRequiredService<IConfiguration>();
|
1223 | 1291 | Assert.Equal("nestedvalue", appConfig["key"]);
|
1224 | 1292 | }
|
@@ -1574,6 +1642,21 @@ public void Configure(IWebHostBuilder builder)
|
1574 | 1642 | }
|
1575 | 1643 | }
|
1576 | 1644 |
|
| 1645 | + public class DelegatingStartup |
| 1646 | + { |
| 1647 | + private readonly Action<IServiceCollection> _configureServices; |
| 1648 | + private readonly Action<IApplicationBuilder> _configure; |
| 1649 | + |
| 1650 | + public DelegatingStartup(Action<IServiceCollection> configureServices, Action<IApplicationBuilder> configure) |
| 1651 | + { |
| 1652 | + _configureServices = configureServices; |
| 1653 | + _configure = configure; |
| 1654 | + } |
| 1655 | + |
| 1656 | + public void ConfigureServices(IServiceCollection services) => _configureServices(services); |
| 1657 | + public void Configure(IApplicationBuilder app) => _configure(app); |
| 1658 | + } |
| 1659 | + |
1577 | 1660 | public class StartupWithResolvedDisposableThatThrows
|
1578 | 1661 | {
|
1579 | 1662 | public StartupWithResolvedDisposableThatThrows(DisposableService service)
|
|
0 commit comments