I'm working on a custom server based on HttpListener
. It's working fairly well, until I try to use a FileSystemWatcher
to make it able to respond to changing script content without needing to reboot the server. It doesn't appear to ever fire.
The relevant code looks like this:
foreach (var script in Directory.EnumerateFiles(folder, mask))
{
var filename = Path.GetFileNameWithoutExtension(script);
var compileResult = scriptCompiler.CompileScript(script);
if (compileResult.Errors.Count > 0)
throw new Exception(compileResult.Errors.ToString());
AddScriptType(compileResult.GeneratedAssembly.GetType(filename));
}
this._watcher = new FileSystemWatcher(folder, mask);
_watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
var changed = (s, e) => RebuildScript(scriptCompiler, e);
_watcher.Changed += changed;
_watcher.Created += changed;
_watcher.Deleted += this.ScriptDeleted;
_watcher.EnableRaisingEvents = true;
This seems pretty straightforward, but RebuildScript
never gets called if I go in and change one of the script files.
I can't help but wonder if HttpListener
is interfering with some messaging architecture needed to make this work properly. The server runs on the main thread, in an infinite loop of waiting for an HTTP request, processing it, and returning the response. Could that be causing FileSystemWatcher
to never receive events from the OS? If not, how do I figure out what's causing this to fail?