11

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.

Brettski
  • 19,351
  • 15
  • 74
  • 97
David
  • 958
  • 2
  • 12
  • 29

6 Answers6

7

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.

Carlos Aguilar Mares
  • 13,411
  • 2
  • 39
  • 36
  • I know this is an old thread but I am trying to do this same thing yet I cannot get it to work with the 32-bit iisexpress.exe found in C:\program files (x86)\IIS Express. Only the 64-bit iisexpress.exe works for some reason. – Dan Aug 18 '15 at 16:23
3

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.

cyrotello
  • 757
  • 9
  • 26
Gautam
  • 1,728
  • 8
  • 32
  • 67
2

Try this. We had the same situation and this worked. This may help you.

This is with IIS Express 7.5 which does not need Administrator rights.

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
});
cyrotello
  • 757
  • 9
  • 26
  • Thanks for taking the time however as I mentioned in my question, this will not work when trying to use port 80 as IIS Express must be run as Administrator when serving external requests on port 80. – David Feb 19 '11 at 04:26
0

I know this is an old post, but have you considered Microsoft's SRVANY Service Wrapper?

It installs and runs as a Windows Service (under any credentials), and launches your process in a windowsless process.

Anything you can run from a command line (or Start/Run window), you can run as a service via SRVANY:

Nice write-up at: http://www.tacktech.com/display.cfm?ttid=197

Mark Ward
  • 11
  • 1
0

There was a similar question (running iisexpress without console window) on iis.net forums. Please take a look at http://forums.iis.net/p/1175262/1970513.aspx#1970513

vikomall
  • 17,379
  • 6
  • 49
  • 39
  • Thanks for the link and vote. I think more people are making mountains out of molehills when it comes to hiding the console window but then again they are only using batch files! The problem I am having is running it as a process started within a windows service. I can't get it to run let alone without a console window. – David Feb 08 '11 at 20:49
  • David.. i think u need to check on "Session 0 Isolation".. per your this comment I am taking it as that you are trying to start IIS express from inside a windows service... it will start the IIS but that will be in session 0... to check that when you run ur service go to task manager->processes-> click on show processes from all users. There you can see IIS running under your name but with session id 0... even I am looking for a solution for the same. if you get one.. please let me also know – Gautam Aug 13 '12 at 14:40
-1

It can't be done if you also want to use Port 80.

David
  • 958
  • 2
  • 12
  • 29
  • See this URL on how to configure HTTP.sys to allow listening for requests on Port 80 with IIS Express. http://learn.iis.net/page.aspx/1005/handling-url-binding-failures-in-iis-express/ – Peter Bernier Jun 28 '12 at 13:44