Problems Starting IIS Express via C #

1

I need to start IIS Express via C # with WPF. I can even upload the site and browse, but for only a few seconds. Soon the site stops responding to requests, and only returns to respond when I close the application.

private void WinPrincipal_Loaded(object sender, RoutedEventArgs e)
{
  IniciarSite();
}

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

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

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

Initially I thought it was because I was using some non-common ports like 9092, 8082. So I started testing with 9090, but it also happens, stops responding and only comes back when I close the application.

I also noticed that when I compile in "debug" the problem happens, however when compiling in "release" it works normally.

    
asked by anonymous 02.09.2016 / 22:20

1 answer

1

I solved my problem by creating a Console Application that launches IISExpress. I pass the arguments to this application, it takes care of starting the IIS process then closes, returning the process code created through the exit code

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

        string exeIIS = args[0];
        string caminhoFisico = args[1];
        string porta = args[2];

        string argumentos = CriarArgumentosInicializacaoIISExpress(caminhoFisico, porta);
        int processoID = LancarIISExpress(argumentos, exeIIS);

        Environment.Exit(processoID);
    }

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

        return process.Id;
    }

    private static string CriarArgumentosInicializacaoIISExpress(string caminhoFisico, string porta)
    {
        var argumentos = new StringBuilder();
        argumentos.Append(@"/path:" + caminhoFisico);
        argumentos.Append(@" /Port:" + porta);
        return argumentos.ToString();
    }
}
    
05.09.2016 / 16:34