Wednesday, 31 July 2013

How to create a setup project for a Windows Service application in Visual C#


Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you must have:
  1. Microsoft Windows 2000, Windows XP, Windows Server 2003, or a later Microsoft operating system with the .NET Framework installed
  2. Microsoft Visual Studio .NET Enterprise, Microsoft Visual Studio .NET Enterprise Architect, Microsoft Visual Studio 2005, or Microsoft Visual Studio 2008.

This article assumes that you are familiar with Windows Services. If you are not familiar with Windows Services, see the first reference in the REFERENCES section of this article.

This article also assumes that the user account that you use to install and to run this service has the permissions that you must have to install and to start services, and also has the permissions that you must have to access the event log.


Create a Setup Project for a Windows Service
This section describes how to create a Windows Service project, and how to use a compiled setup project to install the Windows Service.
Create a Windows Service Project
1.      Start Microsoft Visual Studio.
2.      On the File menu, point to New, and then click Project.
3.      Click Visual C# Projects under Project Types, and then click Windows Service under Templates.

Note In Visual Studio 2005 or Visual Studio 2008, expand Visual C# under Project Types, click Windows, and then click Windows Service under Templates.
4.      Type LogWriterService in the Name text box, and then type C:\ in the Location text box. Click OK.
5.      In Solution Explorer, right-click Service1.cs, and then click View Code.
6.      In the OnStart event handler, replace the comments with the following code:
EventLog.WriteEntry("My simple service started.");
7.      In Solution Explorer, double-click Service1.cs.
8.      In the Code Editor window, right-click Design View, and then click Properties
9.      In the Properties pane, click the Add Installer link.
10.  In the Properties pane for ServiceInstaller1, change the ServiceName property to Service1.
11.  In the Code Editor window in Design view, click ServiceProcessInstaller1.
12.  In the Properties pane, change the Account property to LocalSystem (The LocalService and NetworkService values are available only in Microsoft Windows XP).
Use a Compiled Setup Project to Install the Windows Service
After you complete the steps in the previous section to configure the Windows Service project, follow these steps to add a deployment project that packages the service application so that the service application can be installed:
1.      Add a new project to your LogWriterService project. To do this, follow these steps:
a.       In Solution Explorer, right-click Solution 'LogWriterService' (1 project), point to Add, and then click New Project.
b.      Click Setup and Deployment Projects under Project Types, and then click Setup Project under Templates.
c.       In the Name text box, type ServiceSetup.
d.      Type C:\ in the Location text box, and then click OK.
2.      Tell the deployment project what to package. To do this, follow these steps:
 .        In Solution Explorer, right-click ServiceSetup, point to Add, and then click Project Output
a.       In the Add Project Output Group dialog box, in the Project box, click LogWriterService
b.      Click Primary Output, and then click OK.
3.      For correct installation, add only primary output. To add the custom actions, follow these steps:
 .        In Solution Explorer, right-click ServiceSetup, point to View, and then click Custom Actions
a.       Right-click Custom Actions, and then click Add Custom Action.
b.      Click Application Folder, and then click OK.
c.       Click Primary output from LogWriterService (Active), and then click OK.

Notice that Primary output appears under Install, Commit, Rollback and Uninstall.
4.      By default, setup projects are not included in the build configuration. To build the solution, use one of the following methods:
o    Method 1
a.       Right-click LogWriterService, and then click Build.
b.      Right-click ServiceSetup, and then click Build.
o    Method 2
 .        On the Build menu, click Configuration Manager to build the whole solution.
a.       Click to select the Build check box for ServiceSetup.
b.      Press F7 to build the whole solution. When the solution is built, you have a complete installation package that is available for the service.
5.      To install the newly built service, right-click ServiceSetup, and then click Install.
6.      In the ServiceSetup dialog box, click Next three times. Notice that a progress bar appears while the service installs.
7.      When the service is installed, click Close.
Complete Code Listing
Service1.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;

namespace LogWriterService
{
               public class Service1 : System.ServiceProcess.ServiceBase
               {
                               /// <summary>
                               /// Required designer variable.
                               /// </summary>
                               private System.ComponentModel.Container components = null;

                               public Service1()
                               {
                                              // The Windows.Forms Component Designer must have this call.
                                              InitializeComponent();

                                              // TODO: Add any initialization after the InitComponent call
                               }

                               // The main entry point for the process
                               static void Main()
                               {
                                              System.ServiceProcess.ServiceBase[] ServicesToRun;
              
                                              // More than one user service may run in the same process. To add
                                              // another service to this process, change the following line to
                                              // create a second service object. For example,
                                              //
                                              //   ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
                                              //
                                              ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };

                                              System.ServiceProcess.ServiceBase.Run(ServicesToRun);
                               }

                               /// <summary>
                               /// Required method for Designer support - do not modify
                               /// the contents of this method with the code editor.
                               /// </summary>
                               private void InitializeComponent()
                               {
                                              components = new System.ComponentModel.Container();
                                              this.ServiceName = "Service1";
                               }

                               /// <summary>
                               /// Clean up any resources that are being used.
                               /// </summary>
                               protected override void Dispose( bool disposing )
                               {
                                              if( disposing )
                                              {
                                                             if (components != null)
                                                             {
                                                                            components.Dispose();
                                                             }
                                              }
                                              base.Dispose( disposing );
                               }

                               /// <summary>
                               /// Set things in motion so your service can do its work.
                               /// </summary>
                               protected override void OnStart(string[] args)
                               {
                                              EventLog.WriteEntry("My simple service started.");
                               }

                               /// <summary>
                               /// Stop this service.
                               /// </summary>
                               protected override void OnStop()
                               {
                                              // TODO: Add code here to perform any tear-down necessary to stop your service.
                               }
               }
}
Verify That It Works
1.      In Control Panel, double-click Administrative Tools, and then double-click Services
2.      Right-click Service1, and then click Start
3.      Use one of the following methods to verify that an event is logged in the event log:
o    Method 1
      • In Control Panel, double-click Administrative Tools, and then double-click Event Viewer.
      • Click Application Log in the left pane, and then locate the event log for your service from the right pane.
    • Method 2
      • In Server Explorer, expand Servers, expand ComputerName, expand Event Logs, expand Application, and then expand Service1. Recall that Service1 is the name of the class, not the service itself. Therefore, Service1 is used as the application name. (It is beyond the scope of this article to explain how to customize the names.)
      • Move the cursor over the log entries. The second entry from the top should read "My simple service started".


No comments:

Post a Comment