[IIS] Move to GenericHost (#24280)

This commit is contained in:
Kahbazi 2020-07-28 22:32:09 +04:30 committed by GitHub
parent 0437117cfb
commit 085921305a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 360 additions and 231 deletions

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
@ -12,6 +13,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.IIS; using Microsoft.AspNetCore.Server.IIS;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace NativeIISSample namespace NativeIISSample
{ {
@ -115,16 +117,20 @@ namespace NativeIISSample
"WEBSOCKET_VERSION" "WEBSOCKET_VERSION"
}; };
public static void Main(string[] args) public static Task Main(string[] args)
{ {
var host = new WebHostBuilder() var host = new HostBuilder()
.UseKestrel() .ConfigureWebHost(webHostBuilder =>
.UseIIS() {
.UseIISIntegration() webHostBuilder
.UseStartup<Startup>() .UseKestrel()
.UseIIS()
.UseIISIntegration()
.UseStartup<Startup>();
})
.Build(); .Build();
host.Run(); return host.RunAsync();
} }
} }
} }

View File

@ -23,7 +23,7 @@ using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests
{ {
public class TestServer: IDisposable, IStartup public class TestServer : IDisposable
{ {
private const string InProcessHandlerDll = "aspnetcorev2_inprocess.dll"; private const string InProcessHandlerDll = "aspnetcorev2_inprocess.dll";
private const string AspNetCoreModuleDll = "aspnetcorev2.dll"; private const string AspNetCoreModuleDll = "aspnetcorev2.dll";
@ -51,7 +51,7 @@ namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests
public TestConnection CreateConnection() => new TestConnection(_currentPort); public TestConnection CreateConnection() => new TestConnection(_currentPort);
private static IISServerOptions _options; private static IISServerOptions _options;
private IWebHost _host; private IHost _host;
private string _appHostConfigPath; private string _appHostConfigPath;
private int _currentPort; private int _currentPort;
@ -131,16 +131,24 @@ namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests
private int Main(IntPtr argc, IntPtr argv) private int Main(IntPtr argc, IntPtr argv)
{ {
var builder = new WebHostBuilder() _host = new HostBuilder()
.UseIIS() .ConfigureWebHost(webHostBuilder =>
.ConfigureServices(services =>
{ {
services.Configure<IISServerOptions>(options => options.MaxRequestBodySize = _options.MaxRequestBodySize); webHostBuilder
services.AddSingleton<IStartup>(this); .UseIIS()
services.AddSingleton(_loggerFactory); .UseSetting(WebHostDefaults.ApplicationKey, typeof(TestServer).GetTypeInfo().Assembly.FullName)
.Configure(app =>
{
app.Map("/start", builder => builder.Run(context => context.Response.WriteAsync("Done")));
_appBuilder(app);
})
.ConfigureServices(services =>
{
services.Configure<IISServerOptions>(options => options.MaxRequestBodySize = _options.MaxRequestBodySize);
services.AddSingleton(_loggerFactory);
});
}) })
.UseSetting(WebHostDefaults.ApplicationKey, typeof(TestServer).GetTypeInfo().Assembly.FullName); .Build();
_host = builder.Build();
var doneEvent = new ManualResetEventSlim(); var doneEvent = new ManualResetEventSlim();
var lifetime = _host.Services.GetService<IHostApplicationLifetime>(); var lifetime = _host.Services.GetService<IHostApplicationLifetime>();
@ -167,17 +175,6 @@ namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests
WebCoreLock.Release(); WebCoreLock.Release();
} }
public IServiceProvider ConfigureServices(IServiceCollection services)
{
return services.BuildServiceProvider();
}
public void Configure(IApplicationBuilder app)
{
app.Map("/start", builder => builder.Run(context => context.Response.WriteAsync("Done")));
_appBuilder(app);
}
private delegate int hostfxr_main_fn(IntPtr argc, IntPtr argv); private delegate int hostfxr_main_fn(IntPtr argc, IntPtr argv);
[DllImport(HWebCoreDll)] [DllImport(HWebCoreDll)]
@ -195,7 +192,7 @@ namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests
[DllImport(InProcessHandlerDll)] [DllImport(InProcessHandlerDll)]
private static extern int set_main_handler(hostfxr_main_fn main); private static extern int set_main_handler(hostfxr_main_fn main);
[DllImport("kernel32", SetLastError=true, CharSet = CharSet.Ansi)] [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)]
private static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpFileName); private static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpFileName);
private void Retry(Action func, int attempts) private void Retry(Action func, int attempts)

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
@ -10,6 +11,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.IISIntegration; using Microsoft.AspNetCore.Server.IISIntegration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace IISSample namespace IISSample
@ -88,19 +90,23 @@ namespace IISSample
}); });
} }
public static void Main(string[] args) public static Task Main(string[] args)
{ {
var host = new WebHostBuilder() var host = new HostBuilder()
.ConfigureWebHost(webHostBuidler =>
{
webHostBuidler
.UseKestrel()
.UseStartup<Startup>();
})
.ConfigureLogging((_, factory) => .ConfigureLogging((_, factory) =>
{ {
factory.AddConsole(); factory.AddConsole();
factory.AddFilter("Console", level => level >= LogLevel.Debug); factory.AddFilter("Console", level => level >= LogLevel.Debug);
}) })
.UseKestrel()
.UseStartup<Startup>()
.Build(); .Build();
host.Run(); return host.RunAsync();
} }
} }
} }

