Click here to Skip to main content
15,883,901 members
Articles / Programming Languages / C#

RWWinService - A Simplified .NET 3.5 branch of XYWinService

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
2 Mar 2010CPOL2 min read 13.4K   367   14  
A branch/fork of Xiangyang Liu's XYNTService/XYWinService

Introduction

This project is my personal branch of Xiangyang Liu's XYWinService.

In this version, I leverage my own SimpleService project to add features and solve a few minor annoyances.

New Features

  • Upgraded the solution to VS2008
  • Simplified configuration using app.config
  • Improved install/uninstall service support
  • Console mode - for simplified debugging
  • Logging to application event log
  • More command line options

Configuration

I use RWWinService to run my fossil source repositories in the background. The following configuration is how I make that happen:

XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="Process" 
      type="System.Configuration.IgnoreSectionHandler" />
  </configSections>
  <appSettings>
    <add key="CheckProcessSeconds" value="30"/>
    <add key="PauseStartMs" value="1000"/>
    <add key="PauseEndMs" value="1000"/>
  </appSettings>
  <Process>
    <FilePath>c:\rev\bin\fossil.exe</FilePath>
    <Arguments>server --port 8080 c:\rev\fossil\fossil.fossil</Arguments>
    <WorkingDir>c:\rev\bin</WorkingDir>
    <Restart>Yes</Restart>
  </Process>
  <Process>
    <FilePath>c:\rev\bin\fossil.exe</FilePath>
    <Arguments>server --port 8081 c:\rev\fossil\rwwinservice.fossil</Arguments>
    <WorkingDir>c:\rev\bin</WorkingDir>
    <Restart>Yes</Restart>
  </Process>
</configuration>

There is a little bit of magic going on in this config file. Normally, the ConfigurationManager class expects to see only the appSettings element in the configuration, and it will throw an exception if unexpected elements are present. The configSections element lets you tell the ConfigurationManager class to ignore elements named Process and their children via the System.Configuration.IgnoreSectionHandler. Once that is done, you can parse the Process elements yourself in code. Alternatively, you could write your own SectionHandler, but I leave that to you. This gives us a simple, single configuration file.

The three appSettings keys are pretty much self-explanatory, and the Process elements are exactly the same as in XYWinService. I deprecated several configuration items that were available in XYWinService, most notably the RunAs element. This project assumes that the service will run as ServiceAccount.LocalSystem, and there is no configuration option that exposes this.

The Manifest

The manifest is the glue that makes it all come together. One line in particular configures the service to invoke the Vista/Windows7 UAC.

XML
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Managing the Service

The RWWinService leverages the SimpleService framework, so the following command line options are available:

PS C:\rev\bin> RWWinService -help
= usage:
=   RWWinService -install   == install service
=   RWWinService -uninstall == uninstall service
=   RWWinService -start     == start service
=   RWWinService -stop      == stop service
=   RWWinService -status    == get the current status of the service
=   RWWinService -console   == run in console mode
=   RWWinService -help      == show this help message

Using these command-line switches, you can install and uninstall the service without using InstallUtil.exe. Also, you can start and stop the service once it is installed. The -status switch will tell you if the service is installed, started, or stopped.

Conclusions

I've been using this service for several months now without any problems. I think you will find the command-line interface saves you time. I am very much indebted to Xiangyang Liu for his original contribution; functionally, this branch of XYWinService provides the same basic feature set, only simplified and refactored. Please don't hesitate to notify me of bugs or oversights. Enjoy!

History

  • March 2010 - Initial submission

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Under Armour
United States United States
I'm your huckleberry.

Check out more beards here.


Comments and Discussions

 
-- There are no messages in this forum --