Skip to content

Commit e638756

Browse files
committed
Transient Fault Handling Added
1 parent 4cdac7d commit e638756

File tree

54 files changed

+7117
-28
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+7117
-28
lines changed

.gitignore

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,10 @@
22
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
33
################################################################################
44

5-
/Build/Release
6-
/Source/.vs/EnterpriseLibrary/v15
7-
/Source/Validation Application Block/Validation/obj
8-
/Source/Validation Application Block/Validation/bin
9-
/Source/Policy Injection Application Block/PolicyInjection/obj
10-
/Source/Policy Injection Application Block/PolicyInjection/bin
11-
/Source/Logging Application Block/Logging/obj
12-
/Source/Logging Application Block/Logging/bin
13-
/Source/Exception Handling Application Block/ExceptionHandling/obj
14-
/Source/Exception Handling Application Block/ExceptionHandling/bin
15-
/Source/Data Access Application Block/Data/obj
16-
/Source/Data Access Application Block/Data/bin
17-
/Source/Core/Common/obj
18-
/Source/Core/Common/bin
19-
/Source/Exception Handling Application Block/ExceptionHandling.Logging/obj
20-
/Source/Exception Handling Application Block/ExceptionHandling.Logging/bin
21-
/Source/Logging Application Block/Logging.Database/obj
22-
/Source/Logging Application Block/Logging.Database/bin
23-
/Build/Debug
24-
/Source/Caching Application Block/Caching/obj
25-
/Source/Caching Application Block/Caching/bin
26-
/Source/Caching Application Block/Caching.Cryptography/obj
27-
/Source/Caching Application Block/Caching.Cryptography/bin/Release
28-
/Source/Caching Application Block/Caching.Database/obj
29-
/Source/Caching Application Block/Caching.Database/bin/Release
30-
/Source/Cryptography Application Block/Security.Cryptography/obj
31-
/Source/Cryptography Application Block/Security.Cryptography/bin/Release
5+
/Build
6+
*.suo
327
*.user
8+
bin
9+
obj
10+
packages
11+
.vs

Source/Caching Application Block/Caching.Cryptography/Caching.Cryptography.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@
4848
</EmbeddedResource>
4949
</ItemGroup>
5050

51+
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
52+
<Exec Command="xcopy &quot;$(OutDir)*.*&quot; &quot;$(SolutionDir)..\Build\$(ConfigurationName)\&quot; /s /i /y /d" />
53+
</Target>
54+
5155
</Project>

Source/Caching Application Block/Caching.Database/Caching.Database.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@
4848
</EmbeddedResource>
4949
</ItemGroup>
5050

51+
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
52+
<Exec Command="xcopy &quot;$(OutDir)*.*&quot; &quot;$(SolutionDir)..\Build\$(ConfigurationName)\&quot; /s /i /y /d" />
53+
</Target>
54+
5155
</Project>

Source/Caching Application Block/Caching/Caching.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,8 @@
6060
</EmbeddedResource>
6161
</ItemGroup>
6262

63+
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
64+
<Exec Command="xcopy &quot;$(OutDir)*.*&quot; &quot;$(SolutionDir)..\Build\$(ConfigurationName)\&quot; /s /i /y /d" />
65+
</Target>
66+
6367
</Project>

Source/Cryptography Application Block/Security.Cryptography/Security.Cryptography.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,8 @@
6161
</EmbeddedResource>
6262
</ItemGroup>
6363

64+
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
65+
<Exec Command="xcopy &quot;$(OutDir)*.*&quot; &quot;$(SolutionDir)..\Build\$(ConfigurationName)\&quot; /s /i /y /d" />
66+
</Target>
67+
6468
</Project>

