asp.net - FileSystemWatcher 在 Azure 中不会触发

标签 asp.net azure filesystemwatcher

我试图找出为什么我的 FileSystemWatcher 在本地工作,但当我将其部署到 Azure Web 应用程序时却无法工作。

我验证了文件正在 webapp 文件夹中创建,但创建文件时观察器似乎没有触发。 Azure 日志或我的应用程序日志中没有相关的错误/警告消息。我创建了一个示例应用程序来进行调查,我在那里看到了同样的问题。

public sealed class TestController : Controller
{
    private static readonly ILog _log = LogManager.GetLogger(typeof(TestController));
    private static readonly string _folder = ConfigurationManager.AppSettings["BasePath"];
    private static FileSystemWatcher _watcher;

    [HttpGet, Route("start-monitoring")]
    public ActionResult StartMonitoring()
    {
        if (_watcher != null)
            return new HttpStatusCodeResult(HttpStatusCode.OK);

        _watcher = new FileSystemWatcher(_folder, "*.*") { EnableRaisingEvents = true };

        _watcher.Created += (sender, args) =>
            _log.Debug($"YAY!!! Watcher triggered, new file found `{args.FullPath}`");

        _log.Debug($"Watcher started for folder `{_folder}`");
        return new HttpStatusCodeResult(HttpStatusCode.Created);
    }

    [HttpGet, Route("create-file/{filename}")]
    public async Task<ActionResult> CreateFileAsync(string filename)
    {
        string fullFilename = $"{_folder}\\{filename}.txt";
        using (var writer = new StreamWriter(fullFilename))
        {
            await writer.WriteAsync("Some content").ConfigureAwait(false);
        }

        _log.Debug($"Created file `{fullFilename}`");
        return new HttpStatusCodeResult(HttpStatusCode.OK);
    }
}
<小时/>

本地

使用上面的代码,当我转到http://localhost:23481/start-monitoring时,这将创建 FileSystemWatcher监控 C:\test文件夹。

然后当我转到http://localhost:23481/create-file/blahblah时名为 blahblah.txt 的文件在文件夹 C:\test 中创建。日志文件显示正在创建的文件,以及 FSW 触发器,表明它注意到出现了新文件。

2019-08-15T10:37:54.705Z    DEBUG     TestProject.TestController
Watcher started for folder `C:\test`

2019-08-15T10:38:02.399Z    DEBUG     TestProject.TestController
Created file `C:\test\blahblah.txt`

2019-08-15T10:38:02.400Z    DEBUG     TestProject.TestController
YAY!!! Watcher triggered, new file found `C:\test\blahblah.txt`

2019-08-15T10:38:06.137Z    DEBUG     TestProject.TestController
Created file `C:\test\another.txt`

2019-08-15T10:38:06.141Z    DEBUG     TestProject.TestController
YAY!!! Watcher triggered, new file found `C:\test\another.txt`
<小时/>

azure

我将相同的代码部署到 Azure,唯一的代码差异是配置中的基本路径。日志显示FileSystemWatcher已正确创建以监视 D:\home\site\wwwroot\test Azure webapp 文件系统上的文件夹。

然后当我转到https://xxx.azurewebsites.net/create-file/blahblah时名为 blahblah.txt 的文件在文件夹 D:\home\site\wwwroot\test 中创建。日志文件显示正在创建的文件,但不显示创建时触发的 FSW:

2019-08-15T10:43:01.681Z    DEBUG     TestProject.TestController
Watcher started for folder `D:\home\site\wwwroot\test`

2019-08-15T10:43:27.010Z    DEBUG     TestProject.TestController
Created file `D:\home\site\wwwroot\test\blahblah.txt`

2019-08-15T10:43:33.834Z    DEBUG     TestProject.TestController
Created file `D:\home\site\wwwroot\test\another.txt`
<小时/>

我还尝试在文件夹中手动创建文件,但这仍然不会触发 YAY!!!日志条目。 FileSystemWatcher 在 Azure 中不会触发,这是一个已知问题,还是我错过了什么?

