Skip to content

Commit 9f23105

Browse files
[Release/2.0] Fix configSection collision with System.Data.SqlClient type (#701)
1 parent 9ce5e66 commit 9f23105

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlAuthenticationProviderManager.NetCoreApp.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@ static SqlAuthenticationProviderManager()
2020

2121
try
2222
{
23-
configurationSection = (SqlAuthenticationProviderConfigurationSection)ConfigurationManager.GetSection(SqlAuthenticationProviderConfigurationSection.Name);
23+
// New configuration section for Microsoft.Data.SqlClient accepted to avoid conflicts with older one.
24+
configurationSection = FetchConfigurationSection<SqlClientAuthenticationProviderConfigurationSection>(SqlClientAuthenticationProviderConfigurationSection.Name);
25+
if (null == configurationSection)
26+
{
27+
// If section is not yet found, try with old Configuration Section name for backwards compatibility
28+
configurationSection = FetchConfigurationSection<SqlAuthenticationProviderConfigurationSection>(SqlAuthenticationProviderConfigurationSection.Name);
29+
}
2430
}
2531
catch (ConfigurationErrorsException e)
2632
{
2733
// Don't throw an error for invalid config files
28-
SqlClientEventSource.Log.TraceEvent("Unable to load custom SqlAuthenticationProviders. ConfigurationManager failed to load due to configuration errors: {0}", e);
34+
SqlClientEventSource.Log.TraceEvent("Unable to load custom SqlAuthenticationProviders or SqlClientAuthenticationProviders. ConfigurationManager failed to load due to configuration errors: {0}", e);
2935
}
3036

3137
Instance = new SqlAuthenticationProviderManager(configurationSection);
@@ -48,7 +54,7 @@ public SqlAuthenticationProviderManager(SqlAuthenticationProviderConfigurationSe
4854

4955
if (configSection == null)
5056
{
51-
_sqlAuthLogger.LogInfo(_typeName, methodName, "No SqlAuthProviders configuration section found.");
57+
_sqlAuthLogger.LogInfo(_typeName, methodName, "Neither SqlClientAuthenticationProviders nor SqlAuthenticationProviders configuration section found.");
5258
return;
5359
}
5460

@@ -104,6 +110,24 @@ public SqlAuthenticationProviderManager(SqlAuthenticationProviderConfigurationSe
104110
}
105111
}
106112

113+
private static T FetchConfigurationSection<T>(string name)
114+
{
115+
Type t = typeof(T);
116+
object section = ConfigurationManager.GetSection(name);
117+
if (null != section)
118+
{
119+
if (section is ConfigurationSection configSection && configSection.GetType() == t)
120+
{
121+
return (T)section;
122+
}
123+
else
124+
{
125+
SqlClientEventSource.Log.TraceEvent("Found a custom {0} configuration but it is not of type {1}.", name, t.FullName);
126+
}
127+
}
128+
return default;
129+
}
130+
107131
private static SqlAuthenticationMethod AuthenticationEnumFromString(string authentication)
108132
{
109133
switch (authentication.ToLowerInvariant())
@@ -140,5 +164,13 @@ internal class SqlAuthenticationProviderConfigurationSection : ConfigurationSect
140164
[ConfigurationProperty("initializerType")]
141165
public string InitializerType => base["initializerType"] as string;
142166
}
167+
168+
/// <summary>
169+
/// The configuration section definition for reading app.config.
170+
/// </summary>
171+
internal class SqlClientAuthenticationProviderConfigurationSection : SqlAuthenticationProviderConfigurationSection
172+
{
173+
public new const string Name = "SqlClientAuthenticationProviders";
174+
}
143175
}
144176
}

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,18 @@ static SqlAuthenticationProviderManager()
2828
SqlAuthenticationProviderConfigurationSection configurationSection = null;
2929
try
3030
{
31-
configurationSection = (SqlAuthenticationProviderConfigurationSection)ConfigurationManager.GetSection(SqlAuthenticationProviderConfigurationSection.Name);
31+
// New configuration section for Microsoft.Data.SqlClient accepted to avoid conflicts with older one.
32+
configurationSection = FetchConfigurationSection<SqlClientAuthenticationProviderConfigurationSection>(SqlClientAuthenticationProviderConfigurationSection.Name);
33+
if (null == configurationSection)
34+
{
35+
// If section is not yet found, try with old Configuration Section name for backwards compatibility
36+
configurationSection = FetchConfigurationSection<SqlAuthenticationProviderConfigurationSection>(SqlAuthenticationProviderConfigurationSection.Name);
37+
}
3238
}
3339
catch (ConfigurationErrorsException e)
3440
{
3541
// Don't throw an error for invalid config files
36-
SqlClientEventSource.Log.TraceEvent("Unable to load custom SqlAuthenticationProviders. ConfigurationManager failed to load due to configuration errors: {0}", e);
42+
SqlClientEventSource.Log.TraceEvent("Unable to load custom SqlAuthenticationProviders or SqlClientAuthenticationProviders. ConfigurationManager failed to load due to configuration errors: {0}", e);
3743
}
3844
Instance = new SqlAuthenticationProviderManager(configurationSection);
3945
Instance.SetProvider(SqlAuthenticationMethod.ActiveDirectoryIntegrated, activeDirectoryAuthProvider);
@@ -62,7 +68,7 @@ public SqlAuthenticationProviderManager(SqlAuthenticationProviderConfigurationSe
6268

6369
if (configSection == null)
6470
{
65-
_sqlAuthLogger.LogInfo(_typeName, methodName, "No SqlAuthProviders configuration section found.");
71+
_sqlAuthLogger.LogInfo(_typeName, methodName, "Neither SqlClientAuthenticationProviders nor SqlAuthenticationProviders configuration section found.");
6672
return;
6773
}
6874

@@ -164,6 +170,24 @@ public bool SetProvider(SqlAuthenticationMethod authenticationMethod, SqlAuthent
164170
return true;
165171
}
166172

173+
private static T FetchConfigurationSection<T>(string name)
174+
{
175+
Type t = typeof(T);
176+
object section = ConfigurationManager.GetSection(name);
177+
if (null != section)
178+
{
179+
if (section is ConfigurationSection configSection && configSection.GetType() == t)
180+
{
181+
return (T)section;
182+
}
183+
else
184+
{
185+
SqlClientEventSource.Log.TraceEvent("Found a custom {0} configuration but it is not of type {1}.", name, t.FullName);
186+
}
187+
}
188+
return default;
189+
}
190+
167191
private static SqlAuthenticationMethod AuthenticationEnumFromString(string authentication)
168192
{
169193
switch (authentication.ToLowerInvariant())
@@ -209,6 +233,13 @@ internal class SqlAuthenticationProviderConfigurationSection : ConfigurationSect
209233
public string InitializerType => base["initializerType"] as string;
210234
}
211235

236+
/// <summary>
237+
/// The configuration section definition for reading app.config.
238+
/// </summary>
239+
internal class SqlClientAuthenticationProviderConfigurationSection : SqlAuthenticationProviderConfigurationSection
240+
{
241+
public new const string Name = "SqlClientAuthenticationProviders";
242+
}
212243

213244
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlAuthenticationInitializer.xml' path='docs/members[@name="SqlAuthenticationInitializer"]/SqlAuthenticationInitializer/*'/>
214245
public abstract class SqlAuthenticationInitializer

tools/props/Versions.props

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
<Project ToolsVersion="Latest" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<AssemblyFileVersion Condition="'$(AssemblyFileVersion)' == ''">1.0.0.0</AssemblyFileVersion>
5-
<AssemblyVersion>$(AssemblyFileVersion)</AssemblyVersion>
5+
<!-- This Assembly version corresponds to version of Microsoft.Data.SqlClient v2.0.0 Assembly. -->
6+
<!-- Should only be changed in future when a non-backwards compatible driver is released. -->
7+
<!-- Future Assembly Version values shall be Major.0.0.0; e.g. 3.0.0.0 -->
8+
<AssemblyVersion>2.0.20168.4</AssemblyVersion>
69
<MicrosoftDotNetGenAPIPackageVersion>1.0.0-beta.18578.2</MicrosoftDotNetGenAPIPackageVersion>
710
<NugetPackageVersion Condition="'$(NugetPackageVersion)' == ''">2.0.0-dev</NugetPackageVersion>
811
<Version>$(NugetPackageVersion)</Version>

0 commit comments

Comments
 (0)