SqlClient: Cannot open a connection in .NET 6 FSI
Describe the bug
I cannot open a connection to a local SQL database instance using v4.0 of the Microsoft.Data.SqlClient package. Doing so gives the following error:
error FS0193: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SqlAuthenticationProviderManager' threw an exception.
---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'System.Configuration.ConfigurationManager, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
at Microsoft.Data.SqlClient.SqlAuthenticationProviderManager..cctor()
--- End of inner exception stack trace ---
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) in D:\a\_work\1\s\src\Microsoft.Data.SqlClient\netcore\src\Microsoft\Data\SqlClient\SqlInternalConnectionTds.cs:line 480
at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) in D:\a\_work\1\s\src\Microsoft.Data.SqlClient\netcore\src\Microsoft\Data\SqlClient\SqlConnectionFactory.cs:line 136
at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) in D:\a\_work\1\s\src\Microsoft.Data.SqlClient\netcore\src\Common\src\Microsoft\Data\ProviderBase\DbConnectionFactory.cs:line 123
at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) in D:\a\_work\1\s\src\Microsoft.Data.SqlClient\netcore\src\Microsoft\Data\ProviderBase\DbConnectionPool.cs:line 731
at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) in D:\a\_work\1\s\src\Microsoft.Data.SqlClient\netcore\src\Microsoft\Data\ProviderBase\DbConnectionPool.cs:line 1759
at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) in D:\a\_work\1\s\src\Microsoft.Data.SqlClient\netcore\src\Microsoft\Data\ProviderBase\DbConnectionPool.cs:line 1162
at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) in D:\a\_work\1\s\src\Microsoft.Data.SqlClient\netcore\src\Microsoft\Data\ProviderBase\DbConnectionPool.cs:line 1130
at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) in D:\a\_work\1\s\src\Microsoft.Data.SqlClient\netcore\src\Microsoft\Data\ProviderBase\DbConnectionFactory.cs:line 122
at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) in D:\a\_work\1\s\src\Microsoft.Data.SqlClient\netcore\src\Common\src\Microsoft\Data\ProviderBase\DbConnectionInternal.cs:line 341
at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) in D:\a\_work\1\s\src\Microsoft.Data.SqlClient\netcore\src\Common\src\Microsoft\Data\ProviderBase\DbConnectionClosed.cs:line 39
at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides) in D:\a\_work\1\s\src\Microsoft.Data.SqlClient\netcore\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1838
at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides) in D:\a\_work\1\s\src\Microsoft.Data.SqlClient\netcore\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1327
at Microsoft.Data.SqlClient.SqlConnection.Open() in D:\a\_work\1\s\src\Microsoft.Data.SqlClient\netcore\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1304
at <StartupCode$FSI_0006>.$FSI_0006.main@()
Stopped due to error
To reproduce
#r "nuget:Microsoft.Data.SqlClient, 4.0"
open Microsoft.Data.SqlClient
let conn = new SqlConnection("my connection string goes here")
conn.Open()
Expected behavior
The connection is opened.
Further technical details
Microsoft.Data.SqlClient version: 4.0 .NET target: .NET 6 SQL Server version: SQL Server 2019 Operating system: Windows 11
Additional context This works fine with the 3.0.1 NuGet package
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 27 (12 by maintainers)
For those in need of a quick workaround, this actually worked for me:
#r "nuget:System.Configuration.ConfigurationManager, 4.7.0"
Everything else is .NET 6 and latest nuget versions and I can see no ill effects.@Kaur-Parminder I don’t understand - other people in PowerShell and C# applications are having an issue as well. This doesn’t appear to be an F#-specific thing.
I have tested the issue with new released .net sdk 6.0.101, the same thing might work with previous version of .net 6.
I compared the path C:\Program Files\dotnet\sdk\6.0.101\FSharp with the already working sdk C:\Program Files\dotnet\sdk\3.1.416\FSharp
I see a lot of new files in .net 6 and Also the dll which is causing the issue is there (missing in 3.1).
The version of this dll is old.
I deleted the file ‘System.Configuration.ConfigurationManager’ from C:\Program Files\dotnet\sdk\6.0.101\FSharp and the connection is made succesfully.
I am also attaching dotnet --info, in case anybody needs details of sdk/runtime versions
@larjo Thanks, that workaround managed to solve my FSI issues as well.
@larjo Thanks!! The magical “4.7.0” version did the trick. Quite a rabbit hole.
Yes please reopen and transfer to dotnet/fsharp, it is likely a bug with transitive resolution of somehow we are fixing to an earlier System.Configuration.ConfigurationManager in the FSI implementation process.
We use nuget to resolve all of our dependencies. For net5.0 it resolves to
System.Buffers
5.0.0.0 and for net6.0 it resolves toSystem.Buffers
6.0.0.0 which is what we exepect.We have solved the missing
System.Configuration.ConfigurationManager
by cleaning up some nuget packages that are not needed when targeting net6.0For the
System.Buffers
we have found out that the issue is related to<PublishSingleFile>true</PublishSingleFile>
. If we do not publish as single file it works perfectly, but as soon as we turn it on again we get the missingSystem.Buffers
error. This also explains why it works in net5.0 and not net6.0 as thePublishSingleFile
feature changed quite a bit for net6.0.@JRahnama That one is using manual DLL references and appears to be on .NET Core 3 and an older version of the SQL package. It was also closed nine months ago.
Read my issue again - it only doesn’t work with v4 which was only just released a couple of days ago, and as I stated above, v3 works just fine, and I’ve also provided a simple repro for you to prove the issue.
Thanks