asp.net-core - AspNetCore 2.1 BackgroundService\IHostedService 中的 FileSystemWatcher

标签 asp.net-core filesystemwatcher asp.net-core-2.1

我正在尝试使用 BackgroundService 的实现在 AspNet Core 2.1 应用程序中。我在 ExecuteAsync 中创建了一个 FileSystemWatcher并链接相关的事件,但是,fsw 事件要么从未触发(无法访问?已经处理?),要么我做错了异步或范围困惑。我似乎无法弄清楚。以下是相关代码。

public class FSWImpl : BackgroundService
{
    private readonly IHostingEnvironment _env;
    private readonly ILogger<LiftAndShift> _logger;
    private FileSystemWatcher _fsw;

    public LiftAndShift(IHostingEnvironment env, ILogger<FSWImpl> logger)
    {
        _env = env;
        _logger = logger;
    }

    protected override Task ExecuteAsync(CancellationToken stoppingToken)
    {
        _logger.LogInformation("Creating new FSW");
        var path = Path.Combine(_env.ContentRootPath, "WebData");
        _fsw = new FileSystemWatcher(path,"*.json");
        _fsw.Created += _fsw_Created;
        _fsw.Changed += _fsw_Changed;
        _fsw.Renamed += _fsw_Renamed;
        _fsw.Error += _fsw_Error;
        return Task.CompletedTask;
    }

    private void _fsw_Error(object sender, ErrorEventArgs e) => _logger.LogInformation("File error");
    private void _fsw_Renamed(object sender, RenamedEventArgs e) => _logger.LogInformation("File Renamed");
    private void _fsw_Changed(object sender, FileSystemEventArgs e) => _logger.LogInformation("File changed");
    private void _fsw_Created(object sender, FileSystemEventArgs e) => _logger.LogInformation("File created");
}  

我在启动时将此服务注册为 services.AddHostedService<FSWImpl>();

最佳答案

用于启用 FileSystemWatcher ,您需要设置EnableRaisingEventsTrue .

演示代码:

        protected override Task ExecuteAsync(CancellationToken stoppingToken)
    {
        _logger.LogInformation("Creating new FSW");
        var path = Path.Combine(_env.ContentRootPath, "WebData");
        _fsw = new FileSystemWatcher(path, "*.json");
        _fsw.Created += _fsw_Created;
        _fsw.Changed += _fsw_Changed;
        _fsw.Renamed += _fsw_Renamed;
        _fsw.Error += _fsw_Error;
        _fsw.EnableRaisingEvents = true;
        return Task.CompletedTask;
    }
FileSystemWatcher.cs
    //
    // Summary:
    //     Gets or sets a value indicating whether the component is enabled.
    //
    // Returns:
    //     true if the component is enabled; otherwise, false. The default is false. If
    //     you are using the component on a designer in Visual Studio 2005, the default
    //     is true.
    //
    // Exceptions:
    //   T:System.ObjectDisposedException:
    //     The System.IO.FileSystemWatcher object has been disposed.
    //
    //   T:System.PlatformNotSupportedException:
    //     The current operating system is not Microsoft Windows NT or later.
    //
    //   T:System.IO.FileNotFoundException:
    //     The directory specified in System.IO.FileSystemWatcher.Path could not be found.
    //
    //   T:System.ArgumentException:
    //     System.IO.FileSystemWatcher.Path has not been set or is invalid.
    public bool EnableRaisingEvents { get; set; }

关于asp.net-core - AspNetCore 2.1 BackgroundService\IHostedService 中的 FileSystemWatcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53637364/

相关文章:

entity-framework - 在 AutoMapper 8.0 中缺少 ResolveUsing

c# - 如何在 IActionFilter Asp.net core 中读取 OnActionExecuting(ActionExecutingContext context) 中的请求体

c# - 使用 asp.net core 2.1 下载角度 8 的文件总是给出损坏的文件

c# - 如何在同一网址上使用 [FromBody] 和 [FromForm]?

asp.net-core - 带两个参数的 MVC6 属性路由

asp.net-core - 部署 ASP.NET Core 1.1.0 和 Entity Framework Core 1.1 : Unable to load assembly system. Collections.Immutable,版本=1.1.37.0

multithreading - 使用 FileSystemMonitoring 读取 app.config 中的更改并实时写入 app.config

c# - 如何覆盖未标记为虚拟的事件处理程序?

c# - 使用 FileWatcher 检测文件夹中是否创建了新文件

c# - 将 B2C 回复 URL 从 "signin-oidc"更改为其他内容不起作用