nat*_*nho 15 c# linux filesystemwatcher docker .net-core
鉴于程序:
using System;
using System.IO;
namespace fsw_bug_poc
{
class Program
{
private static FileSystemWatcher _fileSystemWatcher;
static void Main(string[] args)
{
_fileSystemWatcher = new FileSystemWatcher("Watched", "*.*");
_fileSystemWatcher.Changed += Notify;
_fileSystemWatcher.Created += Notify;
_fileSystemWatcher.Deleted += Notify;
_fileSystemWatcher.Renamed += Notify;
_fileSystemWatcher.IncludeSubdirectories = true;
_fileSystemWatcher.EnableRaisingEvents = true;
Console.ReadKey(false);
}
private static void Notify(object sender, FileSystemEventArgs e)
{
Console.WriteLine($"{e.FullPath} {e.ChangeType}");
}
}
}
Run Code Online (Sandbox Code Playgroud)
Dockerfile:
FROM mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["fsw-bug-poc.csproj", ""]
RUN dotnet restore "fsw-bug-poc.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "fsw-bug-poc.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "fsw-bug-poc.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENV DOTNET_USE_POLLING_FILE_WATCHER=true
RUN mkdir -p /app/Watched
VOLUME /app/Watched
ENTRYPOINT ["dotnet", "fsw-bug-poc.dll"]
Run Code Online (Sandbox Code Playgroud)
根据这个链接添加ENV DOTNET_USE_POLLING_FILE_WATCHER=true到 Dockerfile 修复了 FileSystemWatcher 在容器内不起作用。
即使有了这个修复,FileSystemWatcher 在 Windows 上运行 Linux 容器并将共享驱动程序安装到卷时也不会工作:
docker build -t fsw-bug-poc .
docker run -it --rm -v C:\Shared:/app/Watched fsw-bug-poc
Run Code Online (Sandbox Code Playgroud)
修改容器内的文件:
修改共享卷文件夹中的文件:
没发生什么事!!
有人可以解释发生了什么吗?FileSystemWatcher 正在使用轮询策略,所以它应该以相同的方式工作,不是吗?
nat*_*nho 14
切换到PhysicalFileProvider .Watch 完成了这项工作。对于文件系统监视策略来说,它似乎是一种更便携的实现。
当前的实现PhysicalFileProvider支持DOTNET_USE_POLLING_FILE_WATCHER环境变量。我在FileSystemWatcher实现中找不到它的任何参考。
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
using System;
using System.IO;
namespace fsw_bug_poc
{
class Program
{
private static PhysicalFileProvider _fileProvider;
private static IChangeToken _fileChangeToken;
static void Main(string[] args)
{
_fileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "."));
WatchForFileChanges();
Console.ReadKey(false);
}
private static void WatchForFileChanges()
{
_fileChangeToken = _fileProvider.Watch("*.*");
_fileChangeToken.RegisterChangeCallback(Notify, default);
}
private static void Notify(object state)
{
Console.WriteLine("File change detected");
WatchForFileChanges();
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5414 次 |
| 最近记录: |