Skip to content

Commit 0859213

Browse files
authored
[IIS] Move to GenericHost (#24280)
1 parent 0437117 commit 0859213

File tree

5 files changed

+360
-231
lines changed

5 files changed

+360
-231
lines changed

src/Servers/IIS/IIS/samples/NativeIISSample/Startup.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Linq;
6+
using System.Threading.Tasks;
67
using Microsoft.AspNetCore.Authentication;
78
using Microsoft.AspNetCore.Builder;
89
using Microsoft.AspNetCore.Hosting;
@@ -12,6 +13,7 @@
1213
using Microsoft.AspNetCore.Http.Features;
1314
using Microsoft.AspNetCore.Server.IIS;
1415
using Microsoft.Extensions.DependencyInjection;
16+
using Microsoft.Extensions.Hosting;
1517

1618
namespace NativeIISSample
1719
{
@@ -115,16 +117,20 @@ public void Configure(IApplicationBuilder app)
115117
"WEBSOCKET_VERSION"
116118
};
117119

118-
public static void Main(string[] args)
120+
public static Task Main(string[] args)
119121
{
120-
var host = new WebHostBuilder()
121-
.UseKestrel()
122-
.UseIIS()
123-
.UseIISIntegration()
124-
.UseStartup<Startup>()
122+
var host = new HostBuilder()
123+
.ConfigureWebHost(webHostBuilder =>
124+
{
125+
webHostBuilder
126+
.UseKestrel()
127+
.UseIIS()
128+
.UseIISIntegration()
129+
.UseStartup<Startup>();
130+
})
125131
.Build();
126132

127-
host.Run();
133+
return host.RunAsync();
128134
}
129135
}
130136
}

src/Servers/IIS/IIS/test/IIS.Tests/Utilities/TestServer.cs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests
2525
{
26-
public class TestServer: IDisposable, IStartup
26+
public class TestServer : IDisposable
2727
{
2828
private const string InProcessHandlerDll = "aspnetcorev2_inprocess.dll";
2929
private const string AspNetCoreModuleDll = "aspnetcorev2.dll";
@@ -51,7 +51,7 @@ public class TestServer: IDisposable, IStartup
5151
public TestConnection CreateConnection() => new TestConnection(_currentPort);
5252

5353
private static IISServerOptions _options;
54-
private IWebHost _host;
54+
private IHost _host;
5555

5656
private string _appHostConfigPath;
5757
private int _currentPort;
@@ -131,16 +131,24 @@ private void InitializeConfig(int port)
131131

132132
private int Main(IntPtr argc, IntPtr argv)
133133
{
134-
var builder = new WebHostBuilder()
135-
.UseIIS()
136-
.ConfigureServices(services =>
134+
_host = new HostBuilder()
135+
.ConfigureWebHost(webHostBuilder =>
137136
{
138-
services.Configure<IISServerOptions>(options => options.MaxRequestBodySize = _options.MaxRequestBodySize);
139-
services.AddSingleton<IStartup>(this);
140-
services.AddSingleton(_loggerFactory);
137+
webHostBuilder
138+
.UseIIS()
139+
.UseSetting(WebHostDefaults.ApplicationKey, typeof(TestServer).GetTypeInfo().Assembly.FullName)
140+
.Configure(app =>
141+
{
142+
app.Map("/start", builder => builder.Run(context => context.Response.WriteAsync("Done")));
143+
_appBuilder(app);
144+
})
145+
.ConfigureServices(services =>
146+
{
147+
services.Configure<IISServerOptions>(options => options.MaxRequestBodySize = _options.MaxRequestBodySize);
148+
services.AddSingleton(_loggerFactory);
149+
});
141150
})
142-
.UseSetting(WebHostDefaults.ApplicationKey, typeof(TestServer).GetTypeInfo().Assembly.FullName);
143-
_host = builder.Build();
151+
.Build();
144152

145153
var doneEvent = new ManualResetEventSlim();
146154
var lifetime = _host.Services.GetService<IHostApplicationLifetime>();
@@ -167,17 +175,6 @@ public void Dispose()
167175
WebCoreLock.Release();
168176
}
169177

170-
public IServiceProvider ConfigureServices(IServiceCollection services)
171-
{
172-
return services.BuildServiceProvider();
173-
}
174-
175-
public void Configure(IApplicationBuilder app)
176-
{
177-
app.Map("/start", builder => builder.Run(context => context.Response.WriteAsync("Done")));
178-
_appBuilder(app);
179-
}
180-
181178
private delegate int hostfxr_main_fn(IntPtr argc, IntPtr argv);
182179

183180
[DllImport(HWebCoreDll)]
@@ -195,7 +192,7 @@ private static extern int WebCoreActivate(
195192
[DllImport(InProcessHandlerDll)]
196193
private static extern int set_main_handler(hostfxr_main_fn main);
197194

198-
[DllImport("kernel32", SetLastError=true, CharSet = CharSet.Ansi)]
195+
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)]
199196
private static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpFileName);
200197

201198
private void Retry(Action func, int attempts)

src/Servers/IIS/IISIntegration/samples/IISSample/Startup.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
using System;
55
using System.Linq;
6+
using System.Threading.Tasks;
67
using Microsoft.AspNetCore.Authentication;
78
using Microsoft.AspNetCore.Builder;
89
using Microsoft.AspNetCore.Hosting;
910
using Microsoft.AspNetCore.Http;
1011
using Microsoft.AspNetCore.Http.Features;
1112
using Microsoft.AspNetCore.Server.IISIntegration;
1213
using Microsoft.Extensions.DependencyInjection;
14+
using Microsoft.Extensions.Hosting;
1315
using Microsoft.Extensions.Logging;
1416

1517
namespace IISSample
@@ -88,19 +90,23 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory, IAu
8890
});
8991
}
9092

