Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit cfe9b26

Browse files
authored
Added support for generic host based IWebHostBuilder (#1580)
- This adds an implementation of IWebHostBuilder as a facade over the IHostBuilder. This removes the 2 container issue by executing the Startup.ConfigureServies and Startup.ConfigureContainer inline as part of building the IHostBuilder. - The implementation is highly compatible implementation since it exposes the same IWebHostBuilder interface. Existing extensions mostly work. - There are some caveats with this approach. - Injecting services into Startup is not extremely constrained to the services availble on HostBuilderContext. This includes the IHostingEnvironment and the IConfiguration. - IStartup is broken when using this pattern because it isn't composable. - The IStartupConfigureServicesFilter and IStartupConfigureContainer The before and after filters added in 2.1 are also broken because there's a single container (it could maybe be fixed by downcasting and doing something specific on the GenericHostBuilder instance). - Calling into IWebHostBuilder.Build will throw a NotSupportedException since this implementation is just a facade over the IHostBuilder.
1 parent d7b9fd4 commit cfe9b26

22 files changed

+1250
-340
lines changed

samples/GenericWebHost/Program.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
using System;
2-
using System.Net;
3-
using System.Threading.Tasks;
1+
using System.Threading.Tasks;
42
using Microsoft.AspNetCore.Builder;
53
using Microsoft.AspNetCore.Http;
64
using Microsoft.Extensions.Configuration;
75
using Microsoft.Extensions.Hosting;
6+
using Microsoft.AspNetCore.Hosting;
87

98
namespace GenericWebHost
109
{
@@ -19,22 +18,20 @@ public static async Task Main(string[] args)
1918
config.AddJsonFile("appsettings.json", optional: true);
2019
config.AddCommandLine(args);
2120
})
22-
.ConfigureServices((hostContext, services) =>
23-
{
24-
})
2521
.UseFakeServer()
26-
.ConfigureWebHost((hostContext, app) =>
22+
.ConfigureWebHost(builder =>
2723
{
28-
app.Run(async (context) =>
24+
builder.Configure(app =>
2925
{
30-
await context.Response.WriteAsync("Hello World!");
26+
app.Run(async (context) =>
27+
{
28+
await context.Response.WriteAsync("Hello World!");
29+
});
3130
});
3231
})
3332
.UseConsoleLifetime()
3433
.Build();
3534

36-
var s = host.Services;
37-
3835
await host.RunAsync();
3936
}
4037
}

samples/GenericWebHost/WebHostExtensions.cs

Lines changed: 0 additions & 43 deletions
This file was deleted.

samples/GenericWebHost/WebHostService.cs

Lines changed: 0 additions & 62 deletions
This file was deleted.

samples/GenericWebHost/WebHostServiceOptions.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Threading;
5+
6+
namespace Microsoft.AspNetCore.Hosting.Internal
7+
{
8+
internal class GenericWebHostApplicationLifetime : IApplicationLifetime
9+
{
10+
private readonly Microsoft.Extensions.Hosting.IApplicationLifetime _applicationLifetime;
11+
public GenericWebHostApplicationLifetime(Microsoft.Extensions.Hosting.IApplicationLifetime applicationLifetime)
12+
{
13+
_applicationLifetime = applicationLifetime;
14+
}
15+
16+
public CancellationToken ApplicationStarted => _applicationLifetime.ApplicationStarted;
17+
18+
public CancellationToken ApplicationStopping => _applicationLifetime.ApplicationStopping;
19+
20+
public CancellationToken ApplicationStopped => _applicationLifetime.ApplicationStopped;
21+
22+
public void StopApplication() => _applicationLifetime.StopApplication();
23+
}
24+
}

0 commit comments

Comments
 (0)