Solved

Deploying a Windows service with a Setup Project

Posted on 2004-08-17
2
263 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
ID: 11820829
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
ID: 11840860
Thanks a bunch.  Appreciate it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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 …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

932 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

15 Experts available now in Live!

Get 1:1 Help Now