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.