Add GenericWebHostService after user code #11437 (#11575)

This commit is contained in:
Chris Ross 2019-06-26 07:46:30 -07:00 committed by GitHub
parent 9f52639909
commit f990751f54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 2 deletions

View File

@ -75,8 +75,6 @@ namespace Microsoft.AspNetCore.Hosting
options.HostingStartupExceptions = _hostingStartupErrors;
});
services.AddHostedService<GenericWebHostService>();
// REVIEW: This is bad since we don't own this type. Anybody could add one of these and it would mess things up
// We need to flow this differently
var listener = new DiagnosticListener("Microsoft.AspNetCore");

View File

@ -1,5 +1,6 @@
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.Extensions.Hosting
{
@ -9,6 +10,7 @@ namespace Microsoft.Extensions.Hosting
{
var webhostBuilder = new GenericWebHostBuilder(builder);
configure(webhostBuilder);
builder.ConfigureServices((context, services) => services.AddHostedService<GenericWebHostService>());
return builder;
}
}

View File

@ -23,6 +23,7 @@ namespace Microsoft.AspNetCore.Hosting.Tests.Fakes
// This is the only one that doesn't pass through
public IWebHost Build()
{
_hostBuilder.ConfigureServices((context, services) => services.AddHostedService<GenericWebHostService>());
return new GenericWebHost(_hostBuilder.Build());
}

View File

@ -1271,6 +1271,80 @@ namespace Microsoft.AspNetCore.Hosting
Assert.True(service.StopCalled);
}
[Theory]
[MemberData(nameof(DefaultWebHostBuilders))]
public async Task HostedServicesStartedBeforeServer(IWebHostBuilder builder)
{
builder.Configure(app => { })
.ConfigureServices(services =>
{
services.AddSingleton<StartOrder>();
services.AddHostedService<MustBeStartedFirst>();
services.AddSingleton<IServer, ServerMustBeStartedSecond>();
});
using var host = builder.Build();
await host.StartAsync();
var ordering = host.Services.GetRequiredService<StartOrder>();
Assert.Equal(2, ordering.Order);
await host.StopAsync();
}
private class StartOrder
{
public int Order { get; set; }
}
private class MustBeStartedFirst : IHostedService
{
public MustBeStartedFirst(StartOrder ordering)
{
Ordering = ordering;
}
public StartOrder Ordering { get; }
public Task StartAsync(CancellationToken cancellationToken)
{
Assert.Equal(0, Ordering.Order);
Ordering.Order++;
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
private class ServerMustBeStartedSecond : IServer
{
public ServerMustBeStartedSecond(StartOrder ordering)
{
Ordering = ordering;
}
public StartOrder Ordering { get; }
public IFeatureCollection Features => null;
public Task StartAsync<TContext>(IHttpApplication<TContext> application, CancellationToken cancellationToken)
{
Assert.Equal(1, Ordering.Order);
Ordering.Order++;
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public void Dispose()
{
}
}
private static void StaticConfigureMethod(IApplicationBuilder app) { }
private IWebHostBuilder CreateWebHostBuilder()