Microsoft KB Archive/317421

= How to create a Setup project for a Windows Service in Visual Basic .NET or in Visual Basic 2005 =

Article ID: 317421

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q317421





For a Microsoft Visual C# .NET version of this article, see 816169.



SUMMARY
This article discusses how to create a Setup project for a Windows Service application. (Windows Service was formerly known as &quot;NT service.&quot;) To do this, you must first create a solution that contains a simple Windows Service project. This project writes an entry to the application log. You then add a Setup project to the solution to install the Window Service. Finally, you start the service from within Visual Studio .NET Server Explorer.

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

This article assumes that you have a general familiarity with Windows Services. If you are not familiar with Windows Services, see the first reference in the &quot;References&quot; section.

This article also assumes that the user account that you use to install and to run this service has the required permissions to install and to start services. The user account must also have the required permissions 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
 Click Start, point to Programs, point to Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, and then click Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. In the New Project dialog box, follow these steps:  Under Project Types, click Visual Basic Projects or click Windows under Visual Basic. Under Templates, click Windows Service. In the Name box, type LogWriterService .</li> In the Location box, type C:\, and then click OK.</li></ol> </li> In Solution Explorer, right-click Service1.vb, and then click View Code.</li>  In the OnStart event handler, replace the comments with the following code. EventLog.WriteEntry(&quot;My simple service started.&quot;) </li> In Solution Explorer, double-click Service1.vb.</li> In the Properties dialog box, click Add Installer.</li> In the Properties dialog box for ServiceInstaller1, change the ServiceName property to LogWriterService .</li> In Design view, click ServiceProcessInstaller1 in the Code Editor.</li> In the Properties dialog box, change the Account property to LocalSystem. The LocalService value and the NetworkService value are only available in Microsoft Windows XP and later operating systems.</li></ol>

Use a compiled Setup project to install the Windows Service
After you complete the steps in the &quot;Create a Windows Service project&quot; section to configure the Windows Service project, you can add a deployment project that packages the service application so that the service application can be installed. To do this, follow these steps: <ol> Add a new project to your LogWriterService project. <ol style="list-style-type: lower-alpha;"> In Solution Explorer, right-click Solution 'LogWriterService', point to Add, and then click New Project.</li> Under Project Types, click Setup and Deployment Projects or Setup and Deployment.</li> Under Templates, click Setup Project.</li> In the Name box, type ServiceSetup .</li> In the Location box, type C:\, and then click OK.</li></ol> </li> Tell the deployment project what the deployment project will package. <ol style="list-style-type: lower-alpha;"> In Solution Explorer, right-click ServiceSetup, point to Add, and then click Project Output.</li> <li>In the Add Project Output Group dialog box, click LogWriterService in the Project box.</li> <li>Click Primary Output, and then click OK.</li></ol> </li> <li>For correct installation, you have to 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, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Use one of the following methods: <ul> <li>Right-click LogWriterService, and then click Build. Then, right-click ServiceSetup, and then click Build.</li> <li>To build the whole solution at the same time, click Configuration Manager on the Build menu, and then click to select the Build check box for ServiceSetup.</li></ul> </li> <li>Press CTRL+SHIFT+B to build the whole solution. When the solution is built, you have a complete Setup package for the service.</li></ol> </li> <li>To install the 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 Setup program is installing the service.</li> <li>When the service is installed, click Close.</li></ol>

Complete code listing (Service1.vb)
Imports System.ServiceProcess

Public Class Service1 Inherits System.ServiceProcess.ServiceBase


 * 1) Region &quot; Component Designer generated code &quot;

Public Sub New MyBase.New

' The Component Designer requires this call. InitializeComponent

' Add any initialization after the InitializeComponent call.

End Sub

'UserService overrides Dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose End If       End If        MyBase.Dispose(disposing) End Sub

' This is the main entry point for the process. <MTAThread> _ Shared Sub Main Dim ServicesToRun As System.ServiceProcess.ServiceBase

' More than one NT Service may run within the same process. To add ' another service to this process, change the following line of        ' code 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) End Sub

'The Component Designer requires this code. Private components As System.ComponentModel.IContainer

' NOTE: The Component Designer requires the following procedure. ' You can use the Component Designer to modify the procedure. ' However, do not modify use the code editor to modify it. <System.Diagnostics.DebuggerStepThrough> Private Sub InitializeComponent components = New System.ComponentModel.Container Me.ServiceName = &quot;Service1&quot; End Sub


 * 1) End Region

Protected Overrides Sub OnStart(ByVal args As String) EventLog.WriteEntry(&quot;My simple service started.&quot;) End Sub

Protected Overrides Sub OnStop

End Sub

End Class

Verify that the Windows Service works
<ol> <li>Click Start, point to Control Panel, point to Administrative Tools, and then click Services.</li> <li>Right-click Service1, and then click Start.</li> <li>To verify that an event is logged in the event log, use one of the following methods: <ul> <li>Click Start, point to Control Panel, point to Administrative Tools, and then click Event Viewer. In the left pane, click Application Log. In the right pane, locate the event log for your service.</li> <li> In Server Explorer, expand Servers, expand  , expand Event Logs, expand Application, and then expand Service1. Remember that Service1 is the name of the class and not the name of the service. 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 through the log entries. The second entry from the top is the following: My simple service started </li></ul> </li></ol>

Troubleshooting
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:

http://msdn.microsoft.com/library/en-us/vbcon/html/vbtskdebuggingserviceapplications.asp

<div class="references_section">