
Adding Features to the Service
In the next section, you add a custom event log to your Windows service. Event logs are not associated in any way with Windows services. Here the EventLog component is used as an example of the type of component you could add to a Windows service. For more information on custom event logs, see How to: Create and Remove Custom Event Logs.
To add custom event log functionality to your service
-
In Solution Explorer, right-click Service1.vb, Service1.cs, or Service1.jsl and select View Designer.
-
From the Components tab of the Toolbox, drag an EventLog component to the designer.
-
In Solution Explorer, right-click Service1.vb, Service1.cs, or Service1.jsl and select View Code.
-
Edit the constructor to define a custom event log.
' To access the constructor in Visual Basic, select New from the
' method name drop-down list.
Public Sub New()
MyBase.New()
InitializeComponent()
If Not System.Diagnostics.EventLog.SourceExists("MySource") Then
System.Diagnostics.EventLog.CreateEventSource("MySource", _
"MyNewLog")
End If
EventLog1.Source = "MySource"
EventLog1.Log = "MyNewLog"
End Sub
public MyNewService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource","MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}
public MyNewService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource", "MyNewLog");
}
eventLog1.set_Source("MySource");
eventLog1.set_Log("MyNewLog");
}
To define what happens when the service starts
To define what happens when the service is stopped
-
In the Code Editor, select the OnStop procedure from the Method Name drop-down list, which was automatically overridden when you created the project. Write code to determine what occurs when the service is stopped:
Protected Overrides Sub OnStop()
EventLog1.WriteEntry("In OnStop.")
End Sub
protected override void OnStop()
{
eventLog1.WriteEntry("In onStop.");
}
protected void OnStop()
{
eventLog1.WriteEntry("In onStop.");
}
You can also override the OnPause, OnContinue, and OnShutdown methods to define further processing for your component.
To define other actions for the service
-
For the method you want to handle, override the appropriate method and define what you want to occur.
The following code shows what it looks like if you override the OnContinue method:
Protected Overrides Sub OnContinue()
EventLog1.WriteEntry("In OnContinue.")
End Sub
protected override void OnContinue()
{
eventLog1.WriteEntry("In OnContinue.");
}
protected void OnContinue()
{
eventLog1.WriteEntry("In OnContinue.");
}
Some custom actions need to occur when installing a Windows service, which can be done by the Installer class. Visual Studio can create these installers specifically for a Windows service and add them to your project.
To create the installers for your service
-
In Solution Explorer, right-click Service1.vb, Service1.cs, or Service1.jsl and select View Designer.
-
Click the background of the designer to select the service itself, rather than any of its contents.
-
With the designer in focus, right-click, and then click Add Installer.
By default, a component class containing two installers is added to your project. The component is named ProjectInstaller, and the installers it contains are the installer for your service and the installer for the service's associated process.
-
In Design view for ProjectInstaller, click ServiceInstaller1 or serviceInstaller1.
-
In the Properties window, set the ServiceName property to MyNewService.
-
Set the StartType property to Automatic.
-
In the designer, click ServiceProcessInstaller1 (for a Visual Basic project), or serviceProcessInstaller1 (for a Visual C# or Visual J# project). Set the Account property to LocalService. This will cause the service to be installed and to run on a local service account.
Security Note |
|---|
| The LocalService account acts as a non-privileged user on the local computer, and presents anonymous credentials to any remote server. Use the other accounts with caution, as they run with higher privileges and increase your risk of attacks from malicious code. |
To build your service project
-
In Solution Explorer, right-click to select your project and select Properties from the shortcut menu. The project's Property Designer appears.
-
on the Application page, from the Startup object list, choose MyNewService.
-
Press CTRL+SHIFT+B to build the project.
Now that the project is built, it can be deployed. A setup project will install the compiled project files and run the installers needed to run the Windows service. To create a complete setup project you will need to add the project output, MyNewService.exe, to the setup project and then add a custom action to have MyNewService.exe installed. For more information on setup projects, see Setup Projects. For more information on custom actions, see Walkthrough: Creating a Custom Action.
To create a setup project for your service
-
In Solution Explorer, right-click to select your solution, point to Add, and then click New Project.
-
In the Project Types pane, select the Setup and Deployment Projects folder.
-
In the Templates pane, select Setup Project. Name the project MyServiceSetup. Click OK.
A setup project is added to the solution.
Next you will add the output from the Windows service project, MyNewService.exe, to the setup.
To add MyNewService.exe to the setup project
-
In Solution Explorer, right-click MyServiceSetup, point to Add, then choose Project Output.
The Add Project Output Group dialog box appears.
-
MyNewService is selected in the Project box.
-
From the list box, select Primary Output, and click OK.
A project item for the primary output of MyNewService is added to the setup project.
Now add a custom action to install the MyNewService.exe file.
To add a custom action to the setup project
-
In Solution Explorer, right-click the setup project, point to View, and then click Custom Actions.
The Custom Actions editor appears.
-
In the Custom Actions editor, right-click the Custom Actions node and choose Add Custom Action.
The Select Item in Project dialog box appears.
-
Double-click the Application Folder in the list box to open it, select Primary Output from MyNewService (Active), and click OK.
The primary output is added to all four nodes of the custom actions — Install, Commit, Rollback, and Uninstall.
-
In Solution Explorer, right-click the MyServiceSetup project and click Build.
To install the Windows Service
-
To install MyNewService.exe, right-click the setup project in Solution Explorer and select Install.
-
Follow the steps in the Setup Wizard. Build and save your solution.
To start and stop your service
-
Open the Services Control Manager by doing one of the following:
-
In Windows XP and 2000 Professional, right-click My Computer on the desktop, then click Manage. In the Computer Management console, expand the Services and Applications node.
- or -
-
In Windows Server 2003 and Windows 2000 Server, click Start, point to Programs, click Administrative Tools, and then click Services.
Note |
|---|
| In Windows NT version 4.0, you can open this dialog box from Control Panel. |
You should now see MyNewService listed in the Services section of the window.
-
Select your service in the list, right-click it, and then click Start.
-
Right-click the service, and then click Stop.
To verify the event log output of your service
-
Open Server Explorer and access the Event Logs node. For more information, see How to: Work with Event Logs in Server Explorer.
Note |
|---|
| The Windows Service template and associated functionality is not available in the Standard Edition of Visual Studio. For more information, see Visual Studio Editions. |
-
Locate the listing for MyNewLog and expand it. You should see entries for the actions your service has performed.
To uninstall your service
-
On the Start menu, open Control Panel and click Add/Remove Programs, and then locate your service and click Uninstall.
-
You can also uninstall the program by right-clicking the program icon for the .msi file and selecting Uninstall.
Note |
|---|
| If you installed the service on Windows 2000, you will need to reboot the system before you can reinstall the service. In Windows 2000, services are not completely deleted until the system is rebooted. |