简体   繁体   中英

Site stops working after few seconds with IIS Express

I have to start the IIS Express from a WPF Application. I can start IISExpress, and browser on the website for a few seconds. Then when I click any link it doesn't load. Then If I close the WPF Application the webpage responds immediately. My code:

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
   StartSite();
}

public void StartSite()
{
    string path = @"C:\Program Files\IIS Express\iisexpress.exe";
    string args = @"/path:C:\Sites /port:9090 /systray:true";

   if (!File.Exists(path))
      throw new FileNotFoundException();

   var process = Process.Start(new ProcessStartInfo()
   {
     FileName = path,
     Arguments = args,
     RedirectStandardOutput = true,
     UseShellExecute = false,
     CreateNoWindow = true
   });
}

Any guesses why the website works for just few seconds and will work again after closing the application that launched iisexpress?

Edit

I notice that when I build in "Debug" it happens, but when I build in "Release" the problem doesn't occur.

I solved my problem by creating an IIS Launcher(console application). I pass some arguments to this application, it starts IISExpress then dies. Now in my WPF application instead of creating a process for IISExpress, I create a process for my launcher. Also this console application returns the process id to WPF through exit code.

class Program
{
    static void Main(string[] args)
    {
        if (args == null)
            Environment.Exit(-1);

        string exeIIS = args[0];
        string websitePath = args[1];
        string port = args[2];

        string argsIIS = MakeIISExpressArgs(websitePath, port);
        int processID = LaunchIISExpress(argsIIS, exeIIS);

        Environment.Exit(processID);
    }

    private static int LaunchIISExpress(string argsIIS, string pathExeIIS)
    {
        var process = Process.Start(new ProcessStartInfo()
        {
            FileName = pathExeIIS,
            Arguments = argsIIS,
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        });

        return process.Id;
    }

    private static string MakeIISExpressArgs(string websitePath, string port)
    {
        var argsIIS = new StringBuilder();
        argsIIS.Append(@"/path:" + websitePath);
        argsIIS.Append(@" /Port:" + port);
        return argsIIS.ToString();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM