4.2. Detecting the Operating System Version
Because a trigger-start service doesn't run under
older versions of Windows, you want to be sure that users install it as
a trigger-start service under only the right version of the operating
system. The service will still work with other versions of Windows, but
not as a trigger-start service. Consequently, you can still install it
with older versions, but you can't configure it for a trigger-start
service. To accommodate this need, the example handles the AfterInstall event. Use these steps to create the event handler:
Select the TriggerStartServiceInstall entry in the TriggerStartServiceInstaller Design View window.
In the Properties window, click Events. You'll see a list of events associated with the TriggerStartServiceInstall object.
Double-click the AfterInstall event entry. Visual Studio automatically creates the required event handler for you.
Now that you have the event handler in place, it's time to add some code to it. Listing 2 shows the event handler code.
Example 2. Checking the operating system version
private void TriggerStartServiceInstaller_AfterInstall( object sender, InstallEventArgs e) { // Check the operating sytem version. if (Environment.OSVersion.Version >= new Version(6, 1))
// Configure the trigger for the service. this.ConfigurePortTrigger(TriggerStartServiceInstall.ServiceName); else
// This is the wrong version of the operating system. throw new NotSupportedException("Wrong OS Version"); }
|
The code begins by checking the operating system version supplied by Environment.OSVersion.Version against a Version
object that has a major version number of 6 and a minor version number
of 1, which works for either Windows 7 or Windows Server 2008 R2 (but
not the original version of Windows Server 2008). You can see a list of
version numbers at http://msdn.microsoft.com/library/ms724832.aspx.
When the operating system version is new enough, the code calls ConfigurePortTrigger(), which is described in Listing 1, to create the required trigger-start service changes for the service. The TriggerStartServiceInstall.ServiceName property supplies the required service name. Otherwise, the code raises a NotSupportedException() exception and exits.
4.3. Defining the Service Code
The emphasis of this example is on creating a
trigger-start service, so the service code isn't much to look at. In
this case, the service code outputs one of two messages to the event
log to describe the state of Port 23. The service code appears in TriggerStartService.CS. Listing 3 shows the code used in this case.
Example 3. Adding event log entries to monitor the port
protected override void OnStart(string[] args) { // Add a starting event log entry. WriteEventLog("Telnet Port 23 Opened"); }
protected override void OnStop() { // Add a stopping event log entry. WriteEventLog("Telnet Port 23 Closed"); }
private void WriteEventLog(String Message) { // Use the System event log. EventLog.Log = "System";
// Send a message to the event log. EventLog.WriteEntry(Message, EventLogEntryType.Information); }
|
The code needs to override two event handlers: OnStart() and OnStop(). In both cases, the event handlers call WriteEventLog() with the message to place in the event log.
The default configuration for writing event
log entries is to use the Application log. However, service entries
normally appear in the System log, so the WriteEventLog() method begins by setting the EventLog.Log property to System. The code then calls EventLog.WriteEntry() with the message and the type of entry to create, which is EventLogEntryType.Information in this case.