Repository navigation
Closed
Description
Describe the bug
Using Microsoft.Data.SqlClient 4.0.0 with SQL LocalDB and named pipes does not work when attempting to connect to the instance.
I maintain a library that provides interop with the SQL LocalDB APIs, and upgrading its tests to version 4.0.0 is causing a number of its tests to fail.
An example of one of these failing tests is here.
Exception message: The instance of SQL Server you attempted to connect to does not support encryption.
Stack trace:
[xUnit.net 00:01:35.98] Can_Manage_SqlLocalDB_Instances [FAIL]
Failed Can_Manage_SqlLocalDB_Instances [8 s]
Error Message:
Microsoft.Data.SqlClient.SqlException : The instance of SQL Server you attempted to connect to does not support encryption.
Stack Trace:
at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at Microsoft.Data.SqlClient.TdsParser.ConsumePreLoginHandshake(Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean& marsCapable, Boolean& fedAuthRequired)
at Microsoft.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover, SqlAuthenticationMethod authType)
at Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
at Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
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.WaitForPendingOpen()
--- End of stack trace from previous location ---
at MartinCostello.SqlLocalDb.SqlLocalDbApiTests.Can_Manage_SqlLocalDB_Instances() in D:\a\sqllocaldb\sqllocaldb\tests\SqlLocalDb.Tests\SqlLocalDbApiTests.cs:line 287
at MartinCostello.SqlLocalDb.SqlLocalDbApiTests.Can_Manage_SqlLocalDB_Instances() in D:\a\sqllocaldb\sqllocaldb\tests\SqlLocalDb.Tests\SqlLocalDbApiTests.cs:line 288
--- End of stack trace from previous location ---
To reproduce
Clone the martincostello/sqllocaldb repo on a Windows machine and checkout the dependabot/nuget/Microsoft.Data.SqlClient-4.0.0 branch and run build.ps1 to run the library's build and test script.
Expected behavior
The tests pass.
Further technical details
Microsoft.Data.SqlClient version: 4.0.0
.NET target: net6.0
SQL Server version: SQL LocalDB 13
Operating system: Windows 2019
Reacted by Ruben A
Metadata
Metadata
Assignees
Labels
No labels
Activity
martincostello commentedon Nov 19, 2021
Likely related to #1312.
Update CI to use sql2019, not 2017
JRahnama commentedon Nov 20, 2021
@martincostello I am looking at the issue and will update yo soon.
JRahnama commentedon Nov 22, 2021
@martincostello as I see you have build the application for net 6. Are you using VS2022?
On Friday when I tested your repro I got the same error message as you mentioned, but today it worked fine. The only thing I did extra was stopping localdb and start it again.

This is my localdb version:
I also made a fresh clone this morning in case you have changed anything.
martincostello commentedon Nov 22, 2021
I use VS 2022 locally now, but I think the GitHub Actions images still use VS 2019 in CI for
windows-latest.I haven't actually run the PR locally myself, I can check it tomorrow on my development machine to see if it fails there too.
martincostello commentedon Nov 23, 2021
This is failing for me locally. My version appears to be 15.0 from the library path, but 13.1 from the sqllocaldb tool.
JRahnama commentedon Nov 25, 2021
@martincostello any progress on this issue? I am not able to repro it anymore !!!