View File

@ -2,9 +2,11 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost; using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Server.IISIntegration namespace Microsoft.AspNetCore.Server.IISIntegration
@ -12,19 +14,27 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
public class IISExtensionTests public class IISExtensionTests
{ {
[Fact] [Fact]
public void CallingUseIISIntegrationMultipleTimesWorks() public async Task CallingUseIISIntegrationMultipleTimesWorks()
{ {
using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseSetting("TOKEN", "TestToken")
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/")
.UseIISIntegration()
.UseIISIntegration()
.Configure(app => { })
.UseTestServer();
})
.Build();
var builder = new WebHostBuilder() var server = host.GetTestServer();
.UseSetting("TOKEN", "TestToken")
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/")
.UseIISIntegration()
.UseIISIntegration()
.Configure(app => { });
var server = new TestServer(builder);
var filters = server.Host.Services.GetServices<IStartupFilter>() await host.StartAsync();
var filters = server.Services.GetServices<IStartupFilter>()
.OfType<IISSetupFilter>(); .OfType<IISSetupFilter>();
Assert.Single(filters); Assert.Single(filters);

View File

@ -4,7 +4,6 @@
using System; using System;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
@ -12,6 +11,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http.Features.Authentication; using Microsoft.AspNetCore.Http.Features.Authentication;
using Microsoft.AspNetCore.TestHost; using Microsoft.AspNetCore.TestHost;
using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Xunit; using Xunit;
@ -25,21 +25,30 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
{ {
var assertsExecuted = false; var assertsExecuted = false;
var builder = new WebHostBuilder() using var host = new HostBuilder()
.UseSetting("PORT", "12345") .ConfigureWebHost(webHostBuilder =>
.UseSetting("APPL_PATH", "/")
.UseIISIntegration()
.Configure(app =>
{ {
app.Run(context => webHostBuilder
{ .UseSetting("PORT", "12345")
var auth = context.Features.Get<IHttpAuthenticationFeature>(); .UseSetting("APPL_PATH", "/")
Assert.Null(auth); .UseIISIntegration()
assertsExecuted = true; .Configure(app =>
return Task.FromResult(0); {
}); app.Run(context =>
}); {
var server = new TestServer(builder); var auth = context.Features.Get<IHttpAuthenticationFeature>();
Assert.Null(auth);
assertsExecuted = true;
return Task.FromResult(0);
});
})
.UseTestServer();
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var req = new HttpRequestMessage(HttpMethod.Get, ""); var req = new HttpRequestMessage(HttpMethod.Get, "");
req.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken"); req.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken");
@ -53,22 +62,31 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
{ {
var assertsExecuted = false; var assertsExecuted = false;
var builder = new WebHostBuilder() using var host = new HostBuilder()
.UseSetting("TOKEN", "TestToken") .ConfigureWebHost(webHostBuilder =>
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/")
.UseIISIntegration()
.Configure(app =>
{ {
app.Run(context => webHostBuilder
{ .UseSetting("TOKEN", "TestToken")
var auth = context.Features.Get<IHttpAuthenticationFeature>(); .UseSetting("PORT", "12345")
Assert.Null(auth); .UseSetting("APPL_PATH", "/")
assertsExecuted = true; .UseIISIntegration()
return Task.FromResult(0); .Configure(app =>
}); {
}); app.Run(context =>
var server = new TestServer(builder); {
var auth = context.Features.Get<IHttpAuthenticationFeature>();
Assert.Null(auth);
assertsExecuted = true;
return Task.FromResult(0);
});
})
.UseTestServer();
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var req = new HttpRequestMessage(HttpMethod.Get, ""); var req = new HttpRequestMessage(HttpMethod.Get, "");
var response = await server.CreateClient().SendAsync(req); var response = await server.CreateClient().SendAsync(req);
@ -85,23 +103,32 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
{ {
var requestExecuted = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously); var requestExecuted = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
var applicationStoppingFired = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously); var applicationStoppingFired = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
var builder = new WebHostBuilder() using var host = new HostBuilder()
.UseSetting("TOKEN", "TestToken") .ConfigureWebHost(webHostBuilder =>
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", pathBase)
.UseIISIntegration()
.Configure(app =>
{ {
var appLifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>(); webHostBuilder
appLifetime.ApplicationStopping.Register(() => applicationStoppingFired.SetResult(0)); .UseSetting("TOKEN", "TestToken")
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", pathBase)
.UseIISIntegration()
.Configure(app =>
{
var appLifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();
appLifetime.ApplicationStopping.Register(() => applicationStoppingFired.SetResult(0));
app.Run(context => app.Run(context =>
{ {
requestExecuted.SetResult(0); requestExecuted.SetResult(0);
return Task.FromResult(0); return Task.FromResult(0);
}); });
}); })
var server = new TestServer(builder); .UseTestServer();
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var request = new HttpRequestMessage(HttpMethod.Post, requestPath); var request = new HttpRequestMessage(HttpMethod.Post, requestPath);
request.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken"); request.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken");
@ -135,23 +162,32 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
{ {
var requestExecuted = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously); var requestExecuted = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
var applicationStoppingFired = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously); var applicationStoppingFired = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
var builder = new WebHostBuilder() using var host = new HostBuilder()
.UseSetting("TOKEN", "TestToken") .ConfigureWebHost(webHostBuilder =>
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/")
.UseIISIntegration()
.Configure(app =>
{ {
var appLifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>(); webHostBuilder
appLifetime.ApplicationStopping.Register(() => applicationStoppingFired.SetResult(0)); .UseSetting("TOKEN", "TestToken")
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/")
.UseIISIntegration()
.Configure(app =>
{
var appLifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();
appLifetime.ApplicationStopping.Register(() => applicationStoppingFired.SetResult(0));
app.Run(context => app.Run(context =>
{ {
requestExecuted.SetResult(0); requestExecuted.SetResult(0);
return Task.FromResult(0); return Task.FromResult(0);
}); });
}); })
var server = new TestServer(builder); .UseTestServer();
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var request = new HttpRequestMessage(method, "/iisintegration"); var request = new HttpRequestMessage(method, "/iisintegration");
request.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken"); request.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken");
@ -171,23 +207,32 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
{ {
var requestExecuted = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously); var requestExecuted = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
var applicationStoppingFired = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously); var applicationStoppingFired = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
var builder = new WebHostBuilder() using var host = new HostBuilder()
.UseSetting("TOKEN", "TestToken") .ConfigureWebHost(webHostBuilder =>
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/")
.UseIISIntegration()
.Configure(app =>
{ {
var appLifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>(); webHostBuilder
appLifetime.ApplicationStopping.Register(() => applicationStoppingFired.SetResult(0)); .UseSetting("TOKEN", "TestToken")
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/")
.UseIISIntegration()
.Configure(app =>
{
var appLifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();
appLifetime.ApplicationStopping.Register(() => applicationStoppingFired.SetResult(0));
app.Run(context => app.Run(context =>
{ {
requestExecuted.SetResult(0); requestExecuted.SetResult(0);
return Task.FromResult(0); return Task.FromResult(0);
}); });
}); })
var server = new TestServer(builder); .UseTestServer();
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var request = new HttpRequestMessage(HttpMethod.Post, path); var request = new HttpRequestMessage(HttpMethod.Post, path);
request.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken"); request.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken");
@ -207,23 +252,32 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
{ {
var requestExecuted = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously); var requestExecuted = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
var applicationStoppingFired = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously); var applicationStoppingFired = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
var builder = new WebHostBuilder() using var host = new HostBuilder()
.UseSetting("TOKEN", "TestToken") .ConfigureWebHost(webHostBuilder =>
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/")
.UseIISIntegration()
.Configure(app =>
{ {
var appLifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>(); webHostBuilder
appLifetime.ApplicationStopping.Register(() => applicationStoppingFired.SetResult(0)); .UseSetting("TOKEN", "TestToken")
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/")
.UseIISIntegration()
.Configure(app =>
{
var appLifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();
appLifetime.ApplicationStopping.Register(() => applicationStoppingFired.SetResult(0));
app.Run(context => app.Run(context =>
{ {
requestExecuted.SetResult(0); requestExecuted.SetResult(0);
return Task.FromResult(0); return Task.FromResult(0);
}); });
}); })
var server = new TestServer(builder); .UseTestServer();
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var request = new HttpRequestMessage(HttpMethod.Post, "/iisintegration"); var request = new HttpRequestMessage(HttpMethod.Post, "/iisintegration");
request.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken"); request.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken");
@ -236,43 +290,63 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
} }
[Fact] [Fact]
public void UrlDelayRegisteredAndPreferHostingUrlsSet() public async Task UrlDelayRegisteredAndPreferHostingUrlsSet()
{ {
var builder = new WebHostBuilder() using var host = new HostBuilder()
.UseSetting("TOKEN", "TestToken") .ConfigureWebHost(webHostBuilder =>
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/")
.UseIISIntegration()
.Configure(app =>
{ {
app.Run(context => Task.FromResult(0)); webHostBuilder
}); .UseSetting("TOKEN", "TestToken")
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/")
.UseIISIntegration()
.Configure(app =>
{
app.Run(context => Task.FromResult(0));
});
Assert.Null(builder.GetSetting(WebHostDefaults.ServerUrlsKey)); Assert.Null(webHostBuilder.GetSetting(WebHostDefaults.ServerUrlsKey));
Assert.Null(builder.GetSetting(WebHostDefaults.PreferHostingUrlsKey)); Assert.Null(webHostBuilder.GetSetting(WebHostDefaults.PreferHostingUrlsKey));
// Adds a server and calls Build() webHostBuilder.UseTestServer();
var server = new TestServer(builder); })
.Build();
Assert.Equal("http://127.0.0.1:12345", builder.GetSetting(WebHostDefaults.ServerUrlsKey)); var server = host.GetTestServer();
Assert.Equal("true", builder.GetSetting(WebHostDefaults.PreferHostingUrlsKey));
await host.StartAsync();
var configuration = host.Services.GetService<IConfiguration>();
Assert.Equal("http://127.0.0.1:12345", configuration[WebHostDefaults.ServerUrlsKey]);
Assert.Equal("true", configuration[WebHostDefaults.PreferHostingUrlsKey]);
} }
[Fact] [Fact]
public void PathBaseHiddenFromServer() public async Task PathBaseHiddenFromServer()
{ {
var builder = new WebHostBuilder() using var host = new HostBuilder()
.UseSetting("TOKEN", "TestToken") .ConfigureWebHost(webHostBuilder =>
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/pathBase")
.UseIISIntegration()
.Configure(app =>
{ {
app.Run(context => Task.FromResult(0)); webHostBuilder
}); .UseSetting("TOKEN", "TestToken")
new TestServer(builder); .UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/pathBase")
.UseIISIntegration()
.Configure(app =>
{
app.Run(context => Task.FromResult(0));
})
.UseTestServer();
})
.Build();
Assert.Equal("http://127.0.0.1:12345", builder.GetSetting(WebHostDefaults.ServerUrlsKey)); host.GetTestServer();
await host.StartAsync();
var configuration = host.Services.GetService<IConfiguration>();
Assert.Equal("http://127.0.0.1:12345", configuration[WebHostDefaults.ServerUrlsKey]);
} }
[Fact] [Fact]
@ -280,21 +354,30 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
{ {
var requestPathBase = string.Empty; var requestPathBase = string.Empty;
var requestPath = string.Empty; var requestPath = string.Empty;
var builder = new WebHostBuilder() using var host = new HostBuilder()
.UseSetting("TOKEN", "TestToken") .ConfigureWebHost(webHostBuilder =>
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/pathbase")
.UseIISIntegration()
.Configure(app =>
{ {
app.Run(context => webHostBuilder
{ .UseSetting("TOKEN", "TestToken")
requestPathBase = context.Request.PathBase.Value; .UseSetting("PORT", "12345")
requestPath = context.Request.Path.Value; .UseSetting("APPL_PATH", "/pathbase")
return Task.FromResult(0); .UseIISIntegration()
}); .Configure(app =>
}); {
var server = new TestServer(builder); app.Run(context =>
{
requestPathBase = context.Request.PathBase.Value;
requestPath = context.Request.Path.Value;
return Task.FromResult(0);
});
})
.UseTestServer();
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var request = new HttpRequestMessage(HttpMethod.Get, "/PathBase/Path"); var request = new HttpRequestMessage(HttpMethod.Get, "/PathBase/Path");
request.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken"); request.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken");
@ -309,24 +392,33 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
{ {
var assertsExecuted = false; var assertsExecuted = false;
var builder = new WebHostBuilder() using var host = new HostBuilder()
.UseSetting("TOKEN", "TestToken") .ConfigureWebHost(webHostBuilder =>
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/")
.UseIISIntegration()
.Configure(app =>
{ {
app.Run(async context => webHostBuilder
{ .UseSetting("TOKEN", "TestToken")
var auth = context.RequestServices.GetRequiredService<IAuthenticationSchemeProvider>(); .UseSetting("PORT", "12345")
var windows = await auth.GetSchemeAsync(IISDefaults.AuthenticationScheme); .UseSetting("APPL_PATH", "/")
Assert.NotNull(windows); .UseIISIntegration()
Assert.Null(windows.DisplayName); .Configure(app =>
Assert.Equal("Microsoft.AspNetCore.Server.IISIntegration.AuthenticationHandler", windows.HandlerType.FullName); {
assertsExecuted = true; app.Run(async context =>
}); {
}); var auth = context.RequestServices.GetRequiredService<IAuthenticationSchemeProvider>();
var server = new TestServer(builder); var windows = await auth.GetSchemeAsync(IISDefaults.AuthenticationScheme);
Assert.NotNull(windows);
Assert.Null(windows.DisplayName);
Assert.Equal("Microsoft.AspNetCore.Server.IISIntegration.AuthenticationHandler", windows.HandlerType.FullName);
assertsExecuted = true;
});
})
.UseTestServer();
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var req = new HttpRequestMessage(HttpMethod.Get, ""); var req = new HttpRequestMessage(HttpMethod.Get, "");
req.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken"); req.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken");
@ -342,11 +434,36 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
{ {
var assertsExecuted = false; var assertsExecuted = false;
var builder = new WebHostBuilder() using var host = new HostBuilder()
.UseSetting("TOKEN", "TestToken") .ConfigureWebHost(webHostBuilder =>
.UseSetting("PORT", "12345") {
.UseSetting("APPL_PATH", "/") webHostBuilder
.UseIISIntegration() .UseSetting("TOKEN", "TestToken")
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/")
.UseIISIntegration()
.Configure(app =>
{
app.Run(async context =>
{
var auth = context.RequestServices.GetService<IAuthenticationSchemeProvider>();
Assert.NotNull(auth);
var windowsAuth = await auth.GetSchemeAsync(IISDefaults.AuthenticationScheme);
if (forward)
{
Assert.NotNull(windowsAuth);
Assert.Null(windowsAuth.DisplayName);
Assert.Equal("AuthenticationHandler", windowsAuth.HandlerType.Name);
}
else
{
Assert.Null(windowsAuth);
}
assertsExecuted = true;
});
})
.UseTestServer();
})
.ConfigureServices(services => .ConfigureServices(services =>
{ {
services.Configure<IISOptions>(options => services.Configure<IISOptions>(options =>
@ -354,27 +471,11 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
options.ForwardWindowsAuthentication = forward; options.ForwardWindowsAuthentication = forward;
}); });
}) })
.Configure(app => .Build();
{
app.Run(async context => var server = host.GetTestServer();
{
var auth = context.RequestServices.GetService<IAuthenticationSchemeProvider>(); await host.StartAsync();
Assert.NotNull(auth);
var windowsAuth = await auth.GetSchemeAsync(IISDefaults.AuthenticationScheme);
if (forward)
{
Assert.NotNull(windowsAuth);
Assert.Null(windowsAuth.DisplayName);
Assert.Equal("AuthenticationHandler", windowsAuth.HandlerType.Name);
}
else
{
Assert.Null(windowsAuth);
}
assertsExecuted = true;
});
});
var server = new TestServer(builder);
var req = new HttpRequestMessage(HttpMethod.Get, ""); var req = new HttpRequestMessage(HttpMethod.Get, "");
req.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken"); req.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken");
@ -390,11 +491,24 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
{ {
var assertsExecuted = false; var assertsExecuted = false;
var builder = new WebHostBuilder() using var host = new HostBuilder()
.UseSetting("TOKEN", "TestToken") .ConfigureWebHost(webHostBuilder =>
.UseSetting("PORT", "12345") {
.UseSetting("APPL_PATH", "/") webHostBuilder
.UseIISIntegration() .UseSetting("TOKEN", "TestToken")
.UseSetting("PORT", "12345")
.UseSetting("APPL_PATH", "/")
.UseIISIntegration()
.Configure(app =>
{
app.Run(context =>
{
assertsExecuted = true;
return Task.FromResult(0);
});
})
.UseTestServer();
})
.ConfigureServices(services => .ConfigureServices(services =>
{ {
services.Configure<IISOptions>(options => services.Configure<IISOptions>(options =>
@ -402,15 +516,11 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
options.ForwardWindowsAuthentication = forward; options.ForwardWindowsAuthentication = forward;
}); });
}) })
.Configure(app => .Build();
{
app.Run(context => var server = host.GetTestServer();
{
assertsExecuted = true; await host.StartAsync();
return Task.FromResult(0);
});
});
var server = new TestServer(builder);
var req = new HttpRequestMessage(HttpMethod.Get, ""); var req = new HttpRequestMessage(HttpMethod.Get, "");
req.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken"); req.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken");