Skip to content

Commit 995df25

Browse files
authored
Cleanup | Convert SniLoadHandle to static class (#3768)
1 parent 70587f8 commit 995df25

File tree

10 files changed

+40
-52
lines changed

10 files changed

+40
-52
lines changed

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/LocalDB.netcore.Windows.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private string GetConnectionString(string localDbInstance)
5656
int result = localDBStartInstanceFunc(localDbInstance, 0, localDBConnectionString, ref sizeOfbuffer);
5757
if (result != TdsEnums.SNI_SUCCESS)
5858
{
59-
SniLoadHandle.SingletonInstance.LastError = new SniError(SniProviders.INVALID_PROV, 0, SniCommon.LocalDBErrorCode, Strings.SNI_ERROR_50);
59+
SniLoadHandle.LastError = new SniError(SniProviders.INVALID_PROV, 0, SniCommon.LocalDBErrorCode, Strings.SNI_ERROR_50);
6060
SqlClientEventSource.Log.TrySNITraceEvent(nameof(LocalDB), EventType.ERR, "Unsuccessful 'LocalDBStartInstance' method call with {0} result to start '{1}' localDb instance", args0: result, args1: localDbInstance);
6161
localDBConnectionString = null;
6262
}
@@ -134,15 +134,15 @@ private bool LoadUserInstanceDll()
134134
// If there was no DLL path found, then there is an error.
135135
if (dllPath == null)
136136
{
137-
SniLoadHandle.SingletonInstance.LastError = new SniError(SniProviders.INVALID_PROV, 0, MapLocalDBErrorStateToCode(registryQueryErrorState), MapLocalDBErrorStateToErrorMessage(registryQueryErrorState));
137+
SniLoadHandle.LastError = new SniError(SniProviders.INVALID_PROV, 0, MapLocalDBErrorStateToCode(registryQueryErrorState), MapLocalDBErrorStateToErrorMessage(registryQueryErrorState));
138138
SqlClientEventSource.Log.TrySNITraceEvent(nameof(LocalDB), EventType.ERR, "User instance DLL path is null.");
139139
return false;
140140
}
141141

142142
// In case the registry had an empty path for dll
143143
if (string.IsNullOrWhiteSpace(dllPath))
144144
{
145-
SniLoadHandle.SingletonInstance.LastError = new SniError(SniProviders.INVALID_PROV, 0, SniCommon.LocalDBInvalidSqlUserInstanceDllPath, Strings.SNI_ERROR_55);
145+
SniLoadHandle.LastError = new SniError(SniProviders.INVALID_PROV, 0, SniCommon.LocalDBInvalidSqlUserInstanceDllPath, Strings.SNI_ERROR_55);
146146
SqlClientEventSource.Log.TrySNITraceEvent(nameof(LocalDB), EventType.ERR, "User instance DLL path is invalid. DLL path = {0}", dllPath);
147147
return false;
148148
}
@@ -152,7 +152,7 @@ private bool LoadUserInstanceDll()
152152

153153
if (libraryHandle.IsInvalid)
154154
{
155-
SniLoadHandle.SingletonInstance.LastError = new SniError(SniProviders.INVALID_PROV, 0, SniCommon.LocalDBFailedToLoadDll, Strings.SNI_ERROR_56);
155+
SniLoadHandle.LastError = new SniError(SniProviders.INVALID_PROV, 0, SniCommon.LocalDBFailedToLoadDll, Strings.SNI_ERROR_56);
156156
SqlClientEventSource.Log.TrySNITraceEvent(nameof(LocalDB), EventType.ERR, "Library Handle is invalid. Could not load the dll.");
157157
libraryHandle.Dispose();
158158
return false;
@@ -163,7 +163,7 @@ private bool LoadUserInstanceDll()
163163

164164
if (_startInstanceHandle == IntPtr.Zero)
165165
{
166-
SniLoadHandle.SingletonInstance.LastError = new SniError(SniProviders.INVALID_PROV, 0, SniCommon.LocalDBBadRuntime, Strings.SNI_ERROR_57);
166+
SniLoadHandle.LastError = new SniError(SniProviders.INVALID_PROV, 0, SniCommon.LocalDBBadRuntime, Strings.SNI_ERROR_57);
167167
SqlClientEventSource.Log.TrySNITraceEvent(nameof(LocalDB), EventType.ERR, "Was not able to load the PROC from DLL. Bad Runtime.");
168168
libraryHandle.Dispose();
169169
return false;
@@ -174,7 +174,7 @@ private bool LoadUserInstanceDll()
174174

175175
if (localDBStartInstanceFunc == null)
176176
{
177-
SniLoadHandle.SingletonInstance.LastError = new SniError(SniProviders.INVALID_PROV, 0, SniCommon.LocalDBBadRuntime, Strings.SNI_ERROR_57);
177+
SniLoadHandle.LastError = new SniError(SniProviders.INVALID_PROV, 0, SniCommon.LocalDBBadRuntime, Strings.SNI_ERROR_57);
178178
libraryHandle.Dispose();
179179
_startInstanceHandle = IntPtr.Zero;
180180
return false;

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniCommon.netcore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ internal static uint ReportSNIError(SniProviders provider, uint sniError, Except
216216
/// <returns></returns>
217217
internal static uint ReportSNIError(SniError error)
218218
{
219-
SniLoadHandle.SingletonInstance.LastError = error;
219+
SniLoadHandle.LastError = error;
220220
return TdsEnums.SNI_ERROR;
221221
}
222222
}

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniError.netcore.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ internal class SniError
2727
public readonly uint lineNumber;
2828
public readonly Exception exception;
2929

30+
public static SniError Success { get; } = new(SniProviders.INVALID_PROV, 0, TdsEnums.SNI_SUCCESS, string.Empty);
31+
3032
public SniError(SniProviders provider, int nativeError, uint sniErrorCode, string errorMessage)
3133
{
3234
lineNumber = 0;

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniLoadHandle.netcore.cs

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,51 @@
44

55
#if NET
66

7-
using System.Threading;
7+
using System;
8+
9+
#nullable enable
810

911
namespace Microsoft.Data.SqlClient.ManagedSni
1012
{
1113
/// <summary>
1214
/// Global SNI settings and status
1315
/// </summary>
14-
internal class SniLoadHandle
16+
internal static class SniLoadHandle
1517
{
16-
public static readonly SniLoadHandle SingletonInstance = new SniLoadHandle();
17-
18-
public readonly EncryptionOptions _encryptionOption = EncryptionOptions.OFF;
19-
public ThreadLocal<SniError> _lastError = new ThreadLocal<SniError>(static () => new SniError(SniProviders.INVALID_PROV, 0, TdsEnums.SNI_SUCCESS, string.Empty));
20-
21-
private readonly uint _status = TdsEnums.SNI_SUCCESS;
18+
[ThreadStatic]
19+
private static SniError? s_lastError;
2220

2321
/// <summary>
2422
/// Last SNI error
2523
/// </summary>
26-
public SniError LastError
24+
public static SniError LastError
2725
{
2826
get
2927
{
30-
return _lastError.Value;
28+
return s_lastError ??= SniError.Success;
3129
}
3230

3331
set
3432
{
35-
_lastError.Value = value;
33+
s_lastError = value;
3634
}
3735
}
3836

3937
/// <summary>
4038
/// SNI library status
4139
/// </summary>
42-
public uint Status
43-
{
44-
get
45-
{
46-
return _status;
47-
}
48-
}
40+
public const uint Status = TdsEnums.SNI_SUCCESS;
4941

5042
/// <summary>
5143
/// Encryption options setting
5244
/// </summary>
53-
public EncryptionOptions Options
54-
{
55-
get
56-
{
57-
return _encryptionOption;
58-
}
59-
}
45+
public const EncryptionOptions Options = EncryptionOptions.OFF;
6046

6147
/// <summary>
6248
/// Verify client encryption possibility
6349
/// </summary>
6450
// TODO: by adding support ENCRYPT_NOT_SUP, it could be calculated.
65-
public bool ClientOSEncryptionSupport => true;
51+
public const bool ClientOSEncryptionSupport = true;
6652
}
6753
}
6854

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniMarsConnection.netcore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public void HandleReceiveComplete(SniPacket packet, uint sniErrorCode)
291291

292292
if (!_sessions.ContainsKey(_currentHeader.sessionId))
293293
{
294-
SniLoadHandle.SingletonInstance.LastError = new SniError(SniProviders.SMUX_PROV, 0, SniCommon.InvalidParameterError, Strings.SNI_ERROR_5);
294+
SniLoadHandle.LastError = new SniError(SniProviders.SMUX_PROV, 0, SniCommon.InvalidParameterError, Strings.SNI_ERROR_5);
295295
HandleReceiveError(packet);
296296
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniMarsConnection), EventType.ERR, "Current Header Session Id {0} not found, MARS Session Id {1} will be destroyed, New SNI error created: {2}", args0: _currentHeader?.sessionId, args1: _lowerHandle?.ConnectionId, args2: sniErrorCode);
297297
_lowerHandle.Dispose();

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniMarsHandle.netcore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ public void HandleReceiveError(SniPacket packet)
346346

347347
lock (_receivedPacketQueue)
348348
{
349-
_connectionError = SniLoadHandle.SingletonInstance.LastError;
349+
_connectionError = SniLoadHandle.LastError;
350350
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniMarsHandle), EventType.ERR, "MARS Session Id {0}, _connectionError to be handled: {1}", args0: ConnectionId, args1: _connectionError);
351351
_packetEvent.Set();
352352
}
@@ -520,7 +520,7 @@ public override uint Receive(out SniPacket packet, int timeoutInMilliseconds)
520520
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniMarsHandle), EventType.INFO, "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, Waiting for packet event.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater);
521521
if (!_packetEvent.Wait(timeoutInMilliseconds))
522522
{
523-
SniLoadHandle.SingletonInstance.LastError = new SniError(SniProviders.SMUX_PROV, 0, SniCommon.ConnTimeoutError, Strings.SNI_ERROR_11);
523+
SniLoadHandle.LastError = new SniError(SniProviders.SMUX_PROV, 0, SniCommon.ConnTimeoutError, Strings.SNI_ERROR_11);
524524
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniMarsHandle), EventType.INFO, "MARS Session Id {0}, _sequenceNumber {1}, _sendHighwater {2}, _packetEvent wait timed out.", args0: ConnectionId, args1: _sequenceNumber, args2: _sendHighwater);
525525
return TdsEnums.SNI_WAIT_TIMEOUT;
526526
}

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniPacket.netcore.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ private static void ReadFromStreamAsyncContinuation(Task<int> task, object state
285285
Exception e = task.Exception?.InnerException;
286286
if (e != null)
287287
{
288-
SniLoadHandle.SingletonInstance.LastError = new SniError(SniProviders.TCP_PROV, SniCommon.InternalExceptionError, e);
288+
SniLoadHandle.LastError = new SniError(SniProviders.TCP_PROV, SniCommon.InternalExceptionError, e);
289289
#if DEBUG
290290
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniPacket), EventType.ERR, "Connection Id {0}, Internal Exception occurred while reading data: {1}", args0: packet._owner?.ConnectionId, args1: e?.Message);
291291
#endif
@@ -299,7 +299,7 @@ private static void ReadFromStreamAsyncContinuation(Task<int> task, object state
299299
#endif
300300
if (packet._dataLength == 0)
301301
{
302-
SniLoadHandle.SingletonInstance.LastError = new SniError(SniProviders.TCP_PROV, 0, SniCommon.ConnTerminatedError, Strings.SNI_ERROR_2);
302+
SniLoadHandle.LastError = new SniError(SniProviders.TCP_PROV, 0, SniCommon.ConnTerminatedError, Strings.SNI_ERROR_2);
303303
#if DEBUG
304304
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniPacket), EventType.ERR, "Connection Id {0}, No data read from stream, connection was terminated.", args0: packet._owner?.ConnectionId);
305305
#endif
@@ -340,7 +340,7 @@ public async void WriteToStreamAsync(Stream stream, SniAsyncCallback callback, S
340340
}
341341
catch (Exception e)
342342
{
343-
SniLoadHandle.SingletonInstance.LastError = new SniError(provider, SniCommon.InternalExceptionError, e);
343+
SniLoadHandle.LastError = new SniError(provider, SniCommon.InternalExceptionError, e);
344344
#if DEBUG
345345
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniPacket), EventType.ERR, "Connection Id {0}, Internal Exception occurred while writing data: {1}", args0: _owner?.ConnectionId, args1: e?.Message);
346346
#endif

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ManagedSni/SniProxy.netcore.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ internal static SniHandle CreateConnectionHandle(
107107
}
108108
catch (Exception e)
109109
{
110-
SniLoadHandle.SingletonInstance.LastError = new SniError(SniProviders.INVALID_PROV, SniCommon.ErrorSpnLookup, e);
110+
SniLoadHandle.LastError = new SniError(SniProviders.INVALID_PROV, SniCommon.ErrorSpnLookup, e);
111111
}
112112
}
113113

@@ -208,7 +208,7 @@ private static SniTcpHandle CreateTcpHandle(
208208
string hostName = details.ServerName;
209209
if (string.IsNullOrWhiteSpace(hostName))
210210
{
211-
SniLoadHandle.SingletonInstance.LastError = new SniError(SniProviders.TCP_PROV, 0, SniCommon.InvalidConnStringError, Strings.SNI_ERROR_25);
211+
SniLoadHandle.LastError = new SniError(SniProviders.TCP_PROV, 0, SniCommon.InvalidConnStringError, Strings.SNI_ERROR_25);
212212
return null;
213213
}
214214

@@ -224,7 +224,7 @@ private static SniTcpHandle CreateTcpHandle(
224224
}
225225
catch (SocketException se)
226226
{
227-
SniLoadHandle.SingletonInstance.LastError = new SniError(SniProviders.TCP_PROV, SniCommon.ErrorLocatingServerInstance, se);
227+
SniLoadHandle.LastError = new SniError(SniProviders.TCP_PROV, SniCommon.ErrorLocatingServerInstance, se);
228228
return null;
229229
}
230230
}
@@ -268,7 +268,7 @@ private static SniNpHandle CreateNpHandle(DataSource details, TimeoutTimer timeo
268268
/// <returns></returns>
269269
internal SniError GetLastError()
270270
{
271-
return SniLoadHandle.SingletonInstance.LastError;
271+
return SniLoadHandle.LastError;
272272
}
273273

274274
/// <summary>
@@ -448,7 +448,7 @@ internal static string GetLocalDBInstance(string dataSource, out bool error)
448448
}
449449
else if (index > 0)
450450
{
451-
SniLoadHandle.SingletonInstance.LastError = new SniError(SniProviders.INVALID_PROV, 0, SniCommon.ErrorLocatingServerInstance, Strings.SNI_ERROR_26);
451+
SniLoadHandle.LastError = new SniError(SniProviders.INVALID_PROV, 0, SniCommon.ErrorLocatingServerInstance, Strings.SNI_ERROR_26);
452452
SqlClientEventSource.Log.TrySNITraceEvent(nameof(SniProxy), EventType.ERR, "Incompatible use of prefix with LocalDb: '{0}'", dataSource);
453453
error = true;
454454
}
@@ -467,7 +467,7 @@ internal static string GetLocalDBInstance(string dataSource, out bool error)
467467
}
468468
else
469469
{
470-
SniLoadHandle.SingletonInstance.LastError = new SniError(SniProviders.INVALID_PROV, 0, SniCommon.LocalDBNoInstanceName, Strings.SNI_ERROR_51);
470+
SniLoadHandle.LastError = new SniError(SniProviders.INVALID_PROV, 0, SniCommon.LocalDBNoInstanceName, Strings.SNI_ERROR_51);
471471
error = true;
472472
}
473473
}
@@ -594,7 +594,7 @@ private bool InferConnectionDetails()
594594

595595
private void ReportSNIError(SniProviders provider)
596596
{
597-
SniLoadHandle.SingletonInstance.LastError = new SniError(provider, 0, SniCommon.InvalidConnStringError, Strings.SNI_ERROR_25);
597+
SniLoadHandle.LastError = new SniError(provider, 0, SniCommon.InvalidConnStringError, Strings.SNI_ERROR_25);
598598
IsBadDataSource = true;
599599
}
600600

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObjectFactory.Unix.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ internal sealed class TdsParserStateObjectFactory
1010
{
1111
public static readonly TdsParserStateObjectFactory Singleton = new TdsParserStateObjectFactory();
1212

13-
public EncryptionOptions EncryptionOptions => ManagedSni.SniLoadHandle.SingletonInstance.Options;
13+
public EncryptionOptions EncryptionOptions => ManagedSni.SniLoadHandle.Options;
1414

15-
public uint SNIStatus => ManagedSni.SniLoadHandle.SingletonInstance.Status;
15+
public uint SNIStatus => ManagedSni.SniLoadHandle.Status;
1616

1717
/// <summary>
1818
/// Verify client encryption possibility.
1919
/// </summary>
20-
public bool ClientOSEncryptionSupport => ManagedSni.SniLoadHandle.SingletonInstance.ClientOSEncryptionSupport;
20+
public bool ClientOSEncryptionSupport => ManagedSni.SniLoadHandle.ClientOSEncryptionSupport;
2121

2222
public TdsParserStateObject CreateTdsParserStateObject(TdsParser parser)
2323
{

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObjectFactory.Windows.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ internal sealed class TdsParserStateObjectFactory
1515

1616
public EncryptionOptions EncryptionOptions =>
1717
#if NET
18-
LocalAppContextSwitches.UseManagedNetworking ? ManagedSni.SniLoadHandle.SingletonInstance.Options : SNILoadHandle.SingletonInstance.Options;
18+
LocalAppContextSwitches.UseManagedNetworking ? ManagedSni.SniLoadHandle.Options : SNILoadHandle.SingletonInstance.Options;
1919
#else
2020
SNILoadHandle.SingletonInstance.Options;
2121
#endif
2222

2323
public uint SNIStatus =>
2424
#if NET
25-
LocalAppContextSwitches.UseManagedNetworking ? ManagedSni.SniLoadHandle.SingletonInstance.Status : SNILoadHandle.SingletonInstance.Status;
25+
LocalAppContextSwitches.UseManagedNetworking ? ManagedSni.SniLoadHandle.Status : SNILoadHandle.SingletonInstance.Status;
2626
#else
2727
SNILoadHandle.SingletonInstance.Status;
2828
#endif
@@ -32,7 +32,7 @@ internal sealed class TdsParserStateObjectFactory
3232
/// </summary>
3333
public bool ClientOSEncryptionSupport =>
3434
#if NET
35-
LocalAppContextSwitches.UseManagedNetworking ? ManagedSni.SniLoadHandle.SingletonInstance.ClientOSEncryptionSupport : SNILoadHandle.SingletonInstance.ClientOSEncryptionSupport;
35+
LocalAppContextSwitches.UseManagedNetworking ? ManagedSni.SniLoadHandle.ClientOSEncryptionSupport : SNILoadHandle.SingletonInstance.ClientOSEncryptionSupport;
3636
#else
3737
SNILoadHandle.SingletonInstance.ClientOSEncryptionSupport;
3838
#endif

0 commit comments

Comments
 (0)