Skip to content

Instantly share code, notes, and snippets.

@haf
Created February 20, 2012 13:12
Show Gist options
  • Save haf/1869125 to your computer and use it in GitHub Desktop.
Save haf/1869125 to your computer and use it in GitHub Desktop.
Microsoft Exception Handling
// SecuritySafeCritical because it references MessageLockLostException, MessagingCommunicationException,
// MessagingEntityAlreadyExistsException, MessagingEntityNotFoundException, ServerBusyException, ServerErrorException
[SecuritySafeCritical]
private static bool CheckIsTransient(Exception ex)
{
if (ex is FaultException)
{
return false;
}
else if (ex is MessagingEntityNotFoundException)
{
return false;
}
else if (ex is MessagingEntityAlreadyExistsException)
{
return false;
}
else if (ex is MessageLockLostException)
{
return false;
}
else if (ex is CommunicationObjectFaultedException)
{
return false;
}
else if (ex is MessagingCommunicationException)
{
return true;
}
else if (ex is TimeoutException)
{
return true;
}
else if (ex is WebException)
{
return true;
}
else if (ex is SecurityTokenException)
{
return true;
}
else if (ex is ServerTooBusyException)
{
return true;
}
else if (ex is ServerBusyException)
{
return true;
}
else if (ex is ServerErrorException)
{
return true;
}
else if (ex is ProtocolException)
{
return true;
}
else if (ex is EndpointNotFoundException)
{
// This exception may occur when a listener and a consumer are being
// initialized out of sync (e.g. consumer is reaching to a listener that
// is still in the process of setting up the Service Host).
return true;
}
else if (ex is CommunicationException)
{
return true;
}
else if (ex is SocketException)
{
var socketFault = ex as SocketException;
return socketFault.SocketErrorCode == SocketError.TimedOut;
}
else if (ex is UnauthorizedAccessException)
{
// Need to provide some resilience against the following fault that was seen a few times:
// System.UnauthorizedAccessException: The token provider was unable to provide a security token while accessing 'https://mysbns-sb.accesscontrol.windows.net/WRAPv0.9/'.
// Token provider returned message: 'Error:Code:500:SubCode:T9002:Detail:An internal network error occured. Please try again.'.
// System.IdentityModel.Tokens.SecurityTokenException: The token provider was unable to provide a security token while accessing 'https://mysbns-sb.accesscontrol.windows.net/WRAPv0.9/'.
// Token provider returned message: 'Error:Code:500:SubCode:T9002:Detail:An internal network error occured. Please try again.'.
// System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
var match = acsErrorCodeRegEx.Match(ex.Message);
var errorCode = 0;
if (match.Success && match.Groups.Count > 1 && int.TryParse(match.Groups[1].Value, out errorCode))
{
return errorCode == (int)HttpStatusCode.InternalServerError;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment