[Blazor] Move all test projects to Generic host (#13891)

* Moves all test assets to use generic host.
* Cleans up unnecessary code.
This commit is contained in:
Javier Calvarro Nelson 2019-09-12 11:11:14 +02:00 committed by GitHub
parent 04f37e59d5
commit db1aca12a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 61 additions and 92 deletions

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
@ -11,6 +11,7 @@
<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Blazor.Server" />
<Reference Include="Microsoft.AspNetCore" />
<Reference Include="Microsoft.Extensions.Hosting" />
</ItemGroup>
</Project>

View File

@ -1,9 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
namespace HostedInAspNet.Server
{
@ -14,12 +13,12 @@ namespace HostedInAspNet.Server
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseConfiguration(new ConfigurationBuilder()
.AddCommandLine(args)
.Build())
.UseStartup<Startup>()
.Build();
public static IHost BuildWebHost(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webHostBuilder =>
{
webHostBuilder.UseStartup<Startup>();
})
.Build();
}
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
@ -11,6 +11,7 @@
<ItemGroup>
<Reference Include="Microsoft.AspNetCore" />
<Reference Include="Microsoft.AspNetCore.Blazor.Server" />
<Reference Include="Microsoft.Extensions.Hosting" />
</ItemGroup>
</Project>

View File

@ -1,9 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
namespace MonoSanity
{
@ -14,12 +13,12 @@ namespace MonoSanity
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseConfiguration(new ConfigurationBuilder()
.AddCommandLine(args)
.Build())
.UseStartup<Startup>()
.Build();
public static IHost BuildWebHost(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webHostBuilder =>
{
webHostBuilder.UseStartup<Startup>();
})
.Build();
}
}

File diff suppressed because one or more lines are too long

View File

@ -7,12 +7,13 @@ using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
{
public class AspNetSiteServerFixture : WebHostServerFixture
{
public delegate IWebHost BuildWebHost(string[] args);
public delegate IHost BuildWebHost(string[] args);
public Assembly ApplicationAssembly { get; set; }
@ -22,7 +23,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
public List<string> AdditionalArguments { get; set; } = new List<string> { "--test-execution-mode", "server" };
protected override IWebHost CreateWebHost()
protected override IHost CreateWebHost()
{
if (BuildWebHostMethod == null)
{

View File

@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
public string PathBase { get; set; }
public string ContentRoot { get; private set; }
protected override IWebHost CreateWebHost()
protected override IHost CreateWebHost()
{
ContentRoot = FindSampleOrTestSitePath(
typeof(TProgram).Assembly.FullName);
@ -38,29 +38,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
args.Add(Environment);
}
return new FakeWebHost(DevHostServerProgram.BuildWebHost(args.ToArray()));
}
private class FakeWebHost : IWebHost
{
private readonly IHost _realHost;
public FakeWebHost(IHost realHost)
{
_realHost = realHost;
}
public IFeatureCollection ServerFeatures => ((IServer)_realHost.Services.GetService(typeof(IServer))).Features;
public IServiceProvider Services => _realHost.Services;
public void Dispose() => _realHost.Dispose();
public void Start() => _realHost.Start();
public Task StartAsync(CancellationToken cancellationToken = default) => _realHost.StartAsync();
public Task StopAsync(CancellationToken cancellationToken = default) => _realHost.StopAsync();
return DevHostServerProgram.BuildWebHost(args.ToArray());
}
}
}

View File

@ -1,10 +1,11 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
{
@ -16,7 +17,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
{
public string SampleSiteName { get; set; }
protected override IWebHost CreateWebHost()
protected override IHost CreateWebHost()
{
if (string.IsNullOrEmpty(SampleSiteName))
{
@ -25,12 +26,13 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
var sampleSitePath = FindSampleOrTestSitePath(SampleSiteName);
return new WebHostBuilder()
.UseKestrel()
.UseContentRoot(sampleSitePath)
.UseWebRoot(string.Empty)
.UseStartup<StaticSiteStartup>()
.UseUrls("http://127.0.0.1:0")
return new HostBuilder()
.ConfigureWebHost(webHostBuilder => webHostBuilder
.UseKestrel()
.UseContentRoot(sampleSitePath)
.UseWebRoot(string.Empty)
.UseStartup<StaticSiteStartup>()
.UseUrls("http://127.0.0.1:0"))
.Build();
}

View File

@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
{
@ -12,7 +13,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
{
public string PathBase { get; set; }
public IWebHost Host { get; set; }
public IHost Host { get; set; }
public ExecutionMode ExecutionMode { get; set; } = ExecutionMode.Client;

View File

@ -3,7 +3,10 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using Microsoft.AspNetCore.Hosting.Server;
namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
{
@ -13,12 +16,12 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
{
Host = CreateWebHost();
RunInBackgroundThread(Host.Start);
return Host.ServerFeatures
return Host.Services.GetRequiredService<IServer>().Features
.Get<IServerAddressesFeature>()
.Addresses.Single();
}
public IWebHost Host { get; set; }
public IHost Host { get; set; }
public override void Dispose()
{
@ -28,6 +31,6 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
Host?.StopAsync();
}
protected abstract IWebHost CreateWebHost();
protected abstract IHost CreateWebHost();
}
}

View File

@ -22,7 +22,7 @@ namespace TestServer
{
public static void Main(string[] args)
{
var createIndividualHosts = new Dictionary<string, (IWebHost host, string basePath)>
var createIndividualHosts = new Dictionary<string, (IHost host, string basePath)>
{
["Client authentication"] = (BuildWebHost<AuthenticationStartup>(CreateAdditionalArgs(args)), "/subdir"),
["Server authentication"] = (BuildWebHost<ServerAuthenticationStartup>(CreateAdditionalArgs(args)), "/subdir"),
@ -41,13 +41,13 @@ namespace TestServer
var testAppInfo = mainHost.Services.GetRequiredService<TestAppInfo>();
testAppInfo.Scenarios = createIndividualHosts
.ToDictionary(kvp => kvp.Key,
kvp => kvp.Value.host.ServerFeatures.Get<IServerAddressesFeature>().Addresses.FirstOrDefault()
kvp => kvp.Value.host.Services.GetRequiredService<IServer>().Features.Get<IServerAddressesFeature>().Addresses.FirstOrDefault()
.Replace("127.0.0.1", "localhost") + kvp.Value.basePath);
mainHost.Run();
}
private static (IWebHost host, string basePath) CreateDevServerHost(string[] args)
private static (IHost host, string basePath) CreateDevServerHost(string[] args)
{
var contentRoot = typeof(Program).Assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
.Single(a => a.Key == "Microsoft.AspNetCore.Testing.BasicTestApp.ContentRoot")
@ -60,46 +60,30 @@ namespace TestServer
"--applicationpath", typeof(BasicTestApp.Program).Assembly.Location,
}).ToArray();
var host = DevServerProgram.BuildWebHost(finalArgs);
return (new WebHostShim(host), "/subdir");
}
private class WebHostShim : IWebHost
{
private readonly IHost _host;
public WebHostShim(IHost host) => _host = host;
public IFeatureCollection ServerFeatures => _host.Services.GetRequiredService<IServer>().Features;
public IServiceProvider Services => _host.Services;
public void Dispose() => _host.Dispose();
public void Start() => _host.Start();
public Task StartAsync(CancellationToken cancellationToken = default) => _host.StartAsync(cancellationToken);
public Task StopAsync(CancellationToken cancellationToken = default) => _host.StopAsync(cancellationToken);
return (host, "/subdir");
}
private static string[] CreateAdditionalArgs(string[] args) =>
args.Concat(new[] { "--urls", "http://127.0.0.1:0" }).ToArray();
public static IWebHost BuildWebHost(string[] args) => BuildWebHost<Startup>(args);
public static IHost BuildWebHost(string[] args) => BuildWebHost<Startup>(args);
public static IWebHost BuildWebHost<TStartup>(string[] args) where TStartup : class =>
WebHost.CreateDefaultBuilder(args)
public static IHost BuildWebHost<TStartup>(string[] args) where TStartup : class =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging((ctx, lb) =>
{
TestSink sink = new TestSink();
lb.AddProvider(new TestLoggerProvider(sink));
lb.Services.Add(ServiceDescriptor.Singleton(sink));
})
.UseConfiguration(new ConfigurationBuilder()
.AddCommandLine(args)
.Build())
.UseStartup<TStartup>()
.UseStaticWebAssets()
.ConfigureWebHostDefaults(webHostBuilder =>
{
webHostBuilder.UseStartup<TStartup>();
// We require this line because we run in Production environment
// and static web assets are only on by default during development.
webHostBuilder.UseStaticWebAssets();
})
.Build();
}
}