简体   繁体   English

使用IIS Express几秒钟后站点停止工作

[英]Site stops working after few seconds with IIS Express

I have to start the IIS Express from a WPF Application. 我必须从WPF应用程序启动IIS Express。 I can start IISExpress, and browser on the website for a few seconds. 我可以启动IISExpress,并在网站上浏览几秒钟。 Then when I click any link it doesn't load. 然后,当我点击任何链接时,它不会加载。 Then If I close the WPF Application the webpage responds immediately. 然后,如果我关闭WPF应用程序,网页会立即响应。 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? 任何猜测为什么网站只运行几秒钟,并在关闭启动iisexpress的应用程序后再次工作?

Edit 编辑

I notice that when I build in "Debug" it happens, but when I build in "Release" the problem doesn't occur. 我注意到,当我构建“Debug”时,它会发生,但是当我构建“Release”时,问题不会发生。

I solved my problem by creating an IIS Launcher(console application). 我通过创建IIS启动器(控制台应用程序)解决了我的问题。 I pass some arguments to this application, it starts IISExpress then dies. 我将一些参数传递给这个应用程序,它启动IISExpress然后死掉。 Now in my WPF application instead of creating a process for IISExpress, I create a process for my launcher. 现在在我的WPF应用程序中,而不是为IISExpress创建进程,我为我的启动器创建了一个进程。 Also this console application returns the process id to WPF through exit code. 此控制台应用程序还通过退出代码将进程ID返回给WPF。

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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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