91-
public static void Main(string[] args)
93+
public static Task Main(string[] args)
9294
{
93-
var host = new WebHostBuilder()
95+
var host = new HostBuilder()
96+
.ConfigureWebHost(webHostBuidler =>
97+
{
98+
webHostBuidler
99+
.UseKestrel()
100+
.UseStartup<Startup>();
101+
})
94102
.ConfigureLogging((_, factory) =>
95103
{
96104
factory.AddConsole();
97105
factory.AddFilter("Console", level => level >= LogLevel.Debug);
98106
})
99-
.UseKestrel()
100-
.UseStartup<Startup>()
101107
.Build();
102108

103-
host.Run();
109+
return host.RunAsync();
104110
}
105111
}
106112
}

src/Servers/IIS/IISIntegration/test/Tests/IISExtensionTests.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,39 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.Linq;
5+
using System.Threading.Tasks;
56
using Microsoft.AspNetCore.Hosting;
67
using Microsoft.AspNetCore.TestHost;
78
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Hosting;
810
using Xunit;
911

1012
namespace Microsoft.AspNetCore.Server.IISIntegration
1113
{
1214
public class IISExtensionTests
1315
{
1416
[Fact]
15-
public void CallingUseIISIntegrationMultipleTimesWorks()
17+
public async Task CallingUseIISIntegrationMultipleTimesWorks()
1618
{
19+
using var host = new HostBuilder()
20+
.ConfigureWebHost(webHostBuilder =>
21+
{
22+
webHostBuilder
23+
.UseSetting("TOKEN", "TestToken")
24+
.UseSetting("PORT", "12345")
25+
.UseSetting("APPL_PATH", "/")
26+
.UseIISIntegration()
27+
.UseIISIntegration()
28+
.Configure(app => { })
29+
.UseTestServer();
30+
})
31+
.Build();
1732

18-
var builder = new WebHostBuilder()
19-
.UseSetting("TOKEN", "TestToken")
20-
.UseSetting("PORT", "12345")
21-
.UseSetting("APPL_PATH", "/")
22-
.UseIISIntegration()
23-
.UseIISIntegration()
24-
.Configure(app => { });
25-
var server = new TestServer(builder);
33+
var server = host.GetTestServer();
2634

27-
var filters = server.Host.Services.GetServices<IStartupFilter>()
35+
await host.StartAsync();
36+
37+
var filters = server.Services.GetServices<IStartupFilter>()
2838
.OfType<IISSetupFilter>();
2939

3040
Assert.Single(filters);

0 commit comments

Comments
 (0)