Skip to content

Add GenericWebHostService after user code #11575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ public GenericWebHostBuilder(IHostBuilder builder)
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");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.Extensions.Hosting
{
Expand All @@ -9,6 +10,7 @@ public static IHostBuilder ConfigureWebHost(this IHostBuilder builder, Action<IW
{
var webhostBuilder = new GenericWebHostBuilder(builder);
configure(webhostBuilder);
builder.ConfigureServices((context, services) => services.AddHostedService<GenericWebHostService>());
return builder;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal GenericWebHostBuilderWrapper(HostBuilder hostBuilder)
// 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());
}

Expand Down
74 changes: 74 additions & 0 deletions src/Hosting/Hosting/test/WebHostBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,80 @@ public async Task ThrowingFromHostedServiceStopsOtherHostedServicesFromRunningSt
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()
Expand Down