Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run IIS Express as a process started via a Windows Service

Tags:

iis-express

I am trying to distribute IIS Express with my application. IIS Express will serve external web requests on port 80.

I have no problems running IIS Express as well as serving external requests however Microsoft in their infinite wisdom decided to run IIS Express from a console window as well as a system tray item. You can disable the tray item by a command line argument but not the console window.

I want to run IIS Express without the console window being displayed. I also want to run IIS Express from a windows service.

Running the following code from within my application does exactly what I want:

    Directory.SetCurrentDirectory(string.Format("{0}\\IIS Express", iisProgramDirectory));
    process.EnableRaisingEvents = true;
    //process.Exited += new EventHandler(process_Exited);
    process.StartInfo.FileName = "iisexpress.exe";
    process.StartInfo.Arguments = string.Format("\"/config:{0}webservice\\config\\applicationhost.config\"", dataDirectory);
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    //process.StartInfo.UserName = "Administrator";
    //System.Security.SecureString securePwd = new System.Security.SecureString();
    //string password = "**********";
    //char[] pword = password.ToCharArray();
    //for (int i = 0; i < pword.Length; i++)
    //{
    //  securePwd.AppendChar(pword[i]);
    //}
    //process.StartInfo.Password = securePwd;
    process.Start();

Obviously I am running as Administrator. IIS Express apparently needs to run with Administrator privileges to serve external requests as well as listen on port 80.

My windows service runs under the Windows Service account which I believe has full privileges but the IIS Express process just gracefully exits with an error code of 0 when I try to run it from the windows service.

I have tried a number of scenarios (as you can see from the code snippet) but there seems to be no way I can get IIS Express running using my windows service AND hide the darn console window.

Any suggestions will be appreciated.

like image 965
David Avatar asked Feb 08 '11 09:02

David


2 Answers

The answer like: string IIS_EXPRESS = @"C:\Program Files\IIS Express\iisexpress.exe";

    StringBuilder arguments = new StringBuilder();
    arguments.Append(@"/path:");
    arguments.Append(@"C:\Inetpub\wwwroot\ClientSyncService");
    arguments.Append(@" /Port:2000");
    Process process = Process.Start(new ProcessStartInfo()
        {
            FileName = IIS_EXPRESS,
            Arguments = arguments.ToString(),
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        });

Should work, however the trick is that you need to grant ACLs for the the identity of the service so that it can take ownership of port 80. In other words, during your setup program (assuming you have an MSI that will run elevated), make it run a command line like: netsh http add urlacl url=http://WhateverMachineName:80/ user=everyone

where you can limit "everyone" to instead just a specific account under which your service will be running. When you do that, then IIS express should be able to start just fine without requiring administrator privileges.

like image 65
Carlos Aguilar Mares Avatar answered Oct 20 '22 21:10

Carlos Aguilar Mares


To run IIS 7.5 as administrator, just change your code slightly to:

Process process = Process.Start(new ProcessStartInfo()
{
    FileName = IIS_EXPRESS,
    Arguments = arguments.ToString(),
    RedirectStandardOutput = true,
    UseShellExecute = true,
    CreateNoWindow = true,
    Verb = "runas"
});

This will also enable you to run your site on port 80.

like image 3
Gautam Avatar answered Oct 20 '22 19:10

Gautam