简体   繁体   中英

Try/Catch didn't catch WebException

I built a .NET Mono application that uploads text files every minute to a server. In my opinion, the best way to avoid crashing the application is using a try-catch around the upload code, so unexpected errors are caught.

And always it runs correctly for about 2 days, and after that, it suddenly crashes. I'm looking for the cause already for a few weeks but can't find it.

The error I got is:

Unhandled Exception: System.Net.WebException: Request aborted at System.Net.FtpWebRequest.CheckIfAborted() [0x00000] in :0 at System.Net.FtpWebRequest.set_Sate (RequestState value) [0x00000] in :0 at System.Net.FtpWebRequest.ProcessRequest(RequestState value) [0x00000] in <filename:0 unknown>: at System.Threading.Thread.StartUnsafe () [0x00000] in <filename:0 unknown>:

There's probably nothing wrong with your television set, I mean, your code.

The problem is Mono's FtpWebRequest implementation which is not very robust (poorly handles timeouts at various stages of FTP conversation). I did an analysis of similar case and posted my findings there:

Mono for Android / MonoTouch System.Net.WebException: Request aborted

Your case, while not identical, points to the common "bag" of issues with Mono's FtpWebRequest.

Such errors are really hard to find. You said that you run this function in a thread. So I guess it's called from the while loop and than Thread.Sleep(1000*60); or as fire and forget . The reason could be, that the function is not finished before the next call or FtpWebRequest is still not closed and it is doesn't matter, that it runs at different context.

I had similar problems and here is what you can try:

1) Make FtpWebRequest request global and check if it still alive before the function call.

2) Call the garbage collector at the function end:

GC.Collect();
GC.WaitForPendingFinalizers();

Please let me know if that works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM