Skip to content

Commit 8815adb

Browse files
edwardnealMichelZ
authored andcommitted
Merge ContextConnection handling (#2862)
1 parent 4f42345 commit 8815adb

File tree

11 files changed

+62
-148
lines changed

11 files changed

+62
-148
lines changed

doc/snippets/Microsoft.Data.SqlClient/SqlConnectionStringBuilder.xml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -587,22 +587,6 @@
587587
</example>
588588
<exception cref="T:System.ArgumentNullException"><paramref name="keyword" /> is null (<see langword="Nothing" /> in Visual Basic)</exception>
589589
</ContainsKey>
590-
<ContextConnection>
591-
<summary>
592-
Obsolete. Gets or sets a value that indicates whether a client/server or in-process connection to SQL Server should be made.
593-
</summary>
594-
<value>
595-
The value of the <see cref="P:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.ContextConnection" /> property, or <see langword="false" /> if none has been supplied.
596-
</value>
597-
<remarks>
598-
<para>
599-
This property corresponds to the "Context Connection" key within the connection string.
600-
</para>
601-
<note type="note">
602-
The <see cref="P:Microsoft.Data.SqlClient.SqlConnection.DataSource" /> property returns <see langword="null" /> if the connection string for the <see cref="T:Microsoft.Data.SqlClient.SqlConnection" /> is "context connection=true".
603-
</note>
604-
</remarks>
605-
</ContextConnection>
606590
<CurrentLanguage>
607591
<summary>
608592
Gets or sets the language used for database server warning or error messages..

src/Microsoft.Data.SqlClient/netfx/ref/Microsoft.Data.SqlClient.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,11 +1030,6 @@ public SqlConnectionStringBuilder(string connectionString) { }
10301030
[System.ComponentModel.DisplayNameAttribute("Connect Timeout")]
10311031
[System.ComponentModel.RefreshPropertiesAttribute(System.ComponentModel.RefreshProperties.All)]
10321032
public int ConnectTimeout { get { throw null; } set { } }
1033-
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnectionStringBuilder.xml' path='docs/members[@name="SqlConnectionStringBuilder"]/ContextConnection/*'/>
1034-
[System.ComponentModel.DisplayNameAttribute("Context Connection")]
1035-
[System.ComponentModel.RefreshPropertiesAttribute(System.ComponentModel.RefreshProperties.All)]
1036-
[System.ObsoleteAttribute("ContextConnection has been deprecated. SqlConnection will ignore the 'Context Connection' keyword.")]
1037-
public bool ContextConnection { get { throw null; } set { } }
10381033
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnectionStringBuilder.xml' path='docs/members[@name="SqlConnectionStringBuilder"]/CurrentLanguage/*'/>
10391034
[System.ComponentModel.DisplayNameAttribute("Current Language")]
10401035
[System.ComponentModel.RefreshPropertiesAttribute(System.ComponentModel.RefreshProperties.All)]

src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/DbConnectionStringCommon.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,10 +955,10 @@ internal static class DbConnectionStringDefaults
955955
internal const string AttachDBFilename = "";
956956
internal const int CommandTimeout = 30;
957957
internal const int ConnectTimeout = 15;
958+
internal const bool ContextConnection = false;
958959

959960
#if NETFRAMEWORK
960961
internal const bool ConnectionReset = true;
961-
internal const bool ContextConnection = false;
962962
internal static readonly bool TransparentNetworkIPResolution = !LocalAppContextSwitches.DisableTNIRByDefault;
963963
internal const string NetworkLibrary = "";
964964
#endif

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommandSet.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,18 +249,11 @@ internal int ExecuteNonQuery()
249249
{
250250
#if NETFRAMEWORK
251251
SqlConnection.ExecutePermission.Demand();
252-
#else
253-
ValidateCommandBehavior(nameof(ExecuteNonQuery), CommandBehavior.Default);
254252
#endif
255253
using (TryEventScope.Create("SqlCommandSet.ExecuteNonQuery | API | Object Id {0}, Commands executed in Batch RPC mode", ObjectID))
256254
{
257-
#if NETFRAMEWORK
258-
if (Connection.IsContextConnection)
259-
{
260-
throw SQL.BatchedUpdatesNotAvailableOnContextConnection();
261-
}
262255
ValidateCommandBehavior(nameof(ExecuteNonQuery), CommandBehavior.Default);
263-
#endif
256+
264257
BatchCommand.SetBatchRPCMode(true);
265258
BatchCommand.Parameters.Clear();
266259
for (int index = 0; index < _commandList.Count; index++)

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionString.cs

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ internal static class DEFAULT
6363
internal static readonly SqlConnectionIPAddressPreference IpAddressPreference = DbConnectionStringDefaults.IPAddressPreference;
6464
internal const string ServerSPN = DbConnectionStringDefaults.ServerSPN;
6565
internal const string FailoverPartnerSPN = DbConnectionStringDefaults.FailoverPartnerSPN;
66+
internal const bool Context_Connection = DbConnectionStringDefaults.ContextConnection;
6667
#if NETFRAMEWORK
6768
internal static readonly bool TransparentNetworkIPResolution = DbConnectionStringDefaults.TransparentNetworkIPResolution;
6869
internal const bool Connection_Reset = DbConnectionStringDefaults.ConnectionReset;
69-
internal const bool Context_Connection = DbConnectionStringDefaults.ContextConnection;
7070
internal const string Network_Library = DbConnectionStringDefaults.NetworkLibrary;
7171
#endif // NETFRAMEWORK
7272
}
@@ -261,6 +261,7 @@ internal static class TRANSACTIONBINDING
261261
private readonly ApplicationIntent _applicationIntent;
262262
private readonly string _applicationName;
263263
private readonly string _attachDBFileName;
264+
private readonly bool _contextConnection;
264265
private readonly string _currentLanguage;
265266
private readonly string _dataSource;
266267
private readonly string _localDBInstance; // created based on datasource, set to NULL if datasource is not LocalDB
@@ -289,11 +290,8 @@ internal static class TRANSACTIONBINDING
289290
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
290291
internal SqlConnectionString(string connectionString) : base(connectionString, GetParseSynonyms())
291292
{
292-
#if NETFRAMEWORK
293-
bool runningInProc = InOutOfProcHelper.InProc;
294-
#else
293+
#if !NETFRAMEWORK
295294
ThrowUnsupportedIfKeywordSet(KEY.Connection_Reset);
296-
ThrowUnsupportedIfKeywordSet(KEY.Context_Connection);
297295

298296
// Network Library has its own special error message
299297
if (ContainsKey(KEY.Network_Library))
@@ -325,6 +323,7 @@ internal SqlConnectionString(string connectionString) : base(connectionString, G
325323
_applicationIntent = ConvertValueToApplicationIntent();
326324
_applicationName = ConvertValueToString(KEY.Application_Name, DEFAULT.Application_Name);
327325
_attachDBFileName = ConvertValueToString(KEY.AttachDBFilename, DEFAULT.AttachDBFilename);
326+
_contextConnection = ConvertValueToBoolean(KEY.Context_Connection, DEFAULT.Context_Connection);
328327
_currentLanguage = ConvertValueToString(KEY.Current_Language, DEFAULT.Current_Language);
329328
_dataSource = ConvertValueToString(KEY.Data_Source, DEFAULT.Data_Source);
330329
_localDBInstance = LocalDBAPI.GetLocalDbInstanceNameFromServerName(_dataSource);
@@ -349,6 +348,11 @@ internal SqlConnectionString(string connectionString) : base(connectionString, G
349348
_userID = ConvertValueToString(KEY.User_ID, DEFAULT.User_ID);
350349
_workstationId = ConvertValueToString(KEY.Workstation_Id, null);
351350

351+
if (_contextConnection)
352+
{
353+
throw SQL.ContextConnectionIsUnsupported();
354+
}
355+
352356
if (_loadBalanceTimeout < 0)
353357
{
354358
throw ADP.InvalidConnectionOptionValue(KEY.Load_Balance_Timeout);
@@ -386,35 +390,9 @@ internal SqlConnectionString(string connectionString) : base(connectionString, G
386390
#if NETFRAMEWORK
387391
// SQLPT 41700: Ignore ResetConnection=False (still validate the keyword/value)
388392
_connectionReset = ConvertValueToBoolean(KEY.Connection_Reset, DEFAULT.Connection_Reset);
389-
_contextConnection = ConvertValueToBoolean(KEY.Context_Connection, DEFAULT.Context_Connection);
390-
_encrypt = ConvertValueToSqlConnectionEncrypt();
391-
_enlist = ConvertValueToBoolean(KEY.Enlist, ADP.s_isWindowsNT);
392393
_transparentNetworkIPResolution = ConvertValueToBoolean(KEY.TransparentNetworkIPResolution, DEFAULT.TransparentNetworkIPResolution);
393394
_networkLibrary = ConvertValueToString(KEY.Network_Library, null);
394395

395-
if (_contextConnection)
396-
{
397-
// We have to be running in the engine for you to request a
398-
// context connection.
399-
400-
if (!runningInProc)
401-
{
402-
throw SQL.ContextUnavailableOutOfProc();
403-
}
404-
405-
// When using a context connection, we need to ensure that no
406-
// other connection string keywords are specified.
407-
408-
foreach (KeyValuePair<string, string> entry in Parsetable)
409-
{
410-
if (entry.Key != KEY.Context_Connection &&
411-
entry.Key != KEY.Type_System_Version)
412-
{
413-
throw SQL.ContextAllowsLimitedKeywords();
414-
}
415-
}
416-
}
417-
418396
if (_networkLibrary != null)
419397
{ // MDAC 83525
420398
string networkLibrary = _networkLibrary.Trim().ToLower(CultureInfo.InvariantCulture);
@@ -522,12 +500,6 @@ internal SqlConnectionString(string connectionString) : base(connectionString, G
522500
}
523501
else if (typeSystemVersionString.Equals(TYPESYSTEMVERSION.SQL_Server_2000, StringComparison.OrdinalIgnoreCase))
524502
{
525-
#if NETFRAMEWORK
526-
if (_contextConnection)
527-
{
528-
throw SQL.ContextAllowsOnlyTypeSystem2005();
529-
}
530-
#endif
531503
_typeSystemVersion = TypeSystem.SQLServer2000;
532504
}
533505
else if (typeSystemVersionString.Equals(TYPESYSTEMVERSION.SQL_Server_2005, StringComparison.OrdinalIgnoreCase))
@@ -653,6 +625,7 @@ internal SqlConnectionString(SqlConnectionString connectionOptions, string dataS
653625
_packetSize = connectionOptions._packetSize;
654626
_applicationName = connectionOptions._applicationName;
655627
_attachDBFileName = connectionOptions._attachDBFileName;
628+
_contextConnection = connectionOptions._contextConnection;
656629
_currentLanguage = connectionOptions._currentLanguage;
657630
_dataSource = dataSource;
658631
_localDBInstance = LocalDBAPI.GetLocalDbInstanceNameFromServerName(_dataSource);
@@ -676,7 +649,6 @@ internal SqlConnectionString(SqlConnectionString connectionOptions, string dataS
676649
_hostNameInCertificate = connectionOptions._hostNameInCertificate;
677650
#if NETFRAMEWORK
678651
_connectionReset = connectionOptions._connectionReset;
679-
_contextConnection = connectionOptions._contextConnection;
680652
_transparentNetworkIPResolution = connectionOptions._transparentNetworkIPResolution;
681653
_networkLibrary = connectionOptions._networkLibrary;
682654
_typeSystemAssemblyVersion = connectionOptions._typeSystemAssemblyVersion;
@@ -721,6 +693,9 @@ internal SqlConnectionString(SqlConnectionString connectionOptions, string dataS
721693
internal ApplicationIntent ApplicationIntent => _applicationIntent;
722694
internal string ApplicationName => _applicationName;
723695
internal string AttachDBFilename => _attachDBFileName;
696+
// Return a constant value rather than _contextConnection. This allows the JIT to trim
697+
// the code paths referencing it.
698+
internal bool ContextConnection => false;
724699
internal string CurrentLanguage => _currentLanguage;
725700
internal string DataSource => _dataSource;
726701
internal string LocalDBInstance => _localDBInstance;
@@ -1192,11 +1167,9 @@ internal SqlConnectionEncryptOption ConvertValueToEncrypt()
11921167
}
11931168

11941169
private readonly bool _connectionReset;
1195-
private readonly bool _contextConnection;
11961170
private readonly bool _transparentNetworkIPResolution;
11971171
private readonly string _networkLibrary;
11981172

1199-
internal bool ContextConnection => _contextConnection;
12001173
internal bool TransparentNetworkIPResolution => _transparentNetworkIPResolution;
12011174
internal string NetworkLibrary => _networkLibrary;
12021175

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionStringBuilder.cs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ private enum Keywords
6868
IPAddressPreference,
6969
ServerSPN,
7070
FailoverPartnerSPN,
71+
ContextConnection,
7172
#if NETFRAMEWORK
7273
ConnectionReset,
7374
NetworkLibrary,
74-
ContextConnection,
7575
TransparentNetworkIPResolution,
7676
#endif
7777
// keep the KeywordsCount value last
@@ -129,7 +129,6 @@ private enum Keywords
129129

130130
#if NETFRAMEWORK
131131
private bool _connectionReset = DbConnectionStringDefaults.ConnectionReset;
132-
private bool _contextConnection = DbConnectionStringDefaults.ContextConnection;
133132
private bool _transparentNetworkIPResolution = DbConnectionStringDefaults.TransparentNetworkIPResolution;
134133
private string _networkLibrary = DbConnectionStringDefaults.NetworkLibrary;
135134
#else
@@ -181,10 +180,10 @@ private static string[] CreateValidKeywords()
181180
validKeywords[(int)Keywords.IPAddressPreference] = DbConnectionStringKeywords.IPAddressPreference;
182181
validKeywords[(int)Keywords.ServerSPN] = DbConnectionStringKeywords.ServerSPN;
183182
validKeywords[(int)Keywords.FailoverPartnerSPN] = DbConnectionStringKeywords.FailoverPartnerSPN;
183+
validKeywords[(int)Keywords.ContextConnection] = DbConnectionStringKeywords.ContextConnection;
184184
#if NETFRAMEWORK
185185
validKeywords[(int)Keywords.ConnectionReset] = DbConnectionStringKeywords.ConnectionReset;
186186
validKeywords[(int)Keywords.NetworkLibrary] = DbConnectionStringKeywords.NetworkLibrary;
187-
validKeywords[(int)Keywords.ContextConnection] = DbConnectionStringKeywords.ContextConnection;
188187
validKeywords[(int)Keywords.TransparentNetworkIPResolution] = DbConnectionStringKeywords.TransparentNetworkIPResolution;
189188
#endif
190189
return validKeywords;
@@ -234,10 +233,9 @@ private static Dictionary<string, Keywords> CreateKeywordsDictionary()
234233
{ DbConnectionStringKeywords.IPAddressPreference, Keywords.IPAddressPreference },
235234
{ DbConnectionStringKeywords.ServerSPN, Keywords.ServerSPN },
236235
{ DbConnectionStringKeywords.FailoverPartnerSPN, Keywords.FailoverPartnerSPN },
237-
236+
{ DbConnectionStringKeywords.ContextConnection, Keywords.ContextConnection },
238237
#if NETFRAMEWORK
239238
{ DbConnectionStringKeywords.ConnectionReset, Keywords.ConnectionReset },
240-
{ DbConnectionStringKeywords.ContextConnection, Keywords.ContextConnection },
241239
{ DbConnectionStringKeywords.TransparentNetworkIPResolution, Keywords.TransparentNetworkIPResolution },
242240
{ DbConnectionStringKeywords.NetworkLibrary, Keywords.NetworkLibrary },
243241
{ DbConnectionStringSynonyms.NET, Keywords.NetworkLibrary },
@@ -391,12 +389,12 @@ private object GetAt(Keywords index)
391389
return ServerSPN;
392390
case Keywords.FailoverPartnerSPN:
393391
return FailoverPartnerSPN;
392+
case Keywords.ContextConnection:
393+
return false;
394394
#if NETFRAMEWORK
395395
#pragma warning disable 618 // Obsolete properties
396396
case Keywords.ConnectionReset:
397397
return ConnectionReset;
398-
case Keywords.ContextConnection:
399-
return ContextConnection;
400398
#pragma warning restore 618
401399
case Keywords.TransparentNetworkIPResolution:
402400
return TransparentNetworkIPResolution;
@@ -544,13 +542,12 @@ private void Reset(Keywords index)
544542
case Keywords.FailoverPartnerSPN:
545543
_failoverPartnerSPN = DbConnectionStringDefaults.FailoverPartnerSPN;
546544
break;
545+
case Keywords.ContextConnection:
546+
break;
547547
#if NETFRAMEWORK
548548
case Keywords.ConnectionReset:
549549
_connectionReset = DbConnectionStringDefaults.ConnectionReset;
550550
break;
551-
case Keywords.ContextConnection:
552-
_contextConnection = DbConnectionStringDefaults.ContextConnection;
553-
break;
554551
case Keywords.TransparentNetworkIPResolution:
555552
_transparentNetworkIPResolution = DbConnectionStringDefaults.TransparentNetworkIPResolution;
556553
break;
@@ -893,7 +890,6 @@ public override StandardValuesCollection GetStandardValues(ITypeDescriptorContex
893890
#else
894891
private static readonly string[] s_notSupportedKeywords = {
895892
DbConnectionStringKeywords.ConnectionReset,
896-
DbConnectionStringKeywords.ContextConnection,
897893
DbConnectionStringKeywords.TransactionBinding,
898894
DbConnectionStringKeywords.TransparentNetworkIPResolution,
899895
DbConnectionStringSynonyms.TRANSPARENTNETWORKIPRESOLUTION,
@@ -1056,14 +1052,17 @@ public override object this[string keyword]
10561052
case Keywords.FailoverPartnerSPN:
10571053
FailoverPartnerSPN = ConvertToString(value);
10581054
break;
1055+
case Keywords.ContextConnection:
1056+
if (ConvertToBoolean(value))
1057+
{
1058+
throw SQL.ContextConnectionIsUnsupported();
1059+
}
1060+
break;
10591061
#if NETFRAMEWORK
10601062
#pragma warning disable 618 // Obsolete properties
10611063
case Keywords.ConnectionReset:
10621064
ConnectionReset = ConvertToBoolean(value);
10631065
break;
1064-
case Keywords.ContextConnection:
1065-
ContextConnection = ConvertToBoolean(value);
1066-
break;
10671066
#pragma warning restore 618
10681067
case Keywords.NetworkLibrary:
10691068
NetworkLibrary = ConvertToString(value);
@@ -1849,22 +1848,6 @@ public bool ConnectionReset
18491848
}
18501849
}
18511850

1852-
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnectionStringBuilder.xml' path='docs/members[@name="SqlConnectionStringBuilder"]/ContextConnection/*' />
1853-
[DisplayName(DbConnectionStringKeywords.ContextConnection)]
1854-
[Obsolete("ContextConnection has been deprecated. SqlConnection will ignore the 'Context Connection' keyword.")]
1855-
[ResCategory(StringsHelper.ResourceNames.DataCategory_Source)]
1856-
[ResDescription(StringsHelper.ResourceNames.DbConnectionString_ContextConnection)]
1857-
[RefreshProperties(RefreshProperties.All)]
1858-
public bool ContextConnection
1859-
{
1860-
get => _contextConnection;
1861-
set
1862-
{
1863-
SetValue(DbConnectionStringKeywords.ContextConnection, value);
1864-
_contextConnection = value;
1865-
}
1866-
}
1867-
18681851
/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnectionStringBuilder.xml' path='docs/members[@name="SqlConnectionStringBuilder"]/TransparentNetworkIPResolution/*' />
18691852
[DisplayName(DbConnectionStringKeywords.TransparentNetworkIPResolution)]
18701853
[ResCategory(StringsHelper.ResourceNames.DataCategory_Source)]

0 commit comments

Comments
 (0)