public void FixtureTearDown()
 {
     Timer.Time(
         "IIS Express stop",
         () =>
         {
             _iew.Dispose();
             _iew = null;
         });
 }
        public void FixtureSetUp()
        {
            Timer.Time(
                "IIS Express start",
                () =>
                {
                    _iew = IisExpressWrapper.Start(Settings.WebProjectPath);

                    // Make first request to ensure app is started
                    var wc = new WebClient();
                    wc.DownloadString(_iew.RootUrl.TrimEnd('/') + TestConstants.TestPath);
                });
        }
        public static IisExpressWrapper Start(string appPath, TraceLevel traceLevel = TraceLevel.none)
        {
            int port =
                CassiniDev.CassiniNetworkUtils.GetAvailablePort(
                    8000,
                    8999,
                    System.Net.IPAddress.Loopback,
                    includeIdlePorts: false);

            if (port == 0)
            {
                throw new ApplicationException("Couldn't get available port for new IISExpress instance.");
            }

            var startInfo = new System.Diagnostics.ProcessStartInfo()
            {
                Arguments = String.Format(@"/path:""{0}"" /systray:true /clr:v4.0 /trace:{1} /port:{2}", appPath, traceLevel.ToString(), port),
                FileName = @"C:\Program Files (x86)\IIS Express\iisexpress.exe",
                WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
                RedirectStandardError = true,
                //RedirectStandardInput = true,
                RedirectStandardOutput = true,
                UseShellExecute = false,
            };

            var process = System.Diagnostics.Process.Start(startInfo);

            var iew = new IisExpressWrapper(appPath, port, process);

            process.OutputDataReceived += iew.process_OutputDataReceived;
            process.ErrorDataReceived += iew.process_ErrorDataReceived;

            process.BeginErrorReadLine();
            process.BeginOutputReadLine();

            // Hack: wait a little for IIS Express to start.
            iew._iisStartedEvent.Wait(TimeSpan.FromMilliseconds(2000));

            return iew;
        }