Got more questions? Find advice on: SQL | XML | Regular Expressions | Windows
in Search
Welcome to AspAdvice Sign in | Join | Help

Eric Madariaga's Blog

Building ASP.NET solutions with reusable software components

Using the FileSystemWatcher from ASP.NET

A recent ASP.NET project that I am working on requires that the web application monitor a directory for new files, and if new files are found, process them in some way. 

Unfortunately the application is not accessed through its web interface very often and as a result it cannot poll the directory when pages are requested, sessions are created, etc. The web interface is actually used mostly for configuration of the application.

We have decided to keep a FileSystemWatcher object alive by adding it to the ASP.NET application object. In the global.asax we do the following:

private FileSystemWatcher fsw;

protected void Application_Start(Object sender, EventArgs e)
{
	string monitorPath = this.Context.Server.MapPath("incomming/");

	Application.Add("watcher", new System.IO.FileSystemWatcher(monitorPath));
	fsw  = (FileSystemWatcher)Application["watcher"];
	fsw.EnableRaisingEvents = true;
	fsw.IncludeSubdirectories = false;

	fsw.Changed += new System.IO.FileSystemEventHandler(fsw_Changed);
	fsw.Created += new System.IO.FileSystemEventHandler(fsw_Created);
}


This seems to work so far.  Whenever a new file is dropped into the incoming/ directory, the FileSystemWatcher events fire and we are able to process incoming files.

A colleague of mine said that he tried this on another web application and that events from the FileSystemWatcher stopped firing at some point. Has anyone run into any problems using the FileSystemWatcher this way from an ASP.NET application?

Published Friday, August 05, 2005 10:40 AM by ericm

Comments

 

ericm said:

Web applications do end, typically when there is no session that no longer exists (afterall if there is no session then there is no reason to have the app running, or so goes the logic anyhow). You can increase the session timeout (not my recommendation though), but the better idea is to simply make sure you always have an active session. The following article/code attempts to do just that -- to "keep-alive" your web app:

http://authors.aspalliance.com/PaulWilson/Articles/?id=12

Even then, there are still things that can/will cause your web app to eventually stop, and maybe not recycle itself -- and those things tend to be out of your control, so you're probably better off implementing an external service to periodically (every 15 minutes) hit your site if its critical to have it up always. Of course, if you control the server, its also better to just do such work in a service in the first place.
August 5, 2005 1:40 PM
 

ericm said:

Thanks Paul. In this situation we are deploying a distributable ASP.NET application that will run in several different containers (IIS, a custom ASP.NET webserver, casini, etc.). Since it's not really a site, and we won't always have control of the application container it makes it hard to implement a service to do this.

Do you know if there is a way to execute an application without hitting it though a web request? We have some issues where it will be impossible to find out our current URL and we'd like to start the web application programmatically.
August 10, 2005 10:39 AM
Anonymous comments are disabled