private async Task <IisFixture> StartIis(IisAppType appType) { var fixture = new IisFixture { ShutdownPath = "/shutdown" }; try { fixture.TryStartIis(this, appType); } catch (Exception) { fixture.Dispose(); throw; } // Send a request to initialize the app using var httpClient = new HttpClient(); await httpClient.GetAsync($"http://localhost:{fixture.HttpPort}/"); return(fixture); }
public (Process Process, string ConfigFile) StartIISExpress(MockTracerAgent agent, int iisPort, IisAppType appType) { var iisExpress = EnvironmentHelper.GetIisExpressPath(); var appPool = appType switch { IisAppType.AspNetClassic => "Clr4ClassicAppPool", IisAppType.AspNetIntegrated => "Clr4IntegratedAppPool", IisAppType.AspNetCoreInProcess => "UnmanagedClassicAppPool", IisAppType.AspNetCoreOutOfProcess => "UnmanagedClassicAppPool", _ => throw new InvalidOperationException($"Unknown {nameof(IisAppType)} '{appType}'"), }; var appPath = appType switch { IisAppType.AspNetClassic => EnvironmentHelper.GetSampleProjectDirectory(), IisAppType.AspNetIntegrated => EnvironmentHelper.GetSampleProjectDirectory(), IisAppType.AspNetCoreInProcess => EnvironmentHelper.GetSampleApplicationOutputDirectory(), IisAppType.AspNetCoreOutOfProcess => EnvironmentHelper.GetSampleApplicationOutputDirectory(), _ => throw new InvalidOperationException($"Unknown {nameof(IisAppType)} '{appType}'"), }; var configTemplate = File.ReadAllText("applicationHost.config"); var newConfig = Path.GetTempFileName(); configTemplate = configTemplate .Replace("[PATH]", appPath) .Replace("[PORT]", iisPort.ToString()) .Replace("[POOL]", appPool); var isAspNetCore = appType == IisAppType.AspNetCoreInProcess || appType == IisAppType.AspNetCoreOutOfProcess; if (isAspNetCore) { var hostingModel = appType == IisAppType.AspNetCoreInProcess ? "inprocess" : "outofprocess"; configTemplate = configTemplate .Replace("[DOTNET]", EnvironmentHelper.GetDotnetExe()) .Replace("[RELATIVE_SAMPLE_PATH]", $".\\{EnvironmentHelper.GetSampleApplicationFileName()}") .Replace("[HOSTING_MODEL]", hostingModel); } File.WriteAllText(newConfig, configTemplate); var args = new[] { "/site:sample", $"/config:{newConfig}", "/systray:false", "/trace:info" }; Output.WriteLine($"[webserver] starting {iisExpress} {string.Join(" ", args)}"); var process = ProfilerHelper.StartProcessWithProfiler( iisExpress, EnvironmentHelper, agent, arguments: string.Join(" ", args), redirectStandardInput: true, processToProfile: appType == IisAppType.AspNetCoreOutOfProcess ? "dotnet.exe" : iisExpress); var wh = new EventWaitHandle(false, EventResetMode.AutoReset); Task.Run(() => { string line; while ((line = process.StandardOutput.ReadLine()) != null) { Output.WriteLine($"[webserver][stdout] {line}"); if (line.Contains("IIS Express is running")) { wh.Set(); } } }); Task.Run(() => { string line; while ((line = process.StandardError.ReadLine()) != null) { Output.WriteLine($"[webserver][stderr] {line}"); } }); wh.WaitOne(5000); // Wait for iis express to finish starting up var retries = 5; while (true) { var usedPorts = IPGlobalProperties.GetIPGlobalProperties() .GetActiveTcpListeners() .Select(ipEndPoint => ipEndPoint.Port); if (usedPorts.Contains(iisPort)) { break; } retries--; if (retries == 0) { throw new Exception("Gave up waiting for IIS Express."); } Thread.Sleep(1500); } return(process, newConfig); }