The Problem
We are occasionally getting a WebException that, as hard as we try we can not seem to catch. This results in an error showing up in the event log on the WebServer.
Details
We have an ASP.Net application that uses several webservices. One of the webservice methods can take a long time to run. To address this we use caching. When the cache times out, we use the callback featue of ASP.Net Cacheing (CacheItemRemovedCallback) to recall the webservice and repopulate the cache. See code example below
Our problems starts when, occasionally, the webservice times out. When this happens it throws a WebException. We put a Try/Catch block round our code, and just catch and log exceptions. However, for some reason, this does not seem to catch the WebException and I can not work out why.
I have seen some weird behavior in the past with ThreadAbort exceptions not getting caught as expected. I wonder if this could be related? Also, another thing we do is create a new HTTPContext in order to access the Cache. This works fine, and is needed because the callback is not in the main Request thread. I wonder if this could also be related
Any advice welcome!
Cheers James
Code Example
Public Shared Sub CacheCallbackMethod()
Try
If HttpContext.Current Is Nothing Then
HttpContext.Current = New HttpContext(New Hosting.SimpleWorkerRequest(String.Empty, String.Empty, New System.IO.StringWriter()))
End If
[Code to fetch data]
Cache.Insert(key, obj, Nothing, Nothing, SlidingExpTimeSpan, Priority, CallbackMethod)
Catch ex As Exception
End Try
End Function