By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,551 Members | 2,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,551 IT Pros & Developers. It's quick & easy.

Problems with Filesystemwatcher and Web Service

P: n/a
I am trying to find out if there is a way to tell if there is already a
filesystemwatcher (created by a webservice) monitoring a folder.

I have a webservice that creates a filesystemwatcher, monitors a folder and
then returns the contents of the new/changed files. However, if the client
app loses connection to the webservice without closing the filewatcher, and
then reconnects (and thus creates a new watcher), I believe I end up with two
watchers which is causing a few problems.

Is there anyway to tell if a filesystemwatcher already exists and is
watching the folder? Or that the webservice that created the first watcher is
still running?


Sep 20 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

Web Service is a stateless remote call, it does not have the session
feature by default unless you set EnableSession property of the
WebMethodAttribute. Even you enabled the session, you still have to
explicitly add the object references into the Session collection to use it.
Do you enable session in your Web Service? If so, do you add
FileSystemWatcher object into the Session collection?

Regarding your question, I do think it should not be a problem. Whether
your client is disconnected or killed abnormally, the server-side Web
Method will exit(this is the key point) and the Web Service processing
thread will return to the ThreadPool. So, all the stack references in the
Web Method will be released automatically by the stack unwind. Since the
references to the FileSystemWatcher disappeared, the FileSystemWatcher
object will be collected by the GC in future or immediately based on the GC
algorithm. So, once the WebMethod exited, the FileSystemWatcher objects are
guaranteed to be collected by GC, no leak will occur.

If you wanted to ensure the FileSystemWatcher object is collected
immediately, I would recommend you to wrap the object using C# "using"
keyword or place the FileSystemWatcher.Dispose() method calling in a
try...finally clause. This will ensure the FileSystemWatcher.Dispose() when
the WebMethod exits. FileSystemWatcher.Dispose() internally stops the
directory monitoring.

If you are not familiar with C# "using" keyword, please refer to the MSDN
link below, this is called C# deterministic cleanup:
http://msdn2.microsoft.com/en-us/lib...02(VS.80).aspx

Finally, I am curious about how do you use FileSystemWatcher in a
WebMethod? Based on my knowledge, FileSystemWatcher will monitor a
directory consistently, but WebMethod is one-off stateless calling from
client, FileSystemWatcher will be collected after the call. Do you spawn a
second worker thread to use FileSystemWatcher?

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 21 '07 #2

P: n/a
Hi,

Have you reviewed my reply to you? Does it make sense to you? If you still
need any help, please feel free to feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Sep 25 '07 #3

P: n/a
Hi,

Thanks for your reply. It made sense and helped me make several steps forward.

Thanks again
Sep 25 '07 #4

P: n/a
Glad to see it helps. If you need further help or have any other concern,
please feel free to tell me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Sep 25 '07 #5

P: n/a
Hi

I'm working on a VB web service at the moment and want to use the
Filesystemwatcher to monitor a directory but like Jeffrey said how do you get
it to work with the web service which is stateless???

Thanks in advance, Matt

"John S" wrote:
I am trying to find out if there is a way to tell if there is already a
filesystemwatcher (created by a webservice) monitoring a folder.

I have a webservice that creates a filesystemwatcher, monitors a folder and
then returns the contents of the new/changed files. However, if the client
app loses connection to the webservice without closing the filewatcher, and
then reconnects (and thus creates a new watcher), I believe I end up with two
watchers which is causing a few problems.

Is there anyway to tell if a filesystemwatcher already exists and is
watching the folder? Or that the webservice that created the first watcher is
still running?

Oct 4 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.