SqlClient: Could not load type 'System.Configuration.ConfigurationSection' from assembly 'System.Configuration.ConfigurationManager'

Describe the bug

If using Microsoft.Data.SqlClient without also manually adding a reference to System.Configuration.ConfigurationManager, a simple call to new SqlConnection with a raw connection string and then conn.Open() causes the following exception:

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'Microsoft.Data.SqlClient.SqlAuthenticationProviderManager' threw an exception.
  Source=Microsoft.Data.SqlClient
  StackTrace:
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken, DbConnectionPool pool)
   at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
   at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)
   at Microsoft.Data.SqlClient.SqlConnection.Open()
   at Test.Program.Main(String[] args) in D:\Desktop\Test\Test\Program.cs:line 11

Inner Exception 1:
TypeLoadException: Could not load type 'System.Configuration.ConfigurationSection' from assembly 'System.Configuration.ConfigurationManager, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

To reproduce

Repro solution: Repro.zip

The important code:

using (var conn = new SqlConnection("Data Source=(LocalDb)\\MSSQLLocalDB;Initial Catalog=MyDb;Integrated Security=True"))
{
    conn.Open();
}

Adding a reference to System.Configuration.ConfigurationManager 5.0.0 (and probably earlier versions, too) fixes the error.

I’m confused about two things:

  • If Microsoft.Data.SqlClient depends on System.Configuration.ConfigurationManager, why isn’t this in the nuspec, so that System.Configuration.ConfigurationManager is installed alongside Microsoft.Data.SqlClient?
  • Why does Microsoft.Data.SqlClient use the configuration system at all when I just pass a raw connection string? Surely this isn’t necessary?

Expected behavior

The code runs without issues.

Further technical details

Microsoft.Data.SqlClient version: 2.0.1 .NET target: netcoreapp3.1 SQL Server version: Not important Operating system: Windows

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 21 (10 by maintainers)

Most upvoted comments

Excellent tip, I should have thought of that! That fixed the problem.

@cmeeren could you try deleting your nuget cache and then try with the 4.7.0 again?