-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathLocalAppContextSwitchesHelper.cs
More file actions
636 lines (547 loc) · 19.9 KB
/
LocalAppContextSwitchesHelper.cs
File metadata and controls
636 lines (547 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Microsoft.Data.SqlClient.Tests.Common;
/// <summary>
/// This class provides read/write access to LocalAppContextSwitches values
/// for the duration of a test. It is intended to be constructed at the start
/// of a test and disposed of at the end. It captures the original values of
/// the switches and restores them when disposed.
///
/// This follows the RAII pattern to ensure that the switches are always
/// restored, which is important for global state like LocalAppContextSwitches.
///
/// https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization
///
/// This class is not thread-aware and should not be used concurrently.
/// </summary>
public sealed class LocalAppContextSwitchesHelper : IDisposable
{
#region Private Fields
// These fields are used to expose LocalAppContextSwitches's properties.
private readonly PropertyInfo _legacyRowVersionNullBehaviorProperty;
private readonly PropertyInfo _suppressInsecureTlsWarningProperty;
private readonly PropertyInfo _makeReadAsyncBlockingProperty;
private readonly PropertyInfo _useMinimumLoginTimeoutProperty;
private readonly PropertyInfo _legacyVarTimeZeroScaleBehaviourProperty;
private readonly PropertyInfo _useCompatibilityProcessSniProperty;
private readonly PropertyInfo _useCompatibilityAsyncBehaviourProperty;
private readonly PropertyInfo _useConnectionPoolV2Property;
private readonly PropertyInfo _truncateScaledDecimalProperty;
private readonly PropertyInfo _ignoreServerProvidedFailoverPartner;
private readonly PropertyInfo _enableUserAgent;
#if NET
private readonly PropertyInfo _globalizationInvariantModeProperty;
private readonly PropertyInfo _useManagedNetworkingProperty;
#else
private readonly PropertyInfo _disableTnirByDefaultProperty;
#endif
// These fields are used to capture the original switch values.
private readonly FieldInfo _legacyRowVersionNullBehaviorField;
private readonly Tristate _legacyRowVersionNullBehaviorOriginal;
private readonly FieldInfo _suppressInsecureTlsWarningField;
private readonly Tristate _suppressInsecureTlsWarningOriginal;
private readonly FieldInfo _makeReadAsyncBlockingField;
private readonly Tristate _makeReadAsyncBlockingOriginal;
private readonly FieldInfo _useMinimumLoginTimeoutField;
private readonly Tristate _useMinimumLoginTimeoutOriginal;
private readonly FieldInfo _legacyVarTimeZeroScaleBehaviourField;
private readonly Tristate _legacyVarTimeZeroScaleBehaviourOriginal;
private readonly FieldInfo _useCompatibilityProcessSniField;
private readonly Tristate _useCompatibilityProcessSniOriginal;
private readonly FieldInfo _useCompatibilityAsyncBehaviourField;
private readonly Tristate _useCompatibilityAsyncBehaviourOriginal;
private readonly FieldInfo _useConnectionPoolV2Field;
private readonly Tristate _useConnectionPoolV2Original;
private readonly FieldInfo _truncateScaledDecimalField;
private readonly Tristate _truncateScaledDecimalOriginal;
private readonly FieldInfo _ignoreServerProvidedFailoverPartnerField;
private readonly Tristate _ignoreServerProvidedFailoverPartnerOriginal;
private readonly FieldInfo _enableUserAgentField;
private readonly Tristate _enableUserAgentOriginal;
#if NET
private readonly FieldInfo _globalizationInvariantModeField;
private readonly Tristate _globalizationInvariantModeOriginal;
private readonly FieldInfo _useManagedNetworkingField;
private readonly Tristate _useManagedNetworkingOriginal;
#else
private readonly FieldInfo _disableTnirByDefaultField;
private readonly Tristate _disableTnirByDefaultOriginal;
#endif
#endregion
#region Public Types
/// <summary>
/// This enum is used to represent the state of a switch.
///
/// It is a copy of the Tristate enum from LocalAppContextSwitches.
/// </summary>
public enum Tristate : byte
{
NotInitialized = 0,
False = 1,
True = 2
}
#endregion
#region Construction
/// <summary>
/// Construct to capture all existing switch values.
/// </summary>
///
/// <exception cref="Exception">
/// Throws if any values cannot be captured.
/// </exception>
public LocalAppContextSwitchesHelper()
{
// Acquire a handle to the LocalAppContextSwitches type.
var assembly = typeof(SqlCommandBuilder).Assembly;
var switchesType = assembly.GetType(
"Microsoft.Data.SqlClient.LocalAppContextSwitches");
if (switchesType == null)
{
throw new Exception("Unable to find LocalAppContextSwitches type.");
}
// A local helper to acquire a handle to a property.
void InitProperty(string name, out PropertyInfo property)
{
var prop = switchesType.GetProperty(
name, BindingFlags.Public | BindingFlags.Static);
if (prop == null)
{
throw new Exception($"Unable to find {name} property.");
}
property = prop;
}
// Acquire handles to all of the public properties of
// LocalAppContextSwitches.
InitProperty(
"LegacyRowVersionNullBehavior",
out _legacyRowVersionNullBehaviorProperty);
InitProperty(
"SuppressInsecureTlsWarning",
out _suppressInsecureTlsWarningProperty);
InitProperty(
"MakeReadAsyncBlocking",
out _makeReadAsyncBlockingProperty);
InitProperty(
"UseMinimumLoginTimeout",
out _useMinimumLoginTimeoutProperty);
InitProperty(
"LegacyVarTimeZeroScaleBehaviour",
out _legacyVarTimeZeroScaleBehaviourProperty);
InitProperty(
"UseCompatibilityProcessSni",
out _useCompatibilityProcessSniProperty);
InitProperty(
"UseCompatibilityAsyncBehaviour",
out _useCompatibilityAsyncBehaviourProperty);
InitProperty(
"UseConnectionPoolV2",
out _useConnectionPoolV2Property);
InitProperty(
"TruncateScaledDecimal",
out _truncateScaledDecimalProperty);
InitProperty(
"IgnoreServerProvidedFailoverPartner",
out _ignoreServerProvidedFailoverPartner);
InitProperty(
"EnableUserAgent",
out _enableUserAgent);
#if NET
InitProperty(
"GlobalizationInvariantMode",
out _globalizationInvariantModeProperty);
InitProperty(
"UseManagedNetworking",
out _useManagedNetworkingProperty);
#else
InitProperty(
"DisableTnirByDefault",
out _disableTnirByDefaultProperty);
#endif
// A local helper to capture the original value of a switch.
void InitField(string name, out FieldInfo field, out Tristate value)
{
var fieldInfo =
switchesType.GetField(
name, BindingFlags.NonPublic | BindingFlags.Static);
if (fieldInfo == null)
{
throw new Exception($"Unable to find {name} field.");
}
field = fieldInfo;
value = GetValue(field);
}
// Capture the original value of each switch.
InitField(
"s_legacyRowVersionNullBehavior",
out _legacyRowVersionNullBehaviorField,
out _legacyRowVersionNullBehaviorOriginal);
InitField(
"s_suppressInsecureTlsWarning",
out _suppressInsecureTlsWarningField,
out _suppressInsecureTlsWarningOriginal);
InitField(
"s_makeReadAsyncBlocking",
out _makeReadAsyncBlockingField,
out _makeReadAsyncBlockingOriginal);
InitField(
"s_useMinimumLoginTimeout",
out _useMinimumLoginTimeoutField,
out _useMinimumLoginTimeoutOriginal);
InitField(
"s_legacyVarTimeZeroScaleBehaviour",
out _legacyVarTimeZeroScaleBehaviourField,
out _legacyVarTimeZeroScaleBehaviourOriginal);
InitField(
"s_useCompatibilityProcessSni",
out _useCompatibilityProcessSniField,
out _useCompatibilityProcessSniOriginal);
InitField(
"s_useCompatibilityAsyncBehaviour",
out _useCompatibilityAsyncBehaviourField,
out _useCompatibilityAsyncBehaviourOriginal);
InitField(
"s_useConnectionPoolV2",
out _useConnectionPoolV2Field,
out _useConnectionPoolV2Original);
InitField(
"s_truncateScaledDecimal",
out _truncateScaledDecimalField,
out _truncateScaledDecimalOriginal);
InitField(
"s_ignoreServerProvidedFailoverPartner",
out _ignoreServerProvidedFailoverPartnerField,
out _ignoreServerProvidedFailoverPartnerOriginal);
InitField(
"s_enableUserAgent",
out _enableUserAgentField,
out _enableUserAgentOriginal);
#if NET
InitField(
"s_globalizationInvariantMode",
out _globalizationInvariantModeField,
out _globalizationInvariantModeOriginal);
InitField(
"s_useManagedNetworking",
out _useManagedNetworkingField,
out _useManagedNetworkingOriginal);
#else
InitField(
"s_disableTnirByDefault",
out _disableTnirByDefaultField,
out _disableTnirByDefaultOriginal);
#endif
}
/// <summary>
/// Disposal restores all original switch values as a best effort.
/// </summary>
///
/// <exception cref="Exception">
/// Throws if any values could not be restored after trying to restore all
/// values.
/// </exception>
public void Dispose()
{
List<string> failedFields = new();
void RestoreField(FieldInfo field, Tristate value)
{
try
{
SetValue(field, value);
}
catch (Exception)
{
failedFields.Add(field.Name);
}
}
RestoreField(
_legacyRowVersionNullBehaviorField,
_legacyRowVersionNullBehaviorOriginal);
RestoreField(
_suppressInsecureTlsWarningField,
_suppressInsecureTlsWarningOriginal);
RestoreField(
_makeReadAsyncBlockingField,
_makeReadAsyncBlockingOriginal);
RestoreField(
_useMinimumLoginTimeoutField,
_useMinimumLoginTimeoutOriginal);
RestoreField(
_legacyVarTimeZeroScaleBehaviourField,
_legacyVarTimeZeroScaleBehaviourOriginal);
RestoreField(
_useCompatibilityProcessSniField,
_useCompatibilityProcessSniOriginal);
RestoreField(
_useCompatibilityAsyncBehaviourField,
_useCompatibilityAsyncBehaviourOriginal);
RestoreField(
_useConnectionPoolV2Field,
_useConnectionPoolV2Original);
RestoreField(
_truncateScaledDecimalField,
_truncateScaledDecimalOriginal);
RestoreField(
_ignoreServerProvidedFailoverPartnerField,
_ignoreServerProvidedFailoverPartnerOriginal);
RestoreField(
_enableUserAgentField,
_enableUserAgentOriginal);
#if NET
RestoreField(
_globalizationInvariantModeField,
_globalizationInvariantModeOriginal);
RestoreField(
_useManagedNetworkingField,
_useManagedNetworkingOriginal);
#else
RestoreField(
_disableTnirByDefaultField,
_disableTnirByDefaultOriginal);
#endif
if (failedFields.Count > 0)
{
throw new Exception(
"Failed to restore the following fields: " +
string.Join(", ", failedFields));
}
}
#endregion
#region Public Properties
/// <summary>
/// Access the LocalAppContextSwitches.LegacyRowVersionNullBehavior
/// property.
/// </summary>
public bool LegacyRowVersionNullBehavior
{
get => (bool)_legacyRowVersionNullBehaviorProperty.GetValue(null);
}
/// <summary>
/// Access the LocalAppContextSwitches.SuppressInsecureTlsWarning property.
/// </summary>
public bool SuppressInsecureTlsWarning
{
get => (bool)_suppressInsecureTlsWarningProperty.GetValue(null);
}
/// <summary>
/// Access the LocalAppContextSwitches.MakeReadAsyncBlocking property.
/// </summary>
public bool MakeReadAsyncBlocking
{
get => (bool)_makeReadAsyncBlockingProperty.GetValue(null);
}
/// <summary>
/// Access the LocalAppContextSwitches.UseMinimumLoginTimeout property.
/// </summary>
public bool UseMinimumLoginTimeout
{
get => (bool)_useMinimumLoginTimeoutProperty.GetValue(null);
}
/// <summary>
/// Access the LocalAppContextSwitches.LegacyVarTimeZeroScaleBehaviour
/// property.
/// </summary>
public bool LegacyVarTimeZeroScaleBehaviour
{
get => (bool)_legacyVarTimeZeroScaleBehaviourProperty.GetValue(null);
}
/// <summary>
/// Access the LocalAppContextSwitches.UseCompatibilityProcessSni property.
/// </summary>
public bool UseCompatibilityProcessSni
{
get => (bool)_useCompatibilityProcessSniProperty.GetValue(null);
}
/// <summary>
/// Access the LocalAppContextSwitches.UseCompatibilityAsyncBehaviour
/// property.
/// </summary>
public bool UseCompatibilityAsyncBehaviour
{
get => (bool)_useCompatibilityAsyncBehaviourProperty.GetValue(null);
}
/// <summary>
/// Access the LocalAppContextSwitches.UseConnectionPoolV2 property.
/// </summary>
public bool UseConnectionPoolV2
{
get => (bool)_useConnectionPoolV2Property.GetValue(null);
}
/// <summary>
/// Access the LocalAppContextSwitches.TruncateScaledDecimal property.
/// </summary>
public bool TruncateScaledDecimal
{
get => (bool)_truncateScaledDecimalProperty.GetValue(null);
}
public bool IgnoreServerProvidedFailoverPartner
{
get => (bool)_ignoreServerProvidedFailoverPartner.GetValue(null);
}
public bool EnableUserAgent
{
get => (bool)_enableUserAgent.GetValue(null);
}
#if NET
/// <summary>
/// Access the LocalAppContextSwitches.GlobalizationInvariantMode property.
/// </summary>
public bool GlobalizationInvariantMode
{
get => (bool)_globalizationInvariantModeProperty.GetValue(null);
}
/// <summary>
/// Access the LocalAppContextSwitches.UseManagedNetworking property.
/// </summary>
public bool UseManagedNetworking
{
get => (bool)_useManagedNetworkingProperty.GetValue(null);
}
#else
/// <summary>
/// Access the LocalAppContextSwitches.DisableTnirByDefault property.
/// </summary>
public bool DisableTnirByDefault
{
get => (bool)_disableTnirByDefaultProperty.GetValue(null);
}
#endif
// These properties get or set the like-named underlying switch field value.
//
// They all fail the test if the value cannot be retrieved or set.
/// <summary>
/// Get or set the LocalAppContextSwitches.LegacyRowVersionNullBehavior
/// switch value.
/// </summary>
public Tristate LegacyRowVersionNullBehaviorField
{
get => GetValue(_legacyRowVersionNullBehaviorField);
set => SetValue(_legacyRowVersionNullBehaviorField, value);
}
/// <summary>
/// Get or set the LocalAppContextSwitches.SuppressInsecureTlsWarning
/// switch value.
/// </summary>
public Tristate SuppressInsecureTlsWarningField
{
get => GetValue(_suppressInsecureTlsWarningField);
set => SetValue(_suppressInsecureTlsWarningField, value);
}
/// <summary>
/// Get or set the LocalAppContextSwitches.MakeReadAsyncBlocking switch
/// value.
/// </summary>
public Tristate MakeReadAsyncBlockingField
{
get => GetValue(_makeReadAsyncBlockingField);
set => SetValue(_makeReadAsyncBlockingField, value);
}
/// <summary>
/// Get or set the LocalAppContextSwitches.UseMinimumLoginTimeout switch
/// value.
/// </summary>
public Tristate UseMinimumLoginTimeoutField
{
get => GetValue(_useMinimumLoginTimeoutField);
set => SetValue(_useMinimumLoginTimeoutField, value);
}
/// <summary>
/// Get or set the LocalAppContextSwitches.LegacyVarTimeZeroScaleBehaviour
/// switch value.
/// </summary>
public Tristate LegacyVarTimeZeroScaleBehaviourField
{
get => GetValue(_legacyVarTimeZeroScaleBehaviourField);
set => SetValue(_legacyVarTimeZeroScaleBehaviourField, value);
}
/// <summary>
/// Get or set the LocalAppContextSwitches.UseCompatibilityProcessSni switch
/// value.
/// </summary>
public Tristate UseCompatibilityProcessSniField
{
get => GetValue(_useCompatibilityProcessSniField);
set => SetValue(_useCompatibilityProcessSniField, value);
}
/// <summary>
/// Get or set the LocalAppContextSwitches.UseCompatibilityAsyncBehaviour
/// switch value.
/// </summary>
public Tristate UseCompatibilityAsyncBehaviourField
{
get => GetValue(_useCompatibilityAsyncBehaviourField);
set => SetValue(_useCompatibilityAsyncBehaviourField, value);
}
/// <summary>
/// Get or set the LocalAppContextSwitches.UseConnectionPoolV2 switch value.
/// </summary>
public Tristate UseConnectionPoolV2Field
{
get => GetValue(_useConnectionPoolV2Field);
set => SetValue(_useConnectionPoolV2Field, value);
}
/// <summary>
/// Get or set the LocalAppContextSwitches.TruncateScaledDecimal switch value.
/// </summary>
public Tristate TruncateScaledDecimalField
{
get => GetValue(_truncateScaledDecimalField);
set => SetValue(_truncateScaledDecimalField, value);
}
public Tristate IgnoreServerProvidedFailoverPartnerField
{
get => GetValue(_ignoreServerProvidedFailoverPartnerField);
set => SetValue(_ignoreServerProvidedFailoverPartnerField, value);
}
public Tristate EnableUserAgentField
{
get => GetValue(_enableUserAgentField);
set => SetValue(_enableUserAgentField, value);
}
#if NET
/// <summary>
/// Get or set the LocalAppContextSwitches.GlobalizationInvariantMode switch value.
/// </summary>
public Tristate GlobalizationInvariantModeField
{
get => GetValue(_globalizationInvariantModeField);
set => SetValue(_globalizationInvariantModeField, value);
}
/// <summary>
/// Get or set the LocalAppContextSwitches.UseManagedNetworking switch value.
/// </summary>
public Tristate UseManagedNetworkingField
{
get => GetValue(_useManagedNetworkingField);
set => SetValue(_useManagedNetworkingField, value);
}
#else
/// <summary>
/// Get or set the LocalAppContextSwitches.DisableTnirByDefault switch
/// value.
/// </summary>
public Tristate DisableTnirByDefaultField
{
get => GetValue(_disableTnirByDefaultField);
set => SetValue(_disableTnirByDefaultField, value);
}
#endif
#endregion
#region Private Helpers
// Get the value of the given field, or throw if it is null.
private static Tristate GetValue(FieldInfo field)
{
var value = field.GetValue(null);
if (value is null)
{
throw new Exception($"Field {field.Name} has a null value.");
}
return (Tristate)value;
}
// Set the value of the given field.
private static void SetValue(FieldInfo field, Tristate value)
{
field.SetValue(null, (byte)value);
}
#endregion
}