Source/EnterpriseLibrary.sln

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,22 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Caching", "Caching Applicat
3737
EndProject
3838
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Cryptography Application Block", "Cryptography Application Block", "{107DD84A-FF36-46CC-9C1E-7724B0453726}"
3939
EndProject
40-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Security.Cryptography", "Cryptography Application Block\Security.Cryptography\Security.Cryptography.csproj", "{D69C393D-D725-46E1-ADB0-4C37DB96E45B}"
40+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Security.Cryptography", "Cryptography Application Block\Security.Cryptography\Security.Cryptography.csproj", "{D69C393D-D725-46E1-ADB0-4C37DB96E45B}"
4141
EndProject
4242
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Caching.Cryptography", "Caching Application Block\Caching.Cryptography\Caching.Cryptography.csproj", "{B7BAB5C1-8089-4180-BF15-F13D8A8F3B51}"
4343
EndProject
4444
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Caching.Database", "Caching Application Block\Caching.Database\Caching.Database.csproj", "{EBA392F8-54F1-4B25-AE24-54F2EC92E3C5}"
4545
EndProject
46+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Transient Fault Handling Application Block", "Transient Fault Handling Application Block", "{66B030BD-13B8-45D6-9AC8-35EAEC7165F4}"
47+
EndProject
48+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TransientFaultHandling", "Transient Fault Handling Application Block\TransientFaultHandling\TransientFaultHandling.csproj", "{19BEFF33-A451-4319-AA64-8EB3B8EF5641}"
49+
EndProject
50+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TransientFaultHandling.Caching", "Transient Fault Handling Application Block\TransientFaultHandling.Caching\TransientFaultHandling.Caching.csproj", "{ACFAE28D-163B-448C-ABC4-BA57A02556F9}"
51+
EndProject
52+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TransientFaultHandling.Data", "Transient Fault Handling Application Block\TransientFaultHandling.Data\TransientFaultHandling.Data.csproj", "{B92C5C34-8685-4743-89E9-27FD034B7AC2}"
53+
EndProject
54+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TransientFaultHandling.Configuration", "Transient Fault Handling Application Block\TransientFaultHandling.Configuration\TransientFaultHandling.Configuration.csproj", "{09242E89-03A0-42FB-9D66-D9C7DE290D3D}"
55+
EndProject
4656
Global
4757
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4858
Debug|Any CPU = Debug|Any CPU
@@ -97,6 +107,22 @@ Global
97107
{EBA392F8-54F1-4B25-AE24-54F2EC92E3C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
98108
{EBA392F8-54F1-4B25-AE24-54F2EC92E3C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
99109
{EBA392F8-54F1-4B25-AE24-54F2EC92E3C5}.Release|Any CPU.Build.0 = Release|Any CPU
110+
{19BEFF33-A451-4319-AA64-8EB3B8EF5641}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
111+
{19BEFF33-A451-4319-AA64-8EB3B8EF5641}.Debug|Any CPU.Build.0 = Debug|Any CPU
112+
{19BEFF33-A451-4319-AA64-8EB3B8EF5641}.Release|Any CPU.ActiveCfg = Release|Any CPU
113+
{19BEFF33-A451-4319-AA64-8EB3B8EF5641}.Release|Any CPU.Build.0 = Release|Any CPU
114+
{ACFAE28D-163B-448C-ABC4-BA57A02556F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
115+
{ACFAE28D-163B-448C-ABC4-BA57A02556F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
116+
{ACFAE28D-163B-448C-ABC4-BA57A02556F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
117+
{ACFAE28D-163B-448C-ABC4-BA57A02556F9}.Release|Any CPU.Build.0 = Release|Any CPU
118+
{B92C5C34-8685-4743-89E9-27FD034B7AC2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
119+
{B92C5C34-8685-4743-89E9-27FD034B7AC2}.Debug|Any CPU.Build.0 = Debug|Any CPU
120+
{B92C5C34-8685-4743-89E9-27FD034B7AC2}.Release|Any CPU.ActiveCfg = Release|Any CPU
121+
{B92C5C34-8685-4743-89E9-27FD034B7AC2}.Release|Any CPU.Build.0 = Release|Any CPU
122+
{09242E89-03A0-42FB-9D66-D9C7DE290D3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
123+
{09242E89-03A0-42FB-9D66-D9C7DE290D3D}.Debug|Any CPU.Build.0 = Debug|Any CPU
124+
{09242E89-03A0-42FB-9D66-D9C7DE290D3D}.Release|Any CPU.ActiveCfg = Release|Any CPU
125+
{09242E89-03A0-42FB-9D66-D9C7DE290D3D}.Release|Any CPU.Build.0 = Release|Any CPU
100126
EndGlobalSection
101127
GlobalSection(SolutionProperties) = preSolution
102128
HideSolutionNode = FALSE
@@ -114,6 +140,10 @@ Global
114140
{D69C393D-D725-46E1-ADB0-4C37DB96E45B} = {107DD84A-FF36-46CC-9C1E-7724B0453726}
115141
{B7BAB5C1-8089-4180-BF15-F13D8A8F3B51} = {CDE9DAC4-B52B-466A-9915-B61641703D56}
116142
{EBA392F8-54F1-4B25-AE24-54F2EC92E3C5} = {CDE9DAC4-B52B-466A-9915-B61641703D56}
143+
{19BEFF33-A451-4319-AA64-8EB3B8EF5641} = {66B030BD-13B8-45D6-9AC8-35EAEC7165F4}
144+
{ACFAE28D-163B-448C-ABC4-BA57A02556F9} = {66B030BD-13B8-45D6-9AC8-35EAEC7165F4}
145+
{B92C5C34-8685-4743-89E9-27FD034B7AC2} = {66B030BD-13B8-45D6-9AC8-35EAEC7165F4}
146+
{09242E89-03A0-42FB-9D66-D9C7DE290D3D} = {66B030BD-13B8-45D6-9AC8-35EAEC7165F4}
117147
EndGlobalSection
118148
GlobalSection(ExtensibilityGlobals) = postSolution
119149
SolutionGuid = {D33D36F1-68F5-4C14-BBC4-32E07BACF8AE}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#region license
2+
// ==============================================================================
3+
// Microsoft patterns & practices Enterprise Library
4+
// Transient Fault Handling Application Block
5+
// ==============================================================================
6+
// Copyright © Microsoft Corporation. All rights reserved.
7+
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
8+
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
9+
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
10+
// FITNESS FOR A PARTICULAR PURPOSE.
11+
// ==============================================================================
12+
#endregion
13+
14+
using System;
15+
using System.Net.Sockets;
16+
using System.ServiceModel;
17+
18+
using Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.Caching;
19+
20+
namespace Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling
21+
{
22+
/// <summary>
23+
/// Provides the transient error detection logic that can recognize transient faults when dealing with Windows Azure Caching.
24+
/// </summary>
25+
public class CacheTransientErrorDetectionStrategy : ITransientErrorDetectionStrategy
26+
{
27+
/// <summary>
28+
/// Checks whether or not the specified exception belongs to a category of transient failures that can be compensated by a retry.
29+
/// </summary>
30+
/// <param name="ex">The exception object to be verified.</param>
31+
/// <returns>true if the specified exception belongs to the category of transient errors; otherwise, false.</returns>
32+
public bool IsTransient(Exception ex)
33+
{
34+
if (ex == null)
35+
{
36+
return false;
37+
}
38+
39+
if (ex is ServerTooBusyException)
40+
{
41+
return true;
42+
}
43+
44+
var socketFault = ex as SocketException;
45+
if (socketFault != null)
46+
{
47+
return socketFault.SocketErrorCode == SocketError.TimedOut;
48+
}
49+
50+
var dataCacheExceptionResult = DataCacheExceptionChecker.IsTransientDataCacheException(ex);
51+
if (dataCacheExceptionResult.HasValue)
52+
{
53+
return dataCacheExceptionResult.Value;
54+
}
55+
56+
// Some transient exceptions may be wrapped into an outer exception, hence we should also inspect any inner exceptions.
57+
if (ex != null && ex.InnerException != null)
58+
{
59+
return this.IsTransient(ex.InnerException);
60+
}
61+
62+
return false;
63+
}
64+
}
65+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#region license
2+
// ==============================================================================
3+
// Microsoft patterns & practices Enterprise Library
4+
// Transient Fault Handling Application Block
5+
// ==============================================================================
6+
// Copyright © Microsoft Corporation. All rights reserved.
7+
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
8+
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
9+
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
10+
// FITNESS FOR A PARTICULAR PURPOSE.
11+
// ==============================================================================
12+
#endregion
13+
14+
using System;
15+
using System.Globalization;
16+
using System.Linq;
17+
using System.Linq.Expressions;
18+
using System.Reflection;
19+
20+
using Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.Properties;
21+
22+
namespace Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.Caching
23+
{
24+
/// <summary>
25+
/// Checks whether an <see cref="Exception"/> is of the Microsoft.ApplicationServer.Caching.DataCacheException type,
26+
/// and if so, checks the error code and status code to determine if it is transient.
27+
/// This method dynamically discovers the type, to avoid a hard reference to the
28+
/// Microsoft.ApplicationServer.Caching.Core assembly (either version 1.0.0.0 or 101.0.0.0).
29+
/// </summary>
30+
internal static class DataCacheExceptionChecker
31+
{
32+
// PublicKeyToken = 31bf3856ad364e35
33+
private static readonly byte[] CachePublicKeyToken = new byte[] { 0x31, 0xbf, 0x38, 0x56, 0xad, 0x36, 0x4e, 0x35 };
34+
35+
private static Type DataCacheExceptionType;
36+
private static Func<Exception, int> GetErrorCode;
37+
38+
private static int[] errorCodes = new int[]
39+
{
40+
16, //DataCacheErrorCode.ConnectionTerminated,
41+
17, //DataCacheErrorCode.RetryLater,
42+
18, //DataCacheErrorCode.Timeout,
43+
17016, //DataCacheErrorCode.ServiceAccessError,
44+
};
45+
46+
/// <summary>
47+
/// Checks whether <paramref name="ex"/> is a transient DataCacheException.
48+
/// </summary>
49+
/// <param name="ex">The exception object to be verified.</param>
50+
/// <returns>
51+
/// <see langword="true"/> if the exception is of type DataCacheException and is transient;
52+
/// <see langword="false"/> if the exception is of type DataCacheException and is not transient;
53+
/// <see langword="null"/> if the exception is not of type DataCacheException.
54+
/// </returns>
55+
public static bool? IsTransientDataCacheException(Exception ex)
56+
{
57+
if (ex == null)
58+
{
59+
return null;
60+
}
61+
62+
if (DataCacheExceptionType == null)
63+
{
64+
var exceptionType = ex.GetType();
65+
if (exceptionType.FullName == "Microsoft.ApplicationServer.Caching.DataCacheException")
66+
{
67+
InitializeTypeAccesors(exceptionType);
68+
}
69+
}
70+
71+
if (DataCacheExceptionType != null && DataCacheExceptionType.IsInstanceOfType(ex))
72+
{
73+
return errorCodes.Contains(GetErrorCode(ex));
74+
}
75+
76+
return null;
77+
}
78+
79+
private static void InitializeTypeAccesors(Type type)
80+
{
81+
// this creates the following function from the reflected type (DataCacheException):
82+
//GetErrorCode = i => ((DataCacheException)i).ErrorCode;
83+
84+
if (DataCacheExceptionType == null)
85+
{
86+
CheckIsCacheAssembly(type.Assembly);
87+
88+
var errorCodeProperty = type.GetProperty("ErrorCode");
89+
if (errorCodeProperty == null || errorCodeProperty.PropertyType != typeof(int))
90+
throw new InvalidOperationException(Resources.TypeMismatchException);
91+
92+
var parameter = Expression.Parameter(typeof(Exception), "i");
93+
94+
// (DataCacheException)i
95+
var cast = Expression.TypeAs(parameter, type);
96+
97+
// ((DataCacheException)i).ErrorCode
98+
GetErrorCode = Expression.Lambda<Func<Exception, int>>(
99+
Expression.Property(
100+
cast,
101+
errorCodeProperty),
102+
parameter).Compile();
103+
104+
DataCacheExceptionType = type;
105+
}
106+
}
107+
108+
private static void CheckIsCacheAssembly(Assembly assembly)
109+
{
110+
// should we also filter by version to support only 1.0.0.0 and 101.0.0.0?
111+
var assemblyName = assembly.GetName();
112+
if (assemblyName != null && assemblyName.Name == "Microsoft.ApplicationServer.Caching.Core")
113+
{
114+
var token = assemblyName.GetPublicKeyToken();
115+
if (token != null && CachePublicKeyToken.SequenceEqual(token))
116+
{
117+
return;
118+
}
119+
}
120+
121+
throw new InvalidOperationException(
122+
string.Format(CultureInfo.CurrentCulture, Resources.AssemblyMismatchException, assemblyName));
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)