• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3006
  • Last Modified:

Windows Service Template 2010Express for VB

I'm using Visual Studio 2010 Express for VB and would like to create windows services but this version doesn't contain the template by default.  Do you know where I may be able to find this template or have code that I could use to build my own service using the InstallUtil.exe object of .Net 4.0?

Thanks for your help!
0
vbNewbie2009
Asked:
vbNewbie2009
  • 5
  • 4
1 Solution
 
Mohit VijayCommented:
With template you can develop windows service.

Create a windows application using template.
Remove Form (default created) and add references of System.serviceProcess, System.Development.

/*Import below namespaces*/

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Configuration
Imports System.Timers
Imports System.Xml


Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.

        Interval = 60000 * CDbl(ConfigurationManager.AppSettings("TimeInterval"))
        ProcessTimer = New System.Timers.Timer(Interval)

        bContine = True

        AddHandler ProcessTimer.Elapsed, AddressOf CallConfigurationMethods
        With ProcessTimer
            .AutoReset = True
            .Enabled = True
            .Start()
        End With
    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
    End Sub

/*user created fucntion "CallConfigurationMethods" */

    Private Sub CallConfigurationMethods(ByVal Sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
        Try
            If bContine Then
                bContine = False
                SaveCSISConfigurations()
                SyncCRISheets()
                bContine = True
            End If
        Catch ex As Exception
            bContine = True
        End Try
    End Sub
0
 
grayeCommented:
Sorry, you can't import templates from the "full" versions of Visual Studio.  
You can however, download a 90-day trial of the full version
http://www.microsoft.com/visualstudio/en-us/download 
0
 
vbNewbie2009Author Commented:
VjSoft:  Thank you for the suggested option, i'm going to test this method over the next couple of days and let you  know if I stumble across anything that holds me up.

Graye:  I'm hopeful VjSoft's comment will allow me to create the service in the express version; if not, this would be a valid work around at least for 90 days.  I didn't think about this avenue; thanks.

I'll let you know how this goes in the next couple days, and thanks again for your guidance.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mohit VijayCommented:
I hope you will not just do copy and paste my code, you have to make changes according to your requirement. Please let me know if you need any clarification in my code. Thanks!
0
 
vbNewbie2009Author Commented:
VJSoft:
I found some code at http://www.dotheweb.net/2009/11/creating-services-with-vb-express/ that seemed to step me through the necessary components but I'm getting an error when trying to install using installutil.exe (.net framework 4.0).  

I wasn't quite sure how to fit your code in (console app or windows form app).  I ended up creating a console app with two classes.  When I run the install it states:  "No public installers with the RunInstallerAttribute.Yes attribute could be found"

Your code seemed a lot simpler; please let me know where I should inject your code, or what I might change to allow me to install from the code below.  

In addition to the two classes I added the folloing references to the proejct:
System.Configuration.Insatll
System.ServiceProcess

Thanks!
'NewService1 Class
Imports System.ServiceProcess
Imports System.Configuration.Install

Public Class NewService1

    Inherits System.ServiceProcess.ServiceBase
    Friend WithEvents Timer1 As System.Timers.Timer

    Public Sub New()

        MyBase.New()
        InitializeComponents()
        ' TODO: Add any further initialization code

    End Sub

    Private Sub InitializeComponents()

        Me.ServiceName = "NewService1"
        Me.AutoLog = True
        Me.CanStop = True
        Me.Timer1 = New System.Timers.Timer()
        Me.Timer1.Interval = 6000
        Me.Timer1.Enabled = True

    End Sub

    ' This method starts the service.
    <MTAThread()> Shared Sub Main()
        ' To run more than one service you have to add them to the array
        System.ServiceProcess.ServiceBase.Run(New System.ServiceProcess.ServiceBase() _
         {New NewService1})

    End Sub

    ' Clean up any resources being used.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        MyBase.Dispose(disposing)
        ' TODO: Add cleanup code here (if required)

    End Sub

    Protected Overrides Sub OnStart(ByVal args() As String)
        ' TODO: Add start code here (if required)
        ' to start your service.
        Me.Timer1.Enabled = True

    End Sub

    Protected Overrides Sub OnStop()
        ' TODO: Add tear-down code here (if required) 
        ' to stop your service.
        Me.Timer1.Enabled = False

    End Sub

    Private Sub InitializeComponent()

        Me.Timer1 = New System.Timers.Timer
        CType(Me.Timer1,  _
            System.ComponentModel.ISupportInitialize).BeginInit()
        '
        'Timer1
        '
        Me.Timer1.Enabled = True
        CType(Me.Timer1, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub

    Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As  _
                               System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed

        Dim MyLog As New EventLog() ' create a new event log 
        ' Check if the the Event Log Exists 
        If Not MyLog.SourceExists("NewService") Then
            MyLog.CreateEventSource("NewService1", "NewService1 Log") ' Create Log 
        End If

        MyLog.Source = "NewService1"
        MyLog.WriteEntry("NewService1 Log", "It is running", EventLogEntryType.Information)

        'disable the timer so you dont fill up the log
        Timer1.Enabled = False

    End Sub

End Class

'ProjectInstaller Class
Imports System.ComponentModel
Imports System.Configuration.Install

<RunInstaller(True)> Public Class ProjectInstaller
    Inherits System.Configuration.Install.Installer
    'Installer overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try

    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer
    'NOTE: The following procedure is required by the Component Designer
    'It can be modified using the Component Designer. 
    'Do not modify it using the code editor.

    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.ServiceProcessInstaller1 = New System.ServiceProcess.ServiceProcessInstaller
        Me.ServiceInstaller1 = New System.ServiceProcess.ServiceInstaller
        '
        'ServiceProcessInstaller1
        '
        Me.ServiceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem
        Me.ServiceProcessInstaller1.Password = Nothing
        Me.ServiceProcessInstaller1.Username = Nothing
        '
        'ServiceInstaller1
        '
        Me.ServiceInstaller1.ServiceName = "NewService1"
        Me.ServiceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic
        '
        'ProjectInstaller
        '
        Me.Installers.AddRange(New System.Configuration.Install.Installer() _
         {Me.ServiceProcessInstaller1, Me.ServiceInstaller1})

    End Sub

    Friend WithEvents ServiceProcessInstaller1 As New System.ServiceProcess.ServiceProcessInstaller()

    Friend WithEvents ServiceInstaller1 As New System.ServiceProcess.ServiceInstaller()

    Public Sub New()
        MyBase.New()
        'This call is required by the Component Designer.
        InitializeComponent()
        'Add initialization code after the call to InitializeComponent

    End Sub

End Class

Open in new window

0
 
Mohit VijayCommented:
my code will replace your function like

Protected Overrides Sub OnStop()

You can modified my given function as per need.

0
 
vbNewbie2009Author Commented:
Eureka!

Made sure the following references were loaded; or added if not, to the project:
System
System.Deployment
System.ServiceProcess

Created two classes:
See attached code...

Updated class properties:
Service1
   ...set Service1 class ServiceName property to Service1
ProjectInstaller
  ...set ServiceInstaller1 component ServiceName proeprty to Service1
  ...set ServiceProcessInstaller1 component Account property to LocalSystem

Added two objects to Service1 class:
   EventLog1
   Timer1

Built Project
Installed Using InstallUtil.exe
   ...I wasn't running installutil.exe as administrator and even though I am a local admin on my system it wasn't allowing the install.  After running cmd.exe as administrator (right click on utility in system32 folder) I was able to successfully install.
'##################
'##################
'Service1 Code
'##################
'##################

Public Class Service1

    ' To access the constructor in Visual Basic, select New from the
    ' method name drop-down list. 
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        'If Not System.Diagnostics.EventLog.SourceExists("MySource") Then
        'System.Diagnostics.EventLog.CreateEventSource("MySource",
        '"MyNewLog")
        'End If
        EventLog1.Source = "MySource"
        EventLog1.Log = "MyNewLog"

    End Sub


    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.
        EventLog1.WriteEntry("In OnStart")
        Timer1.Enabled = True

    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
        EventLog1.WriteEntry("In OnStop.")
        Timer1.Enabled = False

    End Sub

    Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
        'System.Console.Beep(15000, 10000)
        EventLog1.WriteEntry("Timer Entry " + TimeOfDay)
        Beep()
    End Sub
End Class

'##################
'##################
'ProjectInstaller Code
'##################
'##################

Imports System.ComponentModel
Imports System.Configuration.Install

Public Class ProjectInstaller

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()

        'Add initialization code after the call to InitializeComponent

    End Sub

End Class

Open in new window

0
 
Mohit VijayCommented:
Problem Solved?
0
 
vbNewbie2009Author Commented:
My last comment is an example that worked for me.  I tried to enter as much detail as possible for others who may want to do the same thing too.
0
 
vbNewbie2009Author Commented:
Thanks for the guidance VjSoft!!!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now