Windows Service in C#

E

emanuelanechei

These days i'm creating a windows service in c# which reads some data
from a file and saves it in a database. The data in the file changes
every minute.
I am using FileSystemWatcher object which monitors that file for
LastWrite event.
The problem is that after the reading of file and writing the data in
the database the service quits unexpectedly.
What happends? I googled but found nothing about it.
Thank you in advance
 
A

Alberto Poblacion

These days i'm creating a windows service in c# which reads some data
from a file and saves it in a database. The data in the file changes
every minute.
I am using FileSystemWatcher object which monitors that file for
LastWrite event.
The problem is that after the reading of file and writing the data in
the database the service quits unexpectedly.
What happends? I googled but found nothing about it.
Thank you in advance

Try running the service attached to the debugger, so that you can trace
execution step by step and see where it is failing. To do this, start the
service, and then from Visual Studio select Debug -> Attach to Process, and
select the service that you started. In this way you can use the debugger
against the service in the same way as if you were debugging a standard
windows executable.
 
J

Jeroen Mostert

Alberto said:
Try running the service attached to the debugger, so that you can
trace execution step by step and see where it is failing. To do this,
start the service, and then from Visual Studio select Debug -> Attach to
Process, and select the service that you started. In this way you can
use the debugger against the service in the same way as if you were
debugging a standard windows executable.
This works, but a drawback of this method is that it doesn't allow you to
debug the process from the start, so you can miss events you need to see.
It's also quite inconvenient to have to do this every time.

A more convenient way of debugging a service is to run it as a console
application instead. To do this, change the boilerplate code generated for
the Main() method to something like this:

Service1 service = new Service1();
if (Environment.UserInteractive) {
service.OnStart();
Console.ReadLine();
service.OnStop();
} else {
ServiceBase.Run(service);
}

You will now be able to debug the service from within Visual Studio. Be
aware that there are differences between running the application like this
and running it as a service, the most important being that services
typically run under the local system account rather than your user account.
This method will not help you diagnose failures that are a result of the
service running in a special environment, but it will help you find
"regular" bugs.

Also, to diagnose unexpected exits, it always pays to hook up an event
handler to AppDomain.UnhandledException and write verbose logging somewhere.
Apropros logging: this, too, is invaluable for services, since the event log
should be used sparingly and is not suitable for dumping debugging
information to. I recommend a library like log4net for this.
 
J

Jeff Winn

Yep, I usually do something similar to what Jeroen said with all of the
services I have to maintain. I typically add a command line argument /debug
and pause the thread for 10 seconds in OnStart. Be aware that if the service
doesn't complete OnStart after approximately 30 seconds the service can be
terminated at any time. Also, I tend to use the Event Log on the machine to
log any unhandled exceptions that occur by the services. Depending on what
level of logging you need log4net or the Enterprise Library logging block
may be of use.

I'd start by checking the event log in the application section on the
machine where the errors are occuring at just to make sure something hasn't
already been logged.
 
E

Emy

Thank you for your response, I found out the cause that generated that
behaviour of the windows service.
The cause was that I was reading from the file too quickly after the
file was modified by the other process before the other process closed
the file.
I introduced a delay in the method that reads from the file and now
works fine, but the OnChange event of the FileSystemWatcher object is
triggered from 2 to 4 times.
I don't understand the reason for this. If you can help me this time
please send me a work around if you have any or any other idea that
would solve the issue.
I found out on the net that this is a known issue in the dotNet but
did not find any method to solve it.
Thank you again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top