Solved

Windows Service Template 2010Express for VB

Posted on 2010-08-24
10
2,852 Views
Last Modified: 2013-11-27
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
Comment
Question by:vbNewbie2009
  • 5
  • 4
10 Comments
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
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
 
LVL 41

Expert Comment

by:graye
Comment Utility
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
 
LVL 1

Author Comment

by:vbNewbie2009
Comment Utility
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
 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
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
 
LVL 1

Author Comment

by:vbNewbie2009
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 8

Expert Comment

by:Mohit Vijay
Comment Utility
my code will replace your function like

Protected Overrides Sub OnStop()

You can modified my given function as per need.

0
 
LVL 1

Author Comment

by:vbNewbie2009
Comment Utility
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
 
LVL 8

Accepted Solution

by:
Mohit Vijay earned 500 total points
Comment Utility
Problem Solved?
0
 
LVL 1

Author Closing Comment

by:vbNewbie2009
Comment Utility
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
 
LVL 1

Author Comment

by:vbNewbie2009
Comment Utility
Thanks for the guidance VjSoft!!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

20 Experts available now in Live!

Get 1:1 Help Now