FileSystemWatcher对象的OnError不是从Windows服务工作是从、对象、工作、FileSystemWatcher

2023-09-07 09:00:01 作者:几许清风.

我有一个Windows服务一个FileSystemWatcher的对象。我使用该应用程序作为存根我的组件写在一个控制台应用程序。

I have a FileSystemWatcher object in a Windows service. I wrote it in a console application using that app as a stub for my component.

我的组件实例FileSystemWatcher对象,并设置观看映射驱动器。这对我的伟大工程,无论从测试桩控制台应用程序和部署Windows服务。

My component instantiates the FileSystemWatcher and sets up to watch a mapped drive. This works great for me from both the test stub Console App and the Deployable Windows Service.

我也onerror事件迷上了log4net的日志记录一个致命级错误:

I also have the onError event hooked up with log4net logging a FATAL level error:

public FileSystemWatcher CreateFileWatcher()
    {
        FileSystemWatcher watcher = new FileSystemWatcher();

        try
        {
            log.Info("Configuring DPIFileWatcher");
            watcher.Filter = "*.xml";
            log.Info("DPIFileWatcher Filter set to: *.xml");
            string watcherPath = ConfigurationManager.AppSettings["InoundPath"].ToString();
            watcher.Path = watcherPath;
            log.Info(String.Format("DPIFileWatcher Path set to: {0}", watcherPath)); 

            watcher.Created += new FileSystemEventHandler(DPIWatcher_FileCreated);
            watcher.Changed += new FileSystemEventHandler(DPIWatcher_FileChanged);
            watcher.Error += new ErrorEventHandler(DPIWatcher_Error);
            watcher.EnableRaisingEvents = true;

            log.Info("DPIFileWatcher Configuration Successful.");
        }
        catch(Exception e)
        {
            log.Fatal(String.Format("Failed to configure DPIFileWatcher: {0}", e.Message));
        }

        return watcher;
    }

这是我的错误事件:

Here is my error event:

    private void DPIWatcher_Error(object source, ErrorEventArgs e)
    {
        log.Fatal(String.Format("FileWatacher error: {0}", e.GetException().Message));
    }

如果我拔下网卡测试网络错误的损失,我从我的控制台应用程序获取以下日志错误:

If I test for a network error loss by unplugging the network card, I get the following log error from my console app:

FATAL   [  12] 2013-02-12 12:14:02,142 SMILLER-E6430 METHOD: DPIWatcher_Error     GenFileWatch.DPIFileWatcher- FileWatacher error: The specified network name is no longer available (C:\Development\GenFileWatch\GenFileWatch\DPIFileWatcher.cs: 447)

不过,从一个Windows服务运行时,该日志错误将无法正常工作。

But this log error will not work when running from within a Windows service.

有没有人有任何想法,为什么或如何解决?

Does anyone have any idea why or how to fix?

推荐答案

首先,是你的服务,可以访问你要监视的目录的帐户运行。 99%的时间,运行一个控制台应用程序,并运行在两个不同的用户上下文中运行服务。如果该用户的上下文有到该目录没有访问权限(或URL只意味着东西在另一个用户上下文中),我不认为会的OnError被调用。

First of all, is your service running with an account that has access to the directory you're trying to monitor. 99% of the time, running a "console" app and running a "service" run under two different user contexts. If that user context has no access to that directory (or the URL only means something in another user context), I don't think OnError would be invoked.

其次, FileSystemWatcher的是相当不可靠的。它的工作原理,大部分的时间,但有时却没有。它利用底层的本机功能``这是记录与

Second, FileSystemWatcher is fairly unreliable. It works, most of the time, but sometimes doesn't. It uses the underlying native function `` which is documented with

当你第一次打电话ReadDirectoryChangesW,系统分配一个   缓冲存储更改信息。这个缓冲器与相关联的   目录手柄,直到它被关闭,它的规模不会改变   在其生命周期。调用之间发生目录的变化   此功能被添加到缓冲器,然后与下一个返回   呼叫。如果缓冲器溢出,缓冲器的全部内容都   丢弃,则lpBytesReturned参数包含零,以及   ReadDirectoryChangesW函数失败,出现错误code   ERROR_NOTIFY_ENUM_DIR

When you first call ReadDirectoryChangesW, the system allocates a buffer to store change information. This buffer is associated with the directory handle until it is closed and its size does not change during its lifetime. Directory changes that occur between calls to this function are added to the buffer and then returned with the next call. If the buffer overflows, the entire contents of the buffer are discarded, the lpBytesReturned parameter contains zero, and the ReadDirectoryChangesW function fails with the error code ERROR_NOTIFY_ENUM_DIR