开发者

Windows Service Conundrum

开发者 https://www.devze.com 2022-12-25 07:00 出处:网络
All, I have aCustom 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 interro

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)
    _in开发者_如何学运维cidentRepo = _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.


I've got a couple of suggestions.

First, Windows Services have to be able to successfully start with a set period of time (30 seconds, by default I believe). The error message you're getting doesn't point to this as a problem, but you may want to kick off a quick timer in the service's OnStart and have your Startup procedure in the TimerElapsed event handler. This would guarantee that the service starts every time.

Second, as was suggested in some of the comments, you've got to add logging to your app. Every good service should have the ability to log EVERYTHING that would be useful in tracing issues. Most of the common logging frameworks will allow you to set different verbose levels so when things are running well the logs are smaller.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号