public async Task InitializeAsync() { _webHost = WebHost .CreateDefaultBuilder(Array.Empty <string>()) .ConfigureServices(services => { services.AddSingleton <IStartupFilter>(new SetUserStartupFilter(ctx => _getUser(ctx))); }) .Configure(app => { app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapGet("/", ctx => { if (ctx.User.Identity != null && ctx.User.Identity.IsAuthenticated) { ctx.Response.StatusCode = (int)HttpStatusCode.OK; } else { ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized; } return(Task.CompletedTask); }); }); }) .UseKestrel() .UseUrls("http://127.0.0.1:0") .Build(); await _webHost.StartAsync(); var serverPort = _webHost.GetServerUris().First().Port; _client = new HttpClient { BaseAddress = new Uri($"http://127.0.0.1:{serverPort}") }; }