<小时/>

编辑

我已将处理程序分配给 FileSystemWatcher 支持的所有事件,即更改/创建/删除/处置/错误/重命名,并且在 Azure 中为我触发的唯一一个是 Changed 。我想我必须进行某种黑客攻击来监视 Changed 事件,我可能可以假设如果文件存在,则它是“Created”事件,否则是“Deleted”事件。

最佳答案

我更改了代码以监听 FileSystemWatcher 支持的所有事件。我发现 Azure 上触发的唯一事件是 Changed 事件。没有调用其他事件。我找不到任何关于此的文档,因此它可能是一个错误,也可能是设计好的,谁知道呢。

我现在使用的修复如下,基本上是在更改事件上,我检查文件是否存在,如果存在,我假设它是“文件创建事件”,否则是“文件删除事件”。

[HttpGet, Route("start-monitoring")]
public ActionResult StartMonitoring()
{
    if (_watcher != null)
        return new HttpStatusCodeResult(HttpStatusCode.OK);

    _watcher = new FileSystemWatcher(_folder, "*.*") { EnableRaisingEvents = true };

    //none of these events are triggered in Azure
    _watcher.Created += (sender, args) =>
        _log.Debug($"Watcher triggered `Created`, `{args.FullPath}`");
    _watcher.Deleted += (sender, args) =>
        _log.Debug($"Watcher triggered `Deleted`, `{args.FullPath}`");
    _watcher.Renamed += (sender, args) =>
        _log.Debug($"Watcher triggered `Renamed`, `{args.OldFullPath}` to `{args.FullPath}`");
    _watcher.Error += (sender, args) =>
        _log.Debug($"Watcher triggered `Error`, `{args.GetException().ToString()}`");
    _watcher.Disposed += (sender, args) =>
        _log.Debug($"Watcher triggered `Disposed`");

    //Changed is the only event that gets triggered in Azure
    _watcher.Changed += (sender, args) =>
    {
        if (System.IO.File.Exists(args.FullPath))
            _log.Debug($"Watcher triggered `Changed (created)`, `{args.FullPath}`");
        else
            _log.Debug($"Watcher triggered `Changed (deleted)`, `{args.FullPath}`");
    };

    _log.Debug($"Watcher started for folder `{_folder}`");

    return new HttpStatusCodeResult(HttpStatusCode.Created);
}

现在这对我来说似乎工作得很好:

2019-08-16T08:54:37.419Z    DEBUG     TestProject.TestController
Watcher started for folder `C:\test`

2019-08-16T08:55:16.625Z    DEBUG     TestProject.TestController
Created file `C:\test\moo.txt`

2019-08-16T08:55:16.634Z    DEBUG     TestProject.TestController
Watcher triggered `Changed (created)`, `C:\test\moo.txt`

2019-08-16T08:55:50.096Z    DEBUG     TestProject.TestController
Created file `C:\test\poop.txt`

2019-08-16T08:55:50.101Z    DEBUG     TestProject.TestController
Watcher triggered `Changed (created)`, `C:\test\poop.txt`

关于asp.net - FileSystemWatcher 在 Azure 中不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57509189/

相关文章:

azure - 是否可以在运行时从数据工厂中的 Azure 函数链接服务内部读取参数?

azure - 如何将Azure移动服务添加到Azure资源管理器模板?

azure - 尝试在 Azure 数据工厂中进行 Catch

C# - 预测文件夹删除时的文件系统事件

c# - 如何从 Windows 服务中 FileSystemWatcher 监视的共享文件夹中删除的文件中获取用户名

c# - 在 ASP.NET 中运行 shell 命令

c# - 在回发时保留 html 客户端控制值

c# - 创建新实例或仅设置内部变量

c# - 解决错误 "The ConnectionString property has not been initialized."?

c# - Unity 编辑器的线程安全文件系统观察器