Last post Jan 31, 2011 11:54 AM by atconway
Jan 28, 2011 05:28 PM|ThumbDog|LINK
I have a situation I'm trying to resolve and I'm looking for suggestions on how best to accomplish the following:
The sequence of events is as follows:
1. A piece of software running locally on my pc periodically uploads (approximately every three minutes) to my webserver an xml file comprised of six fields. The file size is < 1K.
1a. The file name is always the same. ie. filename.xml is uploaded. Three minutes later, filename.xml is uploaded again, this time with different contents. I have no control over renaming this file.
2. A chunk of code that parses the xml and inserts the six fields into a database
3. Deletes the xml file
4. Waits for the next xml file to appear in the folder
5. Goto 2.
The locally running software uploads the file without issue. So far, I'm able to parse the xml, do the database insert and delete the file manually by refreshing the browser window and subsequently running the code in the .cshtml file. What I'm having a
hard time conceptualizing is what the best way is to set up an automated event/method that watches the folder and when a new file appears, write it to the db without user intervention. Also, where would I put this code? _AppStart? Do I need to start a FileSystemWatcher
event on the server somehow? I'm kinda stumped as to how best to proceed.
Any insights greatly appreciated.
Jan 28, 2011 09:55 PM|SGWellens|LINK
One method would be to write a program and use the Task Scheduler to run it. Then it is simple and you can easily test it and run it manually every few minutes.
You could also write a Windows Service to do it with a FileSystemWatcher.
There are giant hacks to make an asp application do repeated work...but they are hacks.
Jan 29, 2011 08:56 PM|ThumbDog|LINK
Thanks for the prompt reply SGWellens. I'll be sure and check out the links you provided.
Jan 31, 2011 11:54 AM|atconway|LINK
What I'm having a hard time conceptualizing is what the best way is to set up an automated event/method that watches the folder and when a new file appears, write it to the db without user intervention. Also, where would I put this code? _AppStart? Do I need
to start a FileSystemWatcher event on the server somehow?
I have actually done a lot of processing like this over the years, and your best bet is to create a Windows Service that conatins a FileSystemWatcher object that is wired up to the directory in question. When the new XML file (or any file for that matter)
is dropped into that directory, the event in the Windows Service will fire, and you will be able to run your .NET code. With the FileSystemWatcher you don't need to worry about polling on interval to check for new files, etc as the work is done internally
to fire the event once a new file appears. It should work perfectly for your situation.
Now as far as encorporating this into a Windows Service; no problem. If you have not created one before they are not intemidating at all and the link below shows you step by step on how to create and install the service. If you are stricly just learning
how all of the pieces work together you could place the code to wire up the FileSystemWatcher in the OnStart() method of the Windows Service, and then add the event OnChanged() as well. The code example in the MSDN link above will help you. You can go back
later if you want once you understand and refactor the code from OnStart() into another method (like the MSDN link that created a Run() method).
To create and install a Windows Service, please check out the link below:
Walkthrough: Creating a Windows Service Application in the Component Designer:
Hope this helps!