Windows Services

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I built a service that watch file in a directory. I use a simple filewatcher and add a handler to do something when a file is created. I have the following problem. I can build it, I can install it, I can start it but when I start it I have a message that the service is stop because it does nothing and it doesn't execute the code in the service. Please if you have any idea it will be appreciate

Thanks yo
 
Eric,
Are you doing too much work in the OnStart method?

Have you tried debugging your service:
http://msdn.microsoft.com/library/d.../vsdebug/html/vxtskdebugginwindowsservice.asp

Normally what I do is include a healthy amount of event log messages (via
..EventLog) especially in any asynchronous events that may be raised within
the service. Plus I will include Debug.Write & Trace.Write in strategic
locations.

Something like:

Public Class MyService
Inherits System.ServiceProcess.ServiceBase

Private WithEvents Timer1 As System.Timers.Timer

Public Sub New()
MyBase.New()

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

' Add any initialization after the InitializeComponent() call

AddHandler AppDomain.CurrentDomain.UnhandledException, _
AddressOf AppDomain_UnhandledException
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.
Timer1.Start()
End Sub

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

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
Try
EventLog.WriteEntry("Timer elapsed handler started",
EventLogEntryType.Information)
' Do work here
EventLog.WriteEntry("Timer elapsed handler finished",
EventLogEntryType.Information)
Catch ex As Exception
EventLog.WriteEntry(ex.ToString(), EventLogEntryType.Error)
End Try
End Sub

Private Sub AppDomain_UnhandledException(ByVal sender As Object, _
ByVal e As UnhandledExceptionEventArgs)
EventLog.WriteEntry(e.ExceptionObject.ToString(),
EventLogEntryType.Error)
End Sub


End Class

Note EventLog is inherited from ServiceBase and will be the normal event log
for your service, you can override ServiceBase.EventLog if you want to use a
different Event log.

I configure Debug & Trace to go to a log file.

In your app.config of your Windows Service project:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener"
initializeData="MyService.log" />
</listeners>
</trace>
</system.diagnostics>
</configuration>


Note the UnhandledException event is not working as I expected in the above,
so I put the Elapsed event in its own Try/Catch.

Hope this helps
Jay

Eric said:
Hi,

I built a service that watch file in a directory. I use a simple
filewatcher and add a handler to do something when a file is created. I have
the following problem. I can build it, I can install it, I can start it but
when I start it I have a message that the service is stop because it does
nothing and it doesn't execute the code in the service. Please if you have
any idea it will be appreciate.
 
Another choice is to use the Debug Viewer utility (free) from
www.sysinternals.com. With this program you can catch Debug.Writeline from
any program (including services) without any involvement from the IDE.
 
Hi

This is what I have in my OnStart

Tr
EcrireErreur("test"
'Création des logs s'il n'existe pas
If Not Log.SourceExists("FusionDec") The
Log.CreateEventSource("FusionDec", "FusionDec"
End I
Log.Source = "FusionDec

'option du FileSystemWatcher
fwFichierFusion.Path = "c:\Test2\
fwFichierFusion.Filter = "
fwFichierFusion.IncludeSubdirectories = Tru

fwFichierFusion.EnableRaisingEvents = Tru

'Ajout de l'action évènement
AddHandler fwFichierFusion.Created, AddressOf FusionDe

Log.WriteEntry("FusionDec démarrer avec succès"
Catch ex As Exceptio
Debug.WriteLine(Err.GetException.ToString()
EcrireErreur(Err.GetException.ToString()
Log.WriteEntry("Erreur lors du démarrage du service : " & vbCrLf & ex.ToString
End Tr

I think it isn't to much in the OnStart() and I can't debug it because it start and few second after it stop.

by the way thanks for trying
 
Eric,
Which of the following event log messages are you seeing?
Log.WriteEntry("FusionDec démarrer avec succès")
Log.WriteEntry("Erreur lors du démarrage du service : " &
vbCrLf & ex.ToString)

Depending on which you see will indicate if your service is starting or not.

If you are not seeing either then the OnStart is not being reached or is
terminating early.

Are you calling MyBase.OnStart from your OnStart?

More importantly! As I stated, put a try catch in your FusionDec method, so
you know if it failed or not, plus put logging to know it was called.

This way you if you see the success message you know the service started,
then you can look for the FusionDec messages, so you know


You do know that you can have the Windows Installer create the Event Source
& Event Log for you?
'Création des logs s'il n'existe pas.
If Not Log.SourceExists("FusionDec") Then
Log.CreateEventSource("FusionDec", "FusionDec")
End If
Log.Source = "FusionDec"

Drag an EventLog object from the toolbox onto the design surface of your
Windows Service. Set the Log & Source properties. Click the "Add Installer"
link (or right click on EventLog on the designer). This will add an
Installer to your Service Installer.

Also, rather then using your Log property I override the
ServiceBase.EventLog property and return the object created by dragging an
EventLog to the designer. This causes all the automatic messages as well as
my messages to go to the same log. (read you can leave AutoLog=True and the
auto log messages will go to the event log of your choosing).

Public Overrides ReadOnly Property EventLog() As
System.Diagnostics.EventLog
Get
Return Me.EventLog1
' Seeing as you have a Log property you can return it.
Return Log
End Get
End Property

Then I would simply use EventLog
EventLog.WriteEntry("FusionDec démarrer avec succès")
EventLog.WriteEntry("Erreur lors du démarrage du service : " &
vbCrLf & ex.ToString)

Hope this helps
Jay

Eric said:
Hi,

This is what I have in my OnStart :

Try
EcrireErreur("test")
'Création des logs s'il n'existe pas.
If Not Log.SourceExists("FusionDec") Then
Log.CreateEventSource("FusionDec", "FusionDec")
End If
Log.Source = "FusionDec"

'option du FileSystemWatcher.
fwFichierFusion.Path = "c:\Test2\"
fwFichierFusion.Filter = ""
fwFichierFusion.IncludeSubdirectories = True

fwFichierFusion.EnableRaisingEvents = True

'Ajout de l'action évènement.
AddHandler fwFichierFusion.Created, AddressOf FusionDec

Log.WriteEntry("FusionDec démarrer avec succès")
Catch ex As Exception
Debug.WriteLine(Err.GetException.ToString())
EcrireErreur(Err.GetException.ToString())
Log.WriteEntry("Erreur lors du démarrage du service : " & vbCrLf & ex.ToString)
End Try


I think it isn't to much in the OnStart() and I can't debug it because it
start and few second after it stop.
 
Thanks for your help but I found that it was a security problem. The user I use to install my service haven't the right to create a new EventLog.

great thank for you help. I found out with this little tip : You do know that you can have the Windows Installer create the Event Source & Event Log for you?

bye
 
Back
Top