c# windows service with filewatcher

Go To StackoverFlow.com

2

First time posting long time reader.

I built a working filewatcher inside of a windows forms application functioning 100% properly before moving it to a windows Service and am now recieving two seperate issues. This file watcher reads a flatfile for line updates(lastwrite), deletes/recreates file(streamwriter), and finally parses through a strongly typed data set and then uploads to an SQL server. (This is my first Windows Service) Questions:
1. Does the double event trigger in filewatcher effect the service differently then a forms application?
2. Does anyone have an answer about why the thread will break if the class I am calling has no issue?
3. Are there any known issues with Windows Authentication through a windows service?
4. Does anyone have any strong debug methods for windows services?

Here is my code from the windows Service, thanks in advance and my apologies if there is a silly mistake in the code, again first time making a windows service.

    FileMonitor m_FileMonitor;
    public WindowsService()
    {
        InitializeComponent();
    }
    protected override void OnStart(string[] args)
    {
            try
            {
                Thread myThread = new Thread(DoTheWork);
                myThread.Start();
            }
            catch
            {

            }

    }
    void DoTheWork()
    {
        m_FileMonitor = new FileMonitor(Properties.Settings.Default.PathToFileToWatch, Properties.Settings.Default.PathToErrorLog);
    }
    protected override void OnStop()
    {
        // TODO: Add code here to perform any tear-down necessary to stop your service.
    }
2012-04-04 23:05
by Shane
>1 What "double event trigger"?

2 Are you asking what the possible failure conditions are that could trigger your empty catch block?

3 What kind of Windows authentication? With what are you trying to authenticate? - David Nelson 2012-04-04 23:20



2

For debugging, make sure your project type is Windows Application, and then use this:

[DllImport("kernel32")]
static extern bool AllocConsole();

private static void Main(string[] args)
{
    var service = new MyService();
    var controller = ServiceController.GetServices().FirstOrDefault(c => c.ServiceName == service.ServiceName);
    if (null != controller && controller.Status == ServiceControllerStatus.StartPending)
    {
        ServiceBase.Run(service);
    }
    else
    {
        if (AllocConsole())
        {
            service.OnStart(args);
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
            service.OnStop();
        }
        else
        {
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
    }
}

If the code is running because the Windows Service was started, it will run as a Windows Service. Otherwise it will allocate a console, run the service, then wait for a key press before exiting the service. You could build on this for testing pause and continue.

2012-04-04 23:33
by David Nelson


0

For debugging:

You have to use the ServiceBase.Run method in Main() to execute as a windows service, but you can make a switch in the main method to run the same application as a normal console application (e.g. --standalone). I'm using this on all my services to make them easy to debug.

Regarding the other problems:

I'm not completely sure which problems you encounter and what you mean by "class break" and "double event trigger".

Windows services run under a special service account, which might or might not have permissions to watch the directory you are interested in. You can change the service account or give it permission for the directory if you need to.

Links:

Here is a link to a codeproject article who seems to have implemented a file watcher windows service. Maybe it helps:

http://www.codeproject.com/Articles/18521/How-to-implement-a-simple-filewatcher-Windows-serv

2012-04-04 23:09
by aKzenT
Ads