Re: Windows Service in C#

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Alberto Poblacion wrote:
<emanuelanechei@xxxxxxxxx> wrote in message news:d66f3e2d-0435-4c44-b5d8-04fd06fca9c1@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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.

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.
.



Relevant Pages

  • Re: Office or InDesign breaks VS 2005 debugging?
    ... Given tha I was using and debugging the sub-projects in this solution before ... some getting used to) and the debug symbols for the child project are no ...
    (microsoft.public.vsnet.debugging)
  • Re: Cant Debug COM Interface
    ... When I start to debug the application the DLL is ... a com client in vb.net which is referencing the interface of the COM Server ... If I use this application as Host application I can debug without ... For debugging I can start the ...
    (microsoft.public.vsnet.debugging)
  • Re: C++ Workable Mainframe Debuggers
    ... you have to be able to read assembler to do ... IBM Debug Tool for z/OS is available. ... You can license Debug Tool as MLC or, in the form of the Debug Tool ... For graphical debugging use Rational Developer for System z (or ...
    (bit.listserv.ibm-main)
  • Re: Need help with my logic
    ... Hand Execution ... This will save you a lot of time when you write (and debug) large programs. ... In each method put at least one debugging statement at the head of a method that accepts information to show the arguments: ...
    (comp.lang.java.programmer)
  • Re: Need help on debugging the code in .NET Web application.
    ... > I'm getting the following error while trying to debug the application. ... There are a couple of reasons why debugging fails: ... The IIS mappings are incorrect (very common when IIS installed after ... where Windows directory is normally either Windows or WINNT. ...
    (microsoft.public.dotnet.academic)