Windows Service Conundrum
- by Paul Johnson
All, I have a Custom object which I have written using VB.NET (.net 2.0). The object instantiates its own threading.timer object and carries out a number of background process including periodic interrogation of an oracle database and delivery of emails via smtp according to data detected in the database. The following is the code implemented in the windows service class
Public Class IncidentManagerService
'Fakes
Private _fakeRepoFactory As IRepoFactory
Private _incidentRepo As FakeIncidentRepo
Private _incidentDefinitionRepo As FakeIncidentDefinitionRepo
Private _incManager As IncidentManager.Session
'Real
Private _started As Boolean = False
Private _repoFactory As New NHibernateRepoFactory
Private _psalertsEventRepo As IPsalertsEventRepo = _repoFactory.GetPsalertsEventRepo()
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.
If Not _started Then
Startup()
_started = True
End If
End Sub
Protected Overrides Sub OnStop()
'Tear down class variables in order to ensure the service stops cleanly
_incManager.Dispose()
_incidentDefinitionRepo = Nothing
_incidentRepo = Nothing
_fakeRepoFactory = Nothing
_repoFactory = Nothing
End Sub
Private Sub Startup()
Dim incidents As IList(Of Incident) = Nothing
Dim incidentFactory As New IncidentFactory
incidents = IncidentFactory.GetTwoFakeIncidents
_repoFactory = New NHibernateRepoFactory
_fakeRepoFactory = New FakeRepoFactory(incidents)
_incidentRepo = _fakeRepoFactory.GetIncidentRepo
_incidentDefinitionRepo = _fakeRepoFactory.GetIncidentDefinitionRepo
'Start an incident manager session
_incManager = New IncidentManager.Session(_incidentRepo, _incidentDefinitionRepo, _psalertsEventRepo)
_incManager.Start()
End Sub
End Class
After a little bit of experimentation I arrived at the above code in the OnStart method. All functionality passed testing when deployed from VS2005 on my development PC, however when deployed on a true target machine, the service would not start and responds with the following message:
"The service on local computer started and then stopped..."
Am I going about this the correct way? If not how can I best implement my incident manager within the confines of the Windows Service class. It seems pointless to implement a timer for the incidentmanager because this already implements its own timer...
Any assistance much appreciated.
Kind Regards
Paul J.