Sqlclient: The type initializer for 'Microsoft.Data.SqlClient.SqlConnection' threw an exception.

Created on 15 Aug 2019  路  12Comments  路  Source: dotnet/SqlClient

Describe the bug

Exception message: The type initializer for 'Microsoft.Data.SqlClient.SqlConnection' threw an exception.
Stack trace: System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SqlConnection' threw an exception.
 ---> System.InvalidOperationException: Failed to read the configuration section for enclave providers. Make sure the section is correctly formatted in your application configuration file. Error Message: Configuration system failed to initialize
 ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize
 ---> System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Could not load type 'System.ComponentModel.AsyncCompletedEventArgs' from assembly 'System.Net.WebHeaderCollection, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. (/root/HaproxyUpdater2/Config/machine.config)
 ---> System.TypeLoadException: Could not load type 'System.ComponentModel.AsyncCompletedEventArgs' from assembly 'System.Net.WebHeaderCollection, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
   at System.Configuration.ClientConfigurationHost.OpenStreamForRead(String streamName)
   at System.Configuration.Internal.DelegatingConfigHost.OpenStreamForRead(String streamName)
   at System.Configuration.ImplicitMachineConfigHost.OpenStreamForRead(String streamName)
   at System.Configuration.BaseConfigurationRecord.InitConfigFromFile()
   --- End of inner exception stack trace ---
   at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
   at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
   at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
   at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   --- End of inner exception stack trace ---
   at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   at System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(String sectionName)
   at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at Microsoft.Data.SqlClient.SqlConnection..cctor()
   --- End of inner exception stack trace ---
   at Microsoft.Data.SqlClient.SqlConnection..cctor()
   --- End of inner exception stack trace ---
   at Microsoft.Data.SqlClient.SqlConnection..ctor(String connectionString)

To reproduce

Include a complete code listing (or project/solution) that we can run to reproduce the issue.

Partial code listings, or multiple fragments of code, will slow down our response or cause us to push the issue back to you to provide code to reproduce the issue.

c# new SqlConnection()

Expected behavior

No exception

Further technical details

Microsoft.Data.SqlClient version: 1.0.19221.1-Preview
.NET target: Core 3.0 preview8
SQL Server version: SQL Server 2016
Operating system: Centos7

Additional context

Most helpful comment

Hi @pengweiqhca

FYI.. We were able to identity and fix the issue with "System.Configuration.ConfigurationManager" references for .NET Framework targets and the fix will be available in GA release for 1.0.

All 12 comments

 ---> System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Could not load type 'System.ComponentModel.AsyncCompletedEventArgs' from assembly 'System.Net.WebHeaderCollection, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. (/root/HaproxyUpdater2/Config/machine.config)
 ---> System.TypeLoadException: Could not load type 'System.ComponentModel.AsyncCompletedEventArgs' from assembly 'System.Net.WebHeaderCollection, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

The type System.ComponentModel.AsyncCompletedEventArgs is not in System.Net.WebHeaderCollection. It looks like you're missing a dependency. SqlClient causes the configuration system to be initialized and the configuration system fails and throws. It is unlikely this is an SqlClient specific problem. If you try to use configuration directly it should fail in the same way.

Hi @pengweiqhca

Did you try @Wraith2 's suggestion?

System.Configuration.ConfigurationManager has some bug, I has rollback to use System.Data.SqlClient

Hi @pengweiqhca

FYI.. We were able to identity and fix the issue with "System.Configuration.ConfigurationManager" references for .NET Framework targets and the fix will be available in GA release for 1.0.

I still have this error with version 1.0.19269.1 by trying to create a SqlConnection with a connection string

using var connection = new SqlConnection(executionContext.ConnectionString);

With System.Data.SqlClient package worked without problems. I just switch the package and I have the error bellow.

More details: .netcoreapp 3.0, runtime win10-x64, Windows Server 2016

image

@glucaci unfortunately I wasn't able to reproduce the issue with the latest driver. What is the version of System.Data.SqlClient that was working for you before? Do you know what is the version of System.Configuration.ConfigurationManager.dll you have installed? Can you also try with the latest driver?

An invalid app.config or machine.config file can cause the error to be thrown. When M.D.SqlClient loads, it looks to see if there is config information for enclave providers (related to Always Encrypted).

On my PC I also could not reproduced it, but deployed on Windows Server 2016 I get this error.
Working version uses System.Data.SqlClient 4.7.0 and System.Configuration.ConfigurationManager 4.6.0

