Solved

Deploying a Windows service with a Setup Project

Posted on 2004-08-17
2
262 Views
Last Modified: 2008-01-09
Hello experts,

I know how to install a service using installutil.exe from a command prompt, but how does one deploy a service through the Setup project?  I've done Windows apps, but no services.  Please help me.  

Thanks,

Kevin
0
Comment
Question by:carmodyk
2 Comments
 
LVL 28

Accepted Solution

by:
iboutchkine earned 250 total points
Comment Utility
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q317421&ID=kb;en-us;Q317421&SD=MSDN&FR=1

Create a Setup Project for a Windows Service in Visual Basic .NET

Create a Setup Project for a Windows Service

Create a Windows Service Project
1.On the Start menu, point to Programs, point to Microsoft Visual Studio .NET, and then click Microsoft
Visual Studio .NET.
2.On the File menu, point to New, and then click Project.
3.In the New Project dialog box, follow these steps:
  Under Project Types, click Visual Basic Projects.
  Under Templates, click Windows Service.
4.In the Name text box, type LogWriterService.
5.In the Location text box, type C:\, and then click OK.
6.In the Solution Explorer window, right-click Service1.vb, and then click View Code.
7.In the OnStart event handler, replace the comments with the following code:
   EventLog.WriteEntry("My simple service started.")
8.In Solution Explorer, double-click Service1.vb.
9.In the Properties dialog box, click Add Installer.
10.In the Properties dialog box for ServiceInstaller1, change the ServiceName property to LogWriterService.
11.In the Code Editor window in Design view, click ServiceProcessInstaller1.
12.In the Properties dialog box, change the Account property to LocalService.

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:

To add a new project to your LogWriterService project, follow these steps:
1.In Solution Explorer, right-click Solution 'LogWriterService', point to Add, and then click New Project.
2.Under Project Types, click Setup and Deployment Projects.
3.Under Templates, click Setup Project.
4.In the Name text box, type ServiceSetup.
5.In the Location text box, type C:\, and then click OK.

To tell the deployment project what it should package, follow these steps:
1.In Solution Explorer, right-click ServiceSetup, point to Add, and then click Project Output.
2.In the Add Project Output Group dialog box, in the Project box, click LogWriterService.
3.Click Primary Output, and then click OK.

For proper installation, you are only required to add primary output. To add the custom actions, follow
these steps:
1.In Solution Explorer, right-click ServiceSetup, point to View, and then click Custom Actions.
2.Right-click Custom Actions, and then click Add Custom Action.
3.Click Application Folder, and then click OK.
4.Click Primary output from LogWriterService (Active), and then click OK. Notice that Primary output appears
  under Install, Commit, Rollback and Uninstall.

Setup projects are not included in the build configuration by default. To build the solution, follow these
   steps:
1.Right-click LogWriterService, and then click Build. Subsequently, right-click ServiceSetup, and then click
  Build.

-or-

Click Configuration Manager on the Build menu to build the entire solution at once. Select the Build check
box for ServiceSetup.
Press the CTRL+SHIFT+B key combination to build the entire solution. When the solution is built, you have
a complete installation package available for the service.
To install the newly built service, right-click ServiceSetup, and then click Install.
In the ServiceSetup dialog box, click Next three times. Notice that a progress bar appears while the service installs.
When the service is installed, click Close.

Complete Code Listing (Service1.vb)
Imports System.ServiceProcess

Public Class Service1
    Inherits System.ServiceProcess.ServiceBase

#Region " Component Designer generated code "

    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 = "Service1"
    End Sub

#End Region

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

    Protected Overrides Sub OnStop()

    End Sub

End Class

Verify That It Works
On the Start menu, point to Control Panel, point to Administrative Tools, and then click Services.
Right-click Service1, and then click Start.
Use one of the following methods to verify that an event is logged in the event log:
On the Start menu, point to Control Panel, point to Administrative Tools, and then click Event Viewer. Click
Application Log in the left pane, and then locate the event log for your service in the right pane.
In Server Explorer, click to expand the Servers, the ComputerName, the Event Logs, the Application, and
the Service1 nodes. 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".

0
 
LVL 6

Author Comment

by:carmodyk
Comment Utility
Thanks a bunch.  Appreciate it.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now