diff --git a/test/Microsoft.AspnetCore.Identity.Service.FunctionalTests/Infrastructure/CredentialsServerBuilder.cs b/test/Microsoft.AspnetCore.Identity.Service.FunctionalTests/Infrastructure/CredentialsServerBuilder.cs index ac79f43e4e..520041bed0 100644 --- a/test/Microsoft.AspnetCore.Identity.Service.FunctionalTests/Infrastructure/CredentialsServerBuilder.cs +++ b/test/Microsoft.AspnetCore.Identity.Service.FunctionalTests/Infrastructure/CredentialsServerBuilder.cs @@ -12,6 +12,7 @@ using Microsoft.AspNetCore.Identity.Service; using Microsoft.AspNetCore.Identity.Service.IntegratedWebClient; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.TestHost; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -23,25 +24,24 @@ namespace Microsoft.AspnetCore.Identity.Service.FunctionalTests { private readonly DelegatingHandler _loopBackHandler = new LoopBackHandler(); - public CredentialsServerBuilder() + public CredentialsServerBuilder(string[] args = null) { - Server = new MvcWebApplicationBuilder() - .UseSolutionRelativeContentRoot(@"./test/WebSites/Identity.OpenIdConnect.WebSite") - .UseApplicationAssemblies(); + Server = Program.CreateWebHostBuilder(args ?? Array.Empty()) + .UseSolutionRelativeContentRoot(@"./test/WebSites/Identity.OpenIdConnect.WebSite"); } public CredentialsServerBuilder ConfigureReferenceData(Action action) { var referenceData = new ReferenceData(); action(referenceData); - Server.ConfigureBeforeStartup(s => s.TryAddSingleton(referenceData)); + Server.ConfigureServices(s => s.TryAddSingleton(referenceData)); return this; } public CredentialsServerBuilder ConfigureInMemoryEntityFrameworkStorage(string dbName = "test") { - Server.ConfigureBeforeStartup(services => + Server.ConfigureServices(services => { services.TryAddEnumerable(ServiceDescriptor.Transient()); services.AddDbContext(options => @@ -53,13 +53,13 @@ namespace Microsoft.AspnetCore.Identity.Service.FunctionalTests public CredentialsServerBuilder ConfigureMvcAutomaticSignIn() { - Server.ConfigureBeforeStartup(s => s.Configure(o => o.Filters.Add(new AutoSignInFilter()))); + Server.ConfigureServices(s => s.Configure(o => o.Filters.Add(new AutoSignInFilter()))); return this; } public CredentialsServerBuilder ConfigureOpenIdConnectClient(Action action) { - Server.ConfigureAfterStartup(services => + Server.ConfigureServices(services => { services.Configure(OpenIdConnectDefaults.AuthenticationScheme, options => { @@ -76,7 +76,7 @@ namespace Microsoft.AspnetCore.Identity.Service.FunctionalTests public CredentialsServerBuilder ConfigureIntegratedClient(string clientId) { - Server.ConfigureAfterStartup(services => + Server.ConfigureTestServices(services => { services.Configure(options => options.ClientId = clientId); }); @@ -86,7 +86,7 @@ namespace Microsoft.AspnetCore.Identity.Service.FunctionalTests public CredentialsServerBuilder EnsureDeveloperCertificate() { - Server.ConfigureBeforeStartup(services => services.Configure( + Server.ConfigureServices(services => services.Configure( o => o.SigningKeys.Add( new SigningCredentials( new X509SecurityKey(new X509Certificate2("./test-cert.pfx", "test")), "RS256")))); @@ -94,13 +94,13 @@ namespace Microsoft.AspnetCore.Identity.Service.FunctionalTests return this; } - public MvcWebApplicationBuilder Server { get; } + public IWebHostBuilder Server { get; } public HttpClient Build() { Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development"); - var host = Server.Build(); + var host = new TestServer(Server); host.BaseAddress = new Uri("https://localhost"); var clientHandler = host.CreateHandler(); diff --git a/test/Microsoft.AspnetCore.Identity.Service.FunctionalTests/Infrastructure/EntityFrameworkSeedReferenceData.cs b/test/Microsoft.AspnetCore.Identity.Service.FunctionalTests/Infrastructure/EntityFrameworkSeedReferenceData.cs index 457bceedc6..c878a7f50c 100644 --- a/test/Microsoft.AspnetCore.Identity.Service.FunctionalTests/Infrastructure/EntityFrameworkSeedReferenceData.cs +++ b/test/Microsoft.AspnetCore.Identity.Service.FunctionalTests/Infrastructure/EntityFrameworkSeedReferenceData.cs @@ -7,17 +7,22 @@ using Identity.OpenIdConnect.WebSite.Identity.Models; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspnetCore.Identity.Service.FunctionalTests { public class EntityFrameworkSeedReferenceData : IStartupFilter { public EntityFrameworkSeedReferenceData( - IdentityServiceDbContext dbContext, - UserManager userManager, + IServiceScopeFactory factory, ReferenceData seedData) { - SeedContext(dbContext, userManager, seedData); + using (var scope = factory.CreateScope()) + { + var dbContext = scope.ServiceProvider.GetRequiredService(); + var userManager = scope.ServiceProvider.GetRequiredService>(); + SeedContext(dbContext, userManager, seedData); + } } public Action Configure(Action next) diff --git a/test/WebSites/Identity.OpenIdConnect.WebSite/Program.cs b/test/WebSites/Identity.OpenIdConnect.WebSite/Program.cs index 6129dffd88..50d6d0c6fc 100644 --- a/test/WebSites/Identity.OpenIdConnect.WebSite/Program.cs +++ b/test/WebSites/Identity.OpenIdConnect.WebSite/Program.cs @@ -11,9 +11,9 @@ namespace Identity.OpenIdConnect.WebSite { public class Program { - public static void Main(string[] args) => BuildWebHost(args).Run(); + public static void Main(string[] args) => CreateWebHostBuilder(args).Build().Run(); - public static IWebHost BuildWebHost(string[] args) + public static IWebHostBuilder CreateWebHostBuilder(string[] args) { var builder = new WebHostBuilder() .UseKestrel() @@ -22,7 +22,7 @@ namespace Identity.OpenIdConnect.WebSite ConfigureHost(builder, args); - return builder.Build(); + return builder; } public static IWebHostBuilder ConfigureHost(IWebHostBuilder builder, string[] args)