Skip to content

Cannot open a connection in .NET 6 FSI #1397

Closed

Description

@isaacabraham

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

Activity

  1. Wraith2 commented on Nov 20, 2021

    @Wraith2
    Contributor

    Looks like the same problem that happens with powershell. You're not restoring the dependencies so it isn't working.

  2. isaacabraham commented on Nov 20, 2021

    @isaacabraham
    Author

    @Wraith2 The dependency should be getting pulled down by NuGet, unless it's not part of Microsoft.Data.SqlClient - which would seem to be wrong since it's evidently required to use the library.

  3. JRahnama commented on Nov 20, 2021

    @JRahnama
    Contributor

    @isaacabraham have you looked at issue #645? Although the error message is different, but I think conversation flow could help to solve the issue.

  4. isaacabraham commented on Nov 20, 2021

    @isaacabraham
    Author

    @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

  5. Tornhoof commented on Nov 21, 2021

    @Tornhoof
  6. isaacabraham commented on Nov 23, 2021

    @isaacabraham
    Author

    @Tornhoof See #1397 (comment) - having to add this implies that the package dependency chain is broken. This could then affect all apps (console, class lib, web etc.) across .NET, not only for F#.

  7. isaacabraham commented on Nov 23, 2021

    @isaacabraham
    Author

    Either that or there's an issue with the way that FSI is using nuget refs, although I haven't seen this in any other package at all.

  8. JRahnama commented on Nov 23, 2021

    @JRahnama
    Contributor

    @Tornhoof See #1397 (comment) - having to add this implies that the package dependency chain is broken. This could then affect all apps (console, class lib, web etc.) across .NET, not only for F#.

    @isaacabraham just to keep you and other interested people in the loop, I am testing to see if dropping support for netcoreapp2.1 has anything to do with this. Apparently the package Microsoft.Client.Identity is built for netcoreapp2.1 and in our previous version it was following the same path (From netcoreapp2.1 folder inside the nuget), but now it falls back on netstandard 2.0 and that might be the root cause of the issue( this is my idea not proven yet!) This is being tested now to see if I can gather more facts. If that is true, as you have mentioned, some other scenarios than F# should have the same problem, but so far no other issue has been received regarding this matter.

  9. eziante commented on Dec 7, 2021

    @eziante

    We are having the same issue with a C# application targeting .NET6.0

    Originally we were using System.Data.SqlClient, but after we upgraded our C# application from targeting .NET5.0 to target .NET6.0, we ran into an issue with System.Buffers 4.0.2.0 not being found so we changed our code to use Microsoft.Data.SqlClient instead.

    Now when running the code we get the same error stating that System.Configuration.ConfigurationManager 5.0.0.0 could not be found.

    System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SqlAuthenticationProviderManager' threw an exception.
     ---> System.IO.FileNotFoundException: 
    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.ProviderBase.DbConnectionPool.CheckPoolBlockingPeriod(Exception e)
       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 iFact.Data.Definitions.Helpers.SqlHelper`1..ctor(ILogger`1 logger, SqlConnectionStringBuilder connectionStringBuilder)
       at iFact.Data.Definitions.SqlServer.MSSql.MsSqlSourceSystem`1.GetAndUploadData(ExtractionDetails extractionDetails, Nullable`1 cancellationToken)
       at iFact.Customer.Client.Features.JobExecution.ExecutionWorker.ExecuteExtraction(ExecutionDetails executionDetails, ExtractionDetails extractionDetails, CancellationTokenSource cancellationTokenSource)
    

    Manually installing the System.Configuration.ConfigurationManager in a version 5.0.0 from nuget solved this issue, however, we are now back at our original problem with System.Buffers version 4.0.2.0 not being found that we also experienced with System.Data.SqlClient.

  10. Wraith2 commented on Dec 7, 2021

    @Wraith2
    Contributor

    So what happens when you use nuget to restore the System.Buffers dependency, as you should do with all the library dependencies that are listed?

  11. eziante commented on Dec 8, 2021

    @eziante

    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 to System.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.0

    For 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 missing System.Buffers error. This also explains why it works in net5.0 and not net6.0 as the PublishSingleFile feature changed quite a bit for net6.0.

  12. vforteli commented on Dec 10, 2021

    @vforteli

    Ran into a similar issue with .netcore 3.1 and sql client 4. The transitive dependencies seem to work on windows at least, but when running in the appservices on ubuntu it would fail when trying to find System.Configuration.ConfigurationManager >= 5.
    I guess adding an explicit dependency will fix it, but seems like an ugly workaround. So seems something is haywire with the dependencies of the package

  13. Kaur-Parminder commented on Dec 17, 2021

    @Kaur-Parminder
    Contributor

    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).
    dotnet6vsdotnet3 1

    The version of this dll is old.
    system configuration configurationmanager

    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

    versions I am using

  14. JRahnama commented on Dec 17, 2021

    @JRahnama
    Contributor

    I just checked net5 and netcoreapp 3.1 does not have that dependency.

  15. JRahnama commented on Dec 17, 2021

    @JRahnama
    Contributor

    @dsyme, maybe you can direct us to the right owner of this issue please?

  16. dsyme commented on Dec 17, 2021

    @dsyme

    I suppose add a bug in dotnet/fsharp, and cc @KevinRansom and @vzarytovskii

  17. added
    External 🔗
    on Dec 17, 2021
  18. Kaur-Parminder commented on Dec 20, 2021

    @Kaur-Parminder
    Contributor

    @isaacabraham Please log the issue with Repository https://github.com/dotnet/fsharp for any further work.

    I am closing this as an external issue.

  19. isaacabraham commented on Feb 4, 2022

    @isaacabraham
    Author

    @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.

  20. Kaur-Parminder commented on Feb 4, 2022

    @Kaur-Parminder
    Contributor

    @isaacabraham I tested with netcore 3.1 it works fine for me with the repro you have provided above. It was just .net6 that was causing the issue. Can you be specific with the C# application?

  21. isaacabraham commented on Feb 4, 2022

    @isaacabraham
    Author

    @Kaur-Parminder Have you read the comments above in this thread? For some reason I'm getting the feeling that your main priority here is simply to close this issue down as quickly as possible rather than actually get to the bottom of it.

  22. Wraith2 commented on Feb 4, 2022

    @Wraith2
    Contributor

    If the priority were to close issues there would be a lot less old open issues.

    I think the problem may be that it requires some in depth ecosystem knowledge to know that FSI is the f-sharp interpreter and understand that it's a REPL not a classic compiled environment. Even knowing that if someone isn't familiar with f-sharp they may not know how to investigate the problem. I don't, that's part of why I haven't.

    It's also worth noting that (as far as I know) there is no explicit support for repl situations like fsi and powershell (as far as i know) and that the nuget behaviour of restoring the transitive closure of dependencies relies on the compilation target tfm to identify the list of things needed.

  23. KevinRansom commented on Feb 4, 2022

    @KevinRansom

    @isaacabraham ... it's probably simpler to just log an issue in our repo, we can always investigate and drive any fixes into sqlclient if they are necessary.

    Kevin

  24. dsyme commented on Feb 4, 2022

    @dsyme

    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.

  25. KevinRansom commented on Feb 4, 2022

    @KevinRansom

    I think we have an assembly probing issue because we actually deploy a copy of that dll, albeit a later version in the compiler directory on the sdk.

  26. larjo commented on Mar 22, 2022

    @larjo

    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.

  27. halcwb commented on May 28, 2022

    @halcwb

    @larjo Thanks!! The magical "4.7.0" version did the trick. Quite a rabbit hole.

  28. schonfinkel commented on Jun 3, 2022

    @schonfinkel

    @larjo Thanks, that workaround managed to solve my FSI issues as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

External 🔗

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions