带有filewatcher的c#windows服务

带有filewatcher的c#windows服务,c#,windows-services,filesystemwatcher,windows-authentication,C#,Windows Services,Filesystemwatcher,Windows Authentication,第一次张贴长时间的读者 在将其移动到windows服务之前,我在windows窗体应用程序中构建了一个运行正常的filewatcher,现在收到两个不同的问题。此文件监视程序读取一个平面文件以进行行更新(lastwrite),删除/重新创建文件(streamwriter),最后通过强类型数据集进行解析,然后上载到SQL server。 (这是我的第一个Windows服务) 问题: 1.filewatcher中的双事件触发器对服务的影响是否与表单应用程序不同? 2.如果我调用的类没有问题,那么为什

第一次张贴长时间的读者

在将其移动到windows服务之前,我在windows窗体应用程序中构建了一个运行正常的filewatcher,现在收到两个不同的问题。此文件监视程序读取一个平面文件以进行行更新(lastwrite),删除/重新创建文件(streamwriter),最后通过强类型数据集进行解析,然后上载到SQL server。 (这是我的第一个Windows服务) 问题:
1.filewatcher中的双事件触发器对服务的影响是否与表单应用程序不同?
2.如果我调用的类没有问题,那么为什么线程会中断,有人有答案吗?
3.通过Windows服务进行Windows身份验证是否存在任何已知问题?
4.有人有针对windows服务的强大调试方法吗

这是我的windows服务代码,提前谢谢,如果代码中有愚蠢的错误,我道歉,这是我第一次使用windows服务

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

用于调试:

您必须使用
ServiceBase.Run
Main()
中的
方法作为windows服务执行,但您可以在Main方法中进行切换,以运行与普通控制台应用程序相同的应用程序(例如
--standalone
)。我在我的所有服务上都使用了它,以使它们易于调试

关于其他问题:

我不能完全确定你遇到了哪些问题,以及你所说的“课堂中断”和“双事件触发”是什么意思

Windows服务在特殊服务帐户下运行,该帐户可能有权也可能没有权查看您感兴趣的目录。如果需要,您可以更改服务帐户或授予其目录权限

链接:

下面是一篇codeproject文章的链接,该文章似乎实现了一个文件监视程序windows服务。也许这有助于:


要进行调试,请确保项目类型为Windows应用程序,然后使用以下命令:

[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());
        }
    }
}
如果由于Windows服务已启动而导致代码正在运行,则它将作为Windows服务运行。否则,它将分配一个控制台,运行服务,然后在退出服务之前等待按键。您可以在此基础上继续测试暂停和继续。

#1什么是“双事件触发器”#2您是否在询问可能触发空挡块的故障条件#3什么样的Windows身份验证?你想用什么来验证?