@@ -28,6 +28,9 @@ public class RetryLogicConfigHelper
2828
2929 private const string SqlRetryLogicTypeName = "Microsoft.Data.SqlClient.SqlRetryLogic" ;
3030
31+ private const string CreateExceptionMethodName = "CreateException" ;
32+ private const string AddMethodName = "Add" ;
33+
3134 public const string DefaultTransientErrors = "1204, 1205, 1222, 49918, 49919, 49920, 4060, 4221, 40143, 40613, 40501, 40540, 40197, 42108, 42109, 10929, 10928, 10060, 10054, 10053, 997, 233, 64, 20, 0, -2, 207, 102, 2812" ;
3235
3336 private static readonly Random s_random = new Random ( ) ;
@@ -36,13 +39,23 @@ public class RetryLogicConfigHelper
3639 private static readonly Type s_appContextSwitchManagerType = s_sqlClientAssembly . GetType ( AppContextSwitchManagerTypeName ) ;
3740 private static readonly Type s_sqlretrylogicType = s_sqlClientAssembly . GetType ( SqlRetryLogicTypeName ) ;
3841 private static readonly Type s_configurationLoaderType = s_sqlClientAssembly . GetType ( ConfigurationLoaderTypeName ) ;
42+ private static readonly Type s_sqlErrorType = typeof ( SqlError ) ;
43+ private static readonly Type s_sqlErrorCollectionType = typeof ( SqlErrorCollection ) ;
3944 private static readonly Type [ ] s_cfgLoaderParamsType = new Type [ ]
4045 {
4146 s_sqlClientAssembly . GetType ( InterfaceCnnCfgTypeName ) ,
4247 s_sqlClientAssembly . GetType ( InterfaceCmdCfgTypeName ) ,
4348 typeof ( string ) , typeof ( string )
4449 } ;
50+ private static readonly Type [ ] s_sqlErrorParamsType = new Type [ ]
51+ {
52+ typeof ( int ) , typeof ( byte ) , typeof ( byte ) ,
53+ typeof ( string ) , typeof ( string ) , typeof ( string ) ,
54+ typeof ( int ) , typeof ( Exception )
55+ } ;
4556 private static readonly ConstructorInfo s_loaderCtorInfo = s_configurationLoaderType . GetConstructor ( s_cfgLoaderParamsType ) ;
57+ private static readonly ConstructorInfo s_sqlErrorCtorInfo = s_sqlErrorType . GetConstructor ( BindingFlags . Instance | BindingFlags . NonPublic , null , s_sqlErrorParamsType , null ) ;
58+ private static readonly ConstructorInfo s_sqlErrorCollectionCtorInfo = s_sqlErrorCollectionType . GetConstructor ( BindingFlags . Instance | BindingFlags . NonPublic , null , Type . EmptyTypes , null ) ;
4659
4760 public static object CreateLoader ( RetryLogicConfigs cnnConfig , RetryLogicConfigs cmdConfig )
4861 {
@@ -164,6 +177,22 @@ public static RetryLogicConfigs CreateRandomConfig(string method, string authori
164177 } ;
165178 }
166179
180+ public static SqlException CreateSqlException ( int errorNumber )
181+ {
182+ MethodInfo addSqlErrorMethod = typeof ( SqlErrorCollection ) . GetMethod ( AddMethodName , BindingFlags . Instance | BindingFlags . NonPublic ) ;
183+ MethodInfo createExceptionMethod = typeof ( SqlException ) . GetMethod ( CreateExceptionMethodName , BindingFlags . Static | BindingFlags . NonPublic ,
184+ null , new Type [ ] { typeof ( SqlErrorCollection ) , typeof ( string ) } , null ) ;
185+
186+ SqlError sqlError = s_sqlErrorCtorInfo . Invoke ( new object [ ] { errorNumber , ( byte ) 0 , ( byte ) 0 , string . Empty , string . Empty , string . Empty , 0 , null } ) as SqlError ;
187+ SqlErrorCollection sqlErrorCollection = s_sqlErrorCollectionCtorInfo . Invoke ( new object [ 0 ] { } ) as SqlErrorCollection ;
188+
189+ addSqlErrorMethod . Invoke ( sqlErrorCollection , new object [ ] { sqlError } ) ;
190+
191+ SqlException sqlException = createExceptionMethod . Invoke ( null , new object [ ] { sqlErrorCollection , string . Empty } ) as SqlException ;
192+
193+ return sqlException ;
194+ }
195+
167196 private static TimeSpan GenerateTimeSpan ( TimeSpan start , TimeSpan end )
168197 {
169198 int max = ( int ) ( end - start ) . TotalSeconds ;
0 commit comments