On occasion, our webservices stop responding without warning (They just hang it it takes a while for the App Pool to be recycled). When I went searching on the subject I found this article by Michael Freidgeim. His solution is to retry the webservice call under certain conditions.
I have come up with a slight variation on this solution that rather than call the same webservice in the event of an Exception, calls another instance of the webservice (requires multiple instances of the same webservice to be hosted somewhere, can even be on the same machine in different App Pool).
public static class WSHelper
{
public delegate void CallWebServiceDelegateWithUrl(string url);
private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static void RetryWebServiceCall(string[] urls, CallWebServiceDelegateWithUrl wsDelegate)
{
// Retry webservice call when special conditions are met (see below)
for (int i = 0; i < urls.Length; i++)
{
try
{
//Execute the delegate
wsDelegate(urls[i]);
break;
}
catch (Exception ex)
{
if (ex is WebException)
{
if (i < urls.Length - 1)
{
_log.Debug(”Attempt ” + (i + 1) + ” failed. - ” + urls[i], ex);
continue; //try the next URL in the list
}
_log.Error(ex);
}
throw;
}
}
}
}
Sample usage
WSHelper.RetryWebServiceCall(urlList, delegate(string url)
{
using(Service service = new Service())
{
service.Url = url;
response = service.Execure(request);
}
});
Just a few notes
- All exceptions are caught, so they can be logged, only the type of WebException is dealt with
- urlList is a list of all available webservice URL’s
- Using Log4Net
My overall plan is to migrate our webservices to WCF, so hopefully that will resolve some of the issues we’re having (The webservice was developed by an external company, and the quality is not as I’d like).
Hope this helps some people out, if you have any comments/suggestions or alternate approaches, I’d love to hear them.
Filed under: Programming | Leave a Comment
Tags: .net, c#, webservice exception, webservice redundancy, webservice retry, webservices
Search
-
Blogroll
Categories
- Programming (1)