Problems starting IIS Express via c#

Asked

Viewed 74 times

1

I need to start IIS Express via C# with WPF. I can even upload the site and navigate, but only for a few seconds. Logo the site stops responding to requests, and only return reply 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 unusual doors like 9092, 8082. Then I started testing with the 9090, but it also happens, stops responding and only comes back when you close the application.

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

1 answer

1


I solved my problem by creating a Console Application that makes the release of Iisexpress. I pass the arguments to this application, it takes charge of starting the IIS process then closes by returning the process code created through the output code(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();
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.