Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can EFCore.BulkExtensions influence DbContext connection sequence? #700

Closed
uhfath opened this issue Dec 25, 2021 · 10 comments
Closed

How can EFCore.BulkExtensions influence DbContext connection sequence? #700

uhfath opened this issue Dec 25, 2021 · 10 comments
Labels

Comments

@uhfath
Copy link

uhfath commented Dec 25, 2021

A fresh new console project using VS 2022 and .NET 6:
https://github.com/uhfath/TestBulkExtensionsError

Only EFCore.BulkExtensions and Microsoft.EntityFrameworkCore.SqlServer packages added with a simple DbContext and one model.
When connecting to a remote SQL Server 2019 instance without EFCore.BulkExtensions package installed everything works as expected.
However, when simply installing the EFCore.BulkExtensions package I get the following exception (partially in Russian, so let me know if a translation needed):

A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - Цепочка сертификатов выпущена центром сертификации, не имеющим доверия.)
   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.TdsParserStateObject.ThrowExceptionAndWarning(Boolean callerHasConnectionLock, Boolean asyncClose)
   at Microsoft.Data.SqlClient.TdsParserStateObject.SNIWritePacket(PacketHandle packet, UInt32& sniError, Boolean canAccumulate, Boolean callerHasConnectionLock)
   at Microsoft.Data.SqlClient.TdsParserStateObject.WriteSni(Boolean canAccumulate)
   at Microsoft.Data.SqlClient.TdsParserStateObject.WritePacket(Byte flushMode, Boolean canAccumulate)
   at Microsoft.Data.SqlClient.TdsParser.TdsLogin(SqlLogin rec, FeatureExtension requestedFeatures, SessionData recoverySessionData, FederatedAuthenticationFeatureExtensionData fedAuthFeatureExtensionData)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.Login(ServerInfo server, TimeoutTimer timeout, String newPassword, SecureString newSecurePassword)
   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.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 Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.OpenDbConnection(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternal(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.InitializeReader(Enumerator enumerator)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.<>c.<MoveNext>b__19_0(DbContext _, Enumerator enumerator)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Boolean& found)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
   at Program.<Main>$(String[] args) in D:\Projects\TestBulkExtensionsError\TestBulkExtensionsError\Program.cs:line 4

I'm not sure how EFCore.BulkExtensions package is influencing but the exception happens only when it's installed in the project.
Also, when testing the same test app with a local instance with and without EFCore.BulkExtensions everything works fine.

A connection string used for a remote server looks like this:

Server=host_name;Database=database_name;User Id=user_name;Password=password;

Any ideas how these could be related?

P.S. SQL Server 2019 Standard was installed with all the default settings on a Windows Server 2019 Standard.
I've never had this kind of an issue with my other projects using EFCore.BulkExtensions with this server. But they are .Net Core 2x and 3x versions (with old version of EFCore.BulkExtensions).

@borisdj
Copy link
Owner

borisdj commented Dec 25, 2021

Not sure about this issue.
One thing that comes to mind, because exception mentions certificate, is that nuget package after v5 was Signed for strong name, Signing the nuget package #161

@uhfath
Copy link
Author

uhfath commented Dec 26, 2021

I guess that if the issue was from package signing than the project itself wouldn't run or even build correctly,
whereas in this case the problem is only during connection.
I myself seeing this for the first time in my life.

@borisdj
Copy link
Owner

borisdj commented Dec 26, 2021

Also first time seeing this.
Maybe try to load source of lib. instead of nuget and see what happens, and to debug it if possible.

@uhfath
Copy link
Author

uhfath commented Dec 26, 2021

Tried it just now.
When cloning the repo and checking out this tag everything works fine.
"Curiouser and curiouser" ©

@borisdj
Copy link
Owner

borisdj commented Dec 27, 2021

Have you also tried with latest source v6 ?

@twurm
Copy link

twurm commented Dec 27, 2021

We had the same issues with 6.2.3, though we were upgrading from 6.0.9 so not sure where the bug appeared. Somehow the connection string or underlying code is telling EF core to use SSL encyrption. Adding TrustServerCertificate will work around the issue.

-- this is the connection string that causes the issue for us: 
"ConnectionString": "Server=localhost;Database=MxHuskyBaan;Trusted_Connection=True"

-- this will work around the issue
"ConnectionString": "Server=localhost;Database=MxHuskyBaan;Trusted_Connection=True;TrustServerCertificate=true;"

On our SQL 2019 instance we don't have any SSL certificates installed. Not sure where in the code its being enabled. We are using .Net 6 in our application.

@twurm
Copy link

twurm commented Dec 27, 2021

All the issue seems to be related to the changes in 6.1.5 where Microsoft.Data.SqlClient was bumped to v4.0.0. The issue itself is caused by the following change Microsoft made.

Encrypt default value set to true

@borisdj borisdj closed this as completed Dec 28, 2021
@twurm
Copy link

twurm commented Dec 30, 2021

@borisdj it might be worth adding to the readme, Microsoft.EntityFrameworkCore.SqlServer v6.0.x is using version 2.1.4. So when using this package you are bumped from 2.1.4 to 4.0 with multiple breaking changes.

@borisdj
Copy link
Owner

borisdj commented Jan 9, 2022

SqlClient dependency down to 2.1
Published v6.2.8

@borisdj
Copy link
Owner

borisdj commented May 10, 2022

With v6.5.0+ dependency Microsoft.Data.SqlClient is upgraded to 4.1.0. since dotnet/
efcore
is now also on higher version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants