Tuesday, March 27, 2012

Running a Standalone Web Application as a Windows Service

In my last post, I wrote about how you can run your Real Studio web applications as a daemon.  Windows doesn't have daemons, but it does have an equivalent feature called Services.  Like a daemon, a service is a background process.

As luck would have it, web applications are actually services under the hood so they work just fine as Windows services.  The trick is how to install it as a service on Windows.

The magic command is called "sc" for service control.

Here's a quick example:
  1. Create a simple web project and build it as a standalone web application (I use port 8100).  Note the path of the resulting executable.
  2. Start the Windows command line with administrator privileges.
  3. Enter the sc command to install the service.  Do not leave out the spaces after each = in this command:
    sc create RSWebSvc type= own start= auto binpath= c:\Path\To\Exe\WebApp.exe
  4. You should see: [SC] CreateService SUCCESS
  5. Now open up Control Panel and go to the Services Manager.  It is located in Administrative Tools and is called Services.
  6. Find the service you just created, RSWebSvc.  Click on it and select Start.
  7. Your web application is now running as a service.  To test it, navigate to the URL in your browser:
  8. To stop the service, click on it in Service Manager and click Stop.


Bob Boice said...

Excellent! Thanks, Paul.

Unknown said...

Be aware that Try/Catch blocks don't work as expected when run in a window service. See Case 16636.

Unknown said...

Web is like a console application right? Is this possible with console application template?
So i dont need to use a Service Application template?