@glucaci There is a similar issue #287 and we're working on removing the need for app.config. Meanwhile, can you make sure your app.config or machine.config is valid? Once they are valid, you should not see the error anymore.

Hi @glucaci, we have the fix in v1.1.0-preview2. Can you give it a try?

Hi,
Sql Providers has been a sharp learning curve and still not sure of how to get this SqlClient to show up in VS2019's DataSet Designer connection wizard, anyway, that's been worked around with manual fiddling of connection string providerName and regenerating existing DataSets from the XSD.

So, I'm just reporting that I'm blocked by this error in .

I updated to the latest SqlClient 2.0.0-preview from GitHub a yesterday and get the same exception when calling Open() on an MS.Data.SqlClient.SqlConnection. I've also reinstalled the SNI package 1.1.0. in VS with NuGet on the solution and individual project levels.

From the SqlClient source, I see netfx, it's just a reference to MS.Data.SqlClient.SNI from the dotnet-core nuget repo online - netfx is the flavour of SqlClient I require as I'm developing on an asp.net (.Net 4.7.1 target) solution.

My solution has a WebApp and several a library projects. The library project that uses the new SqlClient has the SNI.dll in it's output folder .\bin\Debugx86 or x64 but the WebApp doesn't get the dll where it's needed - WebApp\bin.

Drilling into the inner exception shows that SNI.dll couldn't be loaded from the bin\Debugx86 folder which is the relative path of the library!.
The WebApp project C:Code\MyProduct\MyProductWeb\bin doesn't have the same relative Debugx86 or x64 folders.

Inner Exception stack trace
at Microsoft.Data.SqlClient.SNINativeMethodWrapper..cctor() in E:\agent1\_work\28\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\Interop\SNINativeMethodWrapper.cs:line 64
Message
Failed to load C:\Code\MyProduct\MyProductWeb\bin\x86\SNI.dll

The thrown Exception

   at Microsoft.Data.SqlClient.SNINativeMethodWrapper.UnmanagedIsTokenRestricted(IntPtr token, Boolean& isRestricted)
   at Microsoft.Data.Win32NativeMethods.IsTokenRestrictedWrapper(IntPtr token) in E:\agent1\_work\28\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\Interop\SNINativeMethodWrapper.cs:line 891
   at Microsoft.Data.ProviderBase.DbConnectionPoolIdentity.GetCurrent() in E:\agent1\_work\28\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPoolIdentity.cs:line 115
   at Microsoft.Data.ProviderBase.DbConnectionPoolGroup.GetConnectionPool(DbConnectionFactory connectionFactory) in E:\agent1\_work\28\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPoolGroup.cs:line 199
   at Microsoft.Data.ProviderBase.DbConnectionFactory.GetConnectionPool(DbConnection owningObject, DbConnectionPoolGroup connectionPoolGroup) in E:\agent1\_work\28\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionFactory.cs:line 425
   at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) in E:\agent1\_work\28\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionFactory.cs:line 238
   at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) in E:\agent1\_work\28\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionInternal.cs:line 773
   at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) in E:\agent1\_work\28\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionClosed.cs:line 72
   at Microsoft.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) in E:\agent1\_work\28\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1864
   at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) in E:\agent1\_work\28\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1857
   at Microsoft.Data.SqlClient.SqlConnection.Open() in E:\agent1\_work\28\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1425
   at FameDLO.dloAdminBase.<>c__DisplayClass4_0.<OpenWithRetry>b__0() in C:\Code\MyProduct\MyProductDLO\Common\dloAdminBase.cs:line 52

So, I'm a bit stuck here and this may be a simple fix to project config, so a couple of questions;

  • Should the relative path to the library output folder be referenced when the WebApp runs, i.e. bin\Debugx86? is this a fixed path, shouldn't it be the WebApp website bin folder path?
  • After installing MS.Data.SqlClient in to the asp.Net WebApp project, shouldn't the website have an SNI.dll copied to it? ...I'm running in VS Debug with IISExpress (as a normal user).

I've worked round this by manually copying the libraries binx86 and x64 folders containing only SNI.dll to the WebApp bin folder.

@MrAnderson69UK the error you mentioned right now is a bit different than that of this original issue.
We have an open issue #406 to track what you're facing.

Was this page helpful?
0 / 5 - 0 ratings