Microsoft KB Archive/816169

= How to create a setup project for a Windows Service application in Visual C# .NET and in Visual C# 2005 =

Article ID: 816169

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-





For a Microsoft Visual Basic .NET version of this article, see 317421.

IN THIS TASK

 * SUMMARY
 * Requirements
 * Create a Setup Project for a Windows Service
 * Create a Windows Service Project
 * Use a Compiled Setup Project to Install the Windows Service
 * Complete Code Listing
 * Verify That It Works
 * Troubleshoot
 * REFERENCES



SUMMARY
This article describes how to create a setup project for a Windows Service application (formerly named an &quot;NT service&quot;). To do this, you must first create a solution that contains a simple Windows Service project that writes an entry to its application log. You then add a setup project to the solution to install the Window Service. Finally, you start the service from Microsoft Visual Studio .NET or Microsoft Visual Studio 2005 Server Explorer.

back to the top

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

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.

back to the top

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
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Click Visual C# Projects under Project Types, and then click Windows Service under Templates.

Note In Visual Studio 2005, expand Visual C# under Project Types, click Windows, and then click Windows Service under Templates. Type LogWriterService in the Name text box, and then type C:\ in the Location text box. Click OK. In Solution Explorer, right-click Service1.cs, and then click View Code.  In the OnStart event handler, replace the comments with the following code: EventLog.WriteEntry(&quot;My simple service started.&quot;);  In Solution Explorer, double-click Service1.cs.</li> In the Code Editor window, right-click Design View, and then click Properties</li> In the Properties pane, click the Add Installer link.</li> In the Properties pane for ServiceInstaller1, change the ServiceName property to Service1 .</li> In the Code Editor window in Design view, click ServiceProcessInstaller1.</li> In the Properties pane, change the Account property to LocalSystem (The LocalService and NetworkService values are available only in Microsoft Windows XP).</li></ol>

back to the top

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: <ol> Add a new project to your LogWriterService project. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> In Solution Explorer, right-click Solution 'LogWriterService' (1 project), point to Add, and then click New Project.</li> Click Setup and Deployment Projects under Project Types, and then click Setup Project under Templates.</li> In the Name text box, type ServiceSetup .</li> Type C:\ in the Location text box, and then click OK.</li></ol> </li> Tell the deployment project what to package. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> In Solution Explorer, right-click ServiceSetup, point to Add, and then click Project Output</li> In the Add Project Output Group dialog box, in the Project box, click LogWriterService</li> Click Primary Output, and then click OK.</li></ol> </li> For correct installation, add only primary output. To add the custom actions, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>In Solution Explorer, right-click ServiceSetup, point to View, and then click Custom Actions</li> <li>Right-click Custom Actions, and then click Add Custom Action.</li> <li>Click Application Folder, and then click OK.</li> <li>Click Primary output from LogWriterService (Active), and then click OK.

Notice that Primary output appears under Install, Commit, Rollback and Uninstall.</li></ol> </li> <li>By default, setup projects are not included in the build configuration. To build the solution, use one of the following methods: <ul> <li>Method 1 <ol style="list-style-type: lower-alpha;"> <li>Right-click LogWriterService, and then click Build.</li> <li>Right-click ServiceSetup, and then click Build.</li></ol> </li> <li>Method 2 <ol style="list-style-type: lower-alpha;"> <li>On the Build menu, click Configuration Manager to build the whole solution.</li> <li>Click to select the Build check box for ServiceSetup.</li> <li>Press F7 to build the whole solution. When the solution is built, you have a complete installation package that is available for the service.</li></ol> </li></ul> </li> <li>To install the newly built service, right-click ServiceSetup, and then click Install.</li> <li>In the ServiceSetup dialog box, click Next three times. Notice that a progress bar appears while the service installs.</li> <li>When the service is installed, click Close.</li></ol>

back to the top

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 {       ///         /// Required designer variable. ///        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); }

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

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

///        /// Set things in motion so your service can do its work. ///        protected override void OnStart(string[] args) {           EventLog.WriteEntry(&quot;My simple service started.&quot;); }       ///         /// Stop this service. ///        protected override void OnStop {           // TODO: Add code here to perform any tear-down necessary to stop your service. }   } } back to the top

Verify That It Works
<ol> <li>In Control Panel, double-click Administrative Tools, and then double-click Services</li> <li>Right-click Service1, and then click Start</li> <li>Use one of the following methods to verify that an event is logged in the event log: <ul> <li>Method 1 <ol style="list-style-type: lower-alpha;"> <li>In Control Panel, double-click Administrative Tools, and then double-click Event Viewer.</li> <li>Click Application Log in the left pane, and then locate the event log for your service from the right pane.</li></ol> </li> <li>Method 2 <ol style="list-style-type: lower-alpha;"> <li>In Server Explorer, expand Servers, expand  , 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.)</li> <li>Move the cursor over the log entries. The second entry from the top should read &quot;My simple service started&quot;.</li></ol> </li></ul> </li></ol>

back to the top

Troubleshoot
The Framework SDK documentation states the following:

The compiled executable file that a service application project creates must be installed on the server before the project can function in a meaningful way. You cannot debug or run a service application by pressing F5 or F11; you cannot immediately run a service or step into its code. Instead, you must install and start your service, and then attach a debugger to the service's process.

For more information, visit the following Microsoft Developer Network (MSDN) Web site:

Debugging Windows Service Applicationshttp://msdn2.microsoft.com/en-us/library/aa984342(VS.71).aspx

back to the top

<div class="references_section">