martincostello commentedon Nov 25, 2021
Just retried the linked job to update from GitHub Actions, and it's still failing: https://github.com/martincostello/sqllocaldb/runs/4328686462?check_suite_focus=true
JRahnama commentedon Nov 25, 2021
out of curiosity, may this warning have anything to do with the issue?
martincostello commentedon Nov 25, 2021
I don't think so. That's just a warning from MSBuild about file locks when copying artifacts around during the compilation for different TFMs in parallel.
Rubenisme commentedon Nov 27, 2021
I'm having the same issue, I use Martin's library and had System.Data.SqlClient installed in a project reference, tests worked fine, but then switched to Microsoft.Data.SqlClient 4.0 and then got 2 different errors:
And in another test:
Switching back to either System.Data.SqlClient (4.8.3) or Microsoft.Data.SqlClient 3.0.1 (previous stable before 4.0) fixes it.
JRahnama commentedon Nov 29, 2021
@Rubenisme thank you for updating this thread. One last question before Have you or @martincostello tried with net 5 to get connection from localdb?
@Rubenisme can you provide your connection string and the sample repro?
martincostello commentedon Nov 29, 2021
With version 4.0.0, no. The library's tests were targeting .NET 5 until recently, but moved to .NET 6 not long after GA.
JRahnama commentedon Nov 29, 2021
one more test, if you can add
Encrypt=falseto your connection string and see the result.Rubenisme commentedon Nov 29, 2021
@JRahnama It took me a little to reduce to a somewhat not-way-too-big way but have it reproduced: here.
Just run the unit test (nunit), then see it works, then update the Microsoft.Data.SqlClient nuget package to 4.0.0 and see it fail.
JRahnama commentedon Nov 30, 2021
I am going to make PR to address the cause of this issue. The issue comes from the fact that driver checks if the connection string starts with
(localdb)then it setisLocalDBto true otherwise false. However the samples provided by repros are usingInstance pipe namewhich is in a format ofnp:\\.\pipe\LOCALDB#<some number>\tsql\query(MS doc) That check needs to be added to theLocalDBAPIfor native andSNIProxyfor managed code.JRahnama commentedon Nov 30, 2021
as a work around for now, till the fix is out, if there is a way to add Encrypt=false to your connection string that would solve the issue. In the provided sample repro by @Rubenisme it happens at
await EnsureDatabaseCreated();inside test classIntegrationTestBaseLocalDbRubenisme commentedon Dec 1, 2021
@JRahnama thank you for finding the issue so quickly, and creating a PR to fix it. Sorry I didn’t have a more minimal repo but thank you and for pinpointing the place it happens exactly.
@martincostello will you update the library in the meantime or let users know about the workaround and wait for the fix on the SqlClient library?
martincostello commentedon Dec 1, 2021
I won't be updating the library for this, especially with a fix coming, as users can work around it themselves.
I'll point any users that file issues with the SqlLocalDb library to this issue if I get any reports.
KoalaBear84 commentedon Dec 3, 2021
Got the same error message as mentioned above after updating from 3.x to 4.0.0. SQL Server is on my local machine, not sure if that if the same issue. I've added
Encrypt=noto my connection string and it works again. Might not be the best solution, but one that works.Rubenisme commentedon Dec 3, 2021
@KoalaBear84 Above @JRahnama says that you should use
Encrypt=false, probably yournoalso works as you said.On another note:
@JRahnama if you have this implemented, can you also expose the
IsLocalDbproperty/method somewhere or have the provider name be different from normal SqlServer?For an InMemory database you can do:
context.Database.IsInMemory(). I would like to do the same for a localDb, so eithercontext.Database.IsLocalDb()method orcontext.Database.IsLocalDbget-only property would be great.Update: Nevermind, I realize this is an EF core thingy, and although you work for the same company and probably work somewhat close together with that team, they are still 2 different worlds. If you can expose it to them that would probably help down the road. ;)
ErikEJ commentedon Dec 3, 2021
@Rubenisme All localdb server names have a unique pattern, that you can detect.
Rubenisme commentedon Dec 3, 2021
@ErikEJ Well yes, I now use:
context.Database.GetConnectionString().ToLowerInvariant().Contains("localdb")Not super nice, but it works.JRahnama commentedon Dec 3, 2021
@Rubenisme you can access that through
SELECT SERVERPROPERTY ('IsLocalDb'). Read here.JRahnama commentedon Dec 15, 2021
@martincostello can you test with this artifact . If you do not have access use this file and change the
zipextension tonupkgMicrosoft.Data.SqlClient.4.0.0-pull-c0ca2ea.21348.2.zip
Test Microsoft.Data.SqlClient fix
martincostello commentedon Jan 1, 2022
@JRahnama Validated the fix with martincostello/sqllocaldb#466.