EDN Admin
Well-known member
I have been working on creating a windows service. I am using XP SP3, VB.net, VS 2010 Express, compiling for .Net 4.0 client profile. I have am using very rudimentary code templates that Ive cobbled together.
I have a class the inherits ServiceBase and overrides the constructor, OnStart, and OnStop. I also have another class in another .vb file in the same project that inherits from System.Configuration.Install.Installer and overrides the constructor
to add a ServiceInstaller and a ServiceProcessInstaller with the appropriate properties set for the service. The ServiceInstaller.ServiceName is set to the same name as the service class and the installer class has the RunInstallerAttribuite(True)
attribute.
Everything compiles and if I try to run the exe I get a message telling me its a service and needs to be installed as such. but when I use installutil (.net 4) to install the service, it appears as though it was successful, but the service does not show
up in the services list and I cant find registry entries for it either. Ive checked the exe with the object browser and it presents the installer class with the RunInstallerAttribute as it should. I included some code to create a file in the installer
constructor and no file is created so Im assuming that the installer class is never instantiated by installutil.
Any ideas?? Thanks in advance
<pre class="prettyprint lang-vb Public Class ComplaintsMailer
Inherits System.ServiceProcess.ServiceBase
Public thisTimer As System.Timers.Timer
Public IsRunning As Boolean
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 to
create a second service object. For example,
ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
ServicesToRun = New System.ServiceProcess.ServiceBase() {New ComplaintsMailer}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
MsgBox("hey")
End Sub
Public Sub ComplaintsMailer()
Me.ServiceName = "ComplaintsMailer"
End Sub
This automatically-created subprocedure contains code you want run when the service is started.
Protected Overrides Sub OnStart(ByVal args() As String)
Set initial variable values.
Me.IsRunning = False
Create and start a timer that checks every 1 seconds.
thisTimer = New System.Timers.Timer()
thisTimer.Enabled = True
thisTimer.Interval = 1000
thisTimer.AutoReset = True
AddHandler thisTimer.Elapsed, AddressOf thisTimer_Tick
thisTimer.Start()
End Sub
This automatically-created subprocedure contains code you want run when the service is stopped.
Protected Overrides Sub OnStop()
thisTimer.Stop()
thisTimer.Dispose()
End Sub
This subprocedure is of our own creation, used to store code we want this service to actually execute.
Public Sub DoNextExecution()
SyncLock Me
thisTimer.Stop()
Another execution cycle begins.
Beep()
thisTimer.Start()
End SyncLock
End Sub
Each time the timer completes an interval, it executes the code found within this subprocedure.
Private Sub thisTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
DoNextExecution()
End Sub
End Class[/code]
<br/>
<pre class="prettyprint lang-vb Imports System.Configuration.Install
Imports System.ComponentModel
<summary>
Summary description for ProjectInstaller.
</summary>
<RunInstallerAttribute(True)> _
Public Class ProjectInstaller
Inherits System.Configuration.Install.Installer
<summary>
Required designer variable.
</summary>
private System.ComponentModel.Container components;
Private serviceInstaller As System.ServiceProcess.ServiceInstaller
Private serviceProcessInstaller As System.ServiceProcess.ServiceProcessInstaller
Public Sub ProjectInstaller()
me call is required by the Designer.
InitializeComponent()
End Sub
<summary>
Required method for Designer support - do not modify
the contents of me method with the code editor.
</summary>
Private Sub InitializeComponent()
Me.serviceInstaller = New System.ServiceProcess.ServiceInstaller()
Me.serviceProcessInstaller = New System.ServiceProcess.ServiceProcessInstaller()
serviceInstaller
Me.serviceInstaller.Description = "Automatically queries the copmplaints databse to check for new or overdue complaints and sends out the approriate emails"
Me.serviceInstaller.DisplayName = "Complaints Database Auto-Mailer"
Me.serviceInstaller.ServiceName = "ComplaintsMailer"
serviceProcessInstaller
Me.serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalService
Me.serviceProcessInstaller.Password = ""
Me.serviceProcessInstaller.Username = ""
ServiceInstaller
Me.Installers.AddRange(New System.Configuration.Install.Installer() {Me.serviceProcessInstaller, Me.serviceInstaller})
FileOpen(1, "indic.txt", OpenMode.Output)
FileClose(1)
End Sub
End Class[/code]
View the full article
I have a class the inherits ServiceBase and overrides the constructor, OnStart, and OnStop. I also have another class in another .vb file in the same project that inherits from System.Configuration.Install.Installer and overrides the constructor
to add a ServiceInstaller and a ServiceProcessInstaller with the appropriate properties set for the service. The ServiceInstaller.ServiceName is set to the same name as the service class and the installer class has the RunInstallerAttribuite(True)
attribute.
Everything compiles and if I try to run the exe I get a message telling me its a service and needs to be installed as such. but when I use installutil (.net 4) to install the service, it appears as though it was successful, but the service does not show
up in the services list and I cant find registry entries for it either. Ive checked the exe with the object browser and it presents the installer class with the RunInstallerAttribute as it should. I included some code to create a file in the installer
constructor and no file is created so Im assuming that the installer class is never instantiated by installutil.
Any ideas?? Thanks in advance
<pre class="prettyprint lang-vb Public Class ComplaintsMailer
Inherits System.ServiceProcess.ServiceBase
Public thisTimer As System.Timers.Timer
Public IsRunning As Boolean
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 to
create a second service object. For example,
ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
ServicesToRun = New System.ServiceProcess.ServiceBase() {New ComplaintsMailer}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
MsgBox("hey")
End Sub
Public Sub ComplaintsMailer()
Me.ServiceName = "ComplaintsMailer"
End Sub
This automatically-created subprocedure contains code you want run when the service is started.
Protected Overrides Sub OnStart(ByVal args() As String)
Set initial variable values.
Me.IsRunning = False
Create and start a timer that checks every 1 seconds.
thisTimer = New System.Timers.Timer()
thisTimer.Enabled = True
thisTimer.Interval = 1000
thisTimer.AutoReset = True
AddHandler thisTimer.Elapsed, AddressOf thisTimer_Tick
thisTimer.Start()
End Sub
This automatically-created subprocedure contains code you want run when the service is stopped.
Protected Overrides Sub OnStop()
thisTimer.Stop()
thisTimer.Dispose()
End Sub
This subprocedure is of our own creation, used to store code we want this service to actually execute.
Public Sub DoNextExecution()
SyncLock Me
thisTimer.Stop()
Another execution cycle begins.
Beep()
thisTimer.Start()
End SyncLock
End Sub
Each time the timer completes an interval, it executes the code found within this subprocedure.
Private Sub thisTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
DoNextExecution()
End Sub
End Class[/code]
<br/>
<pre class="prettyprint lang-vb Imports System.Configuration.Install
Imports System.ComponentModel
<summary>
Summary description for ProjectInstaller.
</summary>
<RunInstallerAttribute(True)> _
Public Class ProjectInstaller
Inherits System.Configuration.Install.Installer
<summary>
Required designer variable.
</summary>
private System.ComponentModel.Container components;
Private serviceInstaller As System.ServiceProcess.ServiceInstaller
Private serviceProcessInstaller As System.ServiceProcess.ServiceProcessInstaller
Public Sub ProjectInstaller()
me call is required by the Designer.
InitializeComponent()
End Sub
<summary>
Required method for Designer support - do not modify
the contents of me method with the code editor.
</summary>
Private Sub InitializeComponent()
Me.serviceInstaller = New System.ServiceProcess.ServiceInstaller()
Me.serviceProcessInstaller = New System.ServiceProcess.ServiceProcessInstaller()
serviceInstaller
Me.serviceInstaller.Description = "Automatically queries the copmplaints databse to check for new or overdue complaints and sends out the approriate emails"
Me.serviceInstaller.DisplayName = "Complaints Database Auto-Mailer"
Me.serviceInstaller.ServiceName = "ComplaintsMailer"
serviceProcessInstaller
Me.serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalService
Me.serviceProcessInstaller.Password = ""
Me.serviceProcessInstaller.Username = ""
ServiceInstaller
Me.Installers.AddRange(New System.Configuration.Install.Installer() {Me.serviceProcessInstaller, Me.serviceInstaller})
FileOpen(1, "indic.txt", OpenMode.Output)
FileClose(1)
End Sub
End Class[/code]
View the full article