Skip to content

Commit 3fefaf7

Browse files
committed
Use the old security rule for .NET Sandard build, so that it can be used by .NET Framework projects. Fix #5.
1 parent 58713ab commit 3fefaf7

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
namespace Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.Bvt.Tests.Sql;
2+
3+
[TestClass]
4+
public class SqlExceptionTests
5+
{
6+
private const string CustomErrorMessage = nameof(CustomErrorMessage);
7+
8+
[TestMethod]
9+
public async Task CanHandleSqlExceptionAsync()
10+
{
11+
RetryPolicy<SqlDatabaseTransientErrorDetectionStrategy> retryPolicy = new (new Incremental(RetryStrategy.DefaultClientRetryCount, RetryStrategy.DefaultRetryInterval, RetryStrategy.DefaultRetryIncrement));
12+
13+
try
14+
{
15+
await retryPolicy.ExecuteAsync(async () =>
16+
{
17+
await using SqlConnection connection = new (TestDatabase.TransientFaultHandlingTestDatabase);
18+
await connection.OpenAsync();
19+
await using SqlCommand? command = connection.CreateCommand();
20+
command.CommandText = $"RAISERROR('{CustomErrorMessage}', 16, 1)";
21+
await command.ExecuteNonQueryAsync();
22+
Assert.Fail();
23+
});
24+
}
25+
catch (Exception ex)
26+
{
27+
if (ex is SqlException { Message: CustomErrorMessage })
28+
{
29+
return;
30+
}
31+
32+
Assert.Fail();
33+
}
34+
35+
Assert.Fail();
36+
}
37+
38+
[TestMethod]
39+
public void CanHandleSqlException()
40+
{
41+
RetryPolicy<SqlDatabaseTransientErrorDetectionStrategy> retryPolicy = new (new Incremental(RetryStrategy.DefaultClientRetryCount, RetryStrategy.DefaultRetryInterval, RetryStrategy.DefaultRetryIncrement));
42+
43+
try
44+
{
45+
retryPolicy.ExecuteAction(() =>
46+
{
47+
using SqlConnection connection = new (TestDatabase.TransientFaultHandlingTestDatabase);
48+
connection.Open();
49+
using SqlCommand? command = connection.CreateCommand();
50+
command.CommandText = $"RAISERROR('{CustomErrorMessage}', 16, 1)";
51+
command.ExecuteNonQuery();
52+
Assert.Fail();
53+
});
54+
}
55+
catch (Exception ex)
56+
{
57+
if (ex is SqlException { Message: CustomErrorMessage })
58+
{
59+
return;
60+
}
61+
62+
Assert.Fail();
63+
}
64+
65+
Assert.Fail();
66+
}
67+
}

Tools/AssemblyInfo.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,12 @@
22

33
[assembly: CLSCompliant(true)]
44
[assembly: SecurityTransparent]
5+
6+
#if NETSTANDARD1_6 || NETSTANDARD2_0
7+
// The NETSTANDARD1_6 and NETSTANDARD2_0 builds can be used by .NET Framework projects.
8+
// Without this, when TransientFaultHandling.Data.Core is used by .NET Framework projects, it throws System.TypeAccessException when handling SqlException:
9+
// Attempt by security transparent method 'Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.SqlDatabaseTransientErrorDetectionStrategy.IsTransient(System.Exception)' to access security critical type 'Microsoft.Data.SqlClient.SqlException' failed.
10+
// NETSTANDARD1_0 to NETSTANDARD1_5 does not support [SecurityRules]. And NETSTANDARD2_1_OR_GREATER is not supported by .NET Framework.
11+
// https://docs.microsoft.com/en-us/dotnet/standard/net-standard#specification
12+
[assembly: SecurityRules(SecurityRuleSet.Level1)]
13+
#endif

0 commit comments

Comments
 (0)