Export (0) Print
Expand All
43 out of 69 rated this helpful - Rate this topic

Walkthrough: Creating a Windows Service Application in the Component Designer

The procedures in this topic demonstrate creating a simple Windows Service application that writes messages to an event log. The basic steps that you perform to create and use your service include the following:

  • Create a project by using the Windows Service application template. This template creates a class for you that inherits from ServiceBase and writes much of the basic service code, such as the code to start the service.

  • Write the code for the OnStart and OnStop procedures, and override any other methods that you want to redefine.

  • Add the necessary installers for your service application.

  • Build your project.

  • Install your service on the local machine.

  • Access the Windows Services Control Manager and start your service.

To begin, you create the project and set values that are required for the service to function correctly.

Caution note Caution

The Windows Services project template that is required for this walkthrough is not available in the Express edition of Visual Studio.

NoteNote

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.

To create and configure your service

  1. On the File menu, click New Project.

    The New Project dialog box opens.

  2. Select Windows Service in the list of Visual Basic or Visual C# project templates, and name the project MyNewService. Click OK.

    The project template automatically adds a component class named Service1 that inherits from System.ServiceProcess.ServiceBase.

  3. On the Edit menu, choose Find and Replace, Find In Files (Keyboard: CTRL+SHIFT+F). Change all occurrences of Service1 to MyNewService. There are instances in Service1.cs, Program.cs, and in Service1.Designer.cs.

  4. Click the designer to select MyNewService. Then, in the Properties window, set the ServiceName and the (Name) property for Service1 to MyNewService.

In the next section, you add a custom event log to the 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.

To add custom event log functionality to your service

  1. In Solution Explorer, right-click Service1.vb or Service1.cs and select View Designer.

  2. From the Components section of the Toolbox, drag an EventLog component to the designer.

  3. In Solution Explorer, right-click Service1.vb or Service1.cs and select View Code.

  4. Add or edit the constructor to define a custom event log.

    	public MyNewService()
    	{
    		InitializeComponent();
    		if (!System.Diagnostics.EventLog.SourceExists("MySource")) 
    		{         
    				System.Diagnostics.EventLog.CreateEventSource(
    					"MySource","MyNewLog");
    		}
    		eventLog1.Source = "MySource";
    		eventLog1.Log = "MyNewLog";
    	}
    

To define what occurs when the service starts

  • In the Code Editor, locate the OnStart method that was automatically overridden when you created the project, and write code to determine what occurs when the service starts running:

    	protected override void OnStart(string[] args)
    	{
    		eventLog1.WriteEntry("In OnStart");
    	}
    
    Note Note

    A service application is designed to be long running. Therefore, it usually polls or monitors something in the system. The monitoring is set up in the OnStart method. However, OnStart does not actually do the monitoring. The OnStart method must return to the operating system after the service's operation has begun. It must not loop forever or block. To set up a simple polling mechanism, you can use the System.Timers.Timer component. In the OnStart method, you would set parameters on the component, and then you would set the Enabled property to true. The timer would then raise events in your code periodically, at which time your service could do its monitoring.

To define what occurs 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 override void OnStop()
    	{
    		eventLog1.WriteEntry("In onStop.");
    	}
    

You can also override the OnPause, OnContinue, and OnShutdown methods to define additional processing for your component.

To define other actions for the service

  • For the method that 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 override void OnContinue()
    	{
    		eventLog1.WriteEntry("In OnContinue.");
    	}  
    

Some custom actions have to occur when a Windows service is installed, 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

  1. In Solution Explorer, right-click Service1.vb or Service1.cs and select View Designer.

  2. Click the background of the designer to select the service itself, instead of any of its contents.

  3. With the designer in focus, right-click, and then click Add Installer.

    By default, a component class that contains 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.

  4. In Design view for ProjectInstaller, click ServiceInstaller1 for a Visual Basic project, or serviceInstaller1 for a Visual C# project.

  5. In the Properties window, make sure the ServiceName property is set to MyNewService.

  6. Set the StartType property to Automatic.

  7. In the designer, click ServiceProcessInstaller1 for a Visual Basic project, or serviceProcessInstaller1 for a Visual C# project. Set the Account property to LocalSystem. This will cause the service to be installed and to run on a local service account.

    Security note Security Note

    The LocalSystem account has broad permissions, including the ability to write to the event log. Use this account with caution, because it might increase your risk of attacks from malicious software. For other tasks, consider using the LocalService account, which acts as a non-privileged user on the local computer and presents anonymous credentials to any remote server. This example fails if you try to use the LocalService account since it doesn't have permissions to write to the event log.

To build your service project

  1. In Solution Explorer, right-click your project and then click Properties. The project's Property Designer appears.

  2. On the Application page, from the Startup object list, click MyNewService.Program.

  3. Press CTRL+SHIFT+B to build the project.

Now that the you've built the Windows service, you can install it. To install a Windows service, you must have administrative permissions on the computer on which you're installing it.

To install a Windows service

  1. On the Start menu or Start Page, open the shortcut menu for Developer Command Prompt, and then choose Run As Administrator.

  2. Navigate to the folder that contains your project's output. For example, under your My Documents folder, navigate to Visual Studio 2013\Projects\MyNewService\bin\Debug.

  3. Enter the following command:

    installutil.exe MyNewService.exe
    

    If the service installs successfully, installutil.exe will report success.

To start and stop your service

  1. To open the Services Control Manager in Windows 7, Windows Vista, and Windows Server, right-click Computer on the Start menu, and then click Manage. In the Computer Management console, expand the Services and Applications node in the left pane. Click Services.

    You should now see MyNewService listed in the Services section of the window.

  2. Select your service in the list, right-click it, and then click Start.

  3. Right-click the service, and then click Stop.

To verify the event log output of your service

  1. Open Server Explorer (Keyboard: CTRL+ALT+S) and access the Event Logs node for the local computer.

  2. Locate the listing for MyNewLog and expand it. You should see entries for the actions your service has performed.

To uninstall your service

  1. On the Start menu or Start Page, open the shortcut menu for Developer Command Prompt, and then choose Run As Administrator.

  2. Navigate to the folder that contains your project's output. For example, under your My Documents folder, navigate to Visual Studio 2013\Projects\MyNewService\bin\Debug

  3. Enter the following command:

    installutil.exe /u MyNewService.exe
    

    If the service uninstalls successfully, installutil.exe will report that your service was successfully removed.

You can create a standalone setup with which others can install your Windows service, but you must perform additional steps. ClickOnce doesn't support Windows services, so you can't use the Publish Wizard. You can use a full edition of InstallShield, which Microsoft doesn't provide. For more information about InstallShield, seeInstallShield Limited Edition. You can also use Windows Installer XML to create an installer for a Windows Service. For more information, see Windows Installer XML Toolset.

You might explore the use of a ServiceController component to enable you to send commands to the service you have installed.

You can use an installer to create an event log when the application is installed instead of creating the event log when the application runs. Additionally, the event log will be deleted by the installer when the application is uninstalled.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.