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

Cannot connect to a sql database using system.data.sqlclient and native aot win 64 publish #89782

Closed
jdurnil opened this issue Aug 1, 2023 · 19 comments

Comments

@jdurnil
Copy link

jdurnil commented Aug 1, 2023

Hello, using .net 8 preview 6 and trying to use the latest system.data.sqlclient nuget package to connect to a sql database. the project runs fine from visual studio, connects to my database using sql server id and password and returns data. i am building off the new aot api project created from command line. However after publish and running the exe in the publish folder i put a try catch on my db connection to catch the error and get this. Mind you its not my connection string as that works fine running the same app from visual studio.
A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0x14d
at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnNonGCStaticBase(StaticClassConstructionContext*, IntPtr) + 0xd
at System.Data.SqlClient.TdsParser..ctor(Boolean, Boolean) + 0x60
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo, String, SecureString, Boolean, SqlConnectionString, SqlCredential, TimeoutTimer) + 0x144
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer, SqlConnectionString, SqlCredential, String, SecureString, Boolean) + 0x1b2
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity, SqlConnectionString, SqlCredential, Object, String, SecureString, Boolean, SqlConnectionString, SessionData, Boolean, String) + 0x362
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions, DbConnectionPoolKey, Object, DbConnectionPool, DbConnection, DbConnectionOptions) + 0x2e7
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool, DbConnection, DbConnectionOptions, DbConnectionPoolKey, DbConnectionOptions) + 0x33
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection, DbConnectionOptions, DbConnectionInternal) + 0x2a8
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection, DbConnectionOptions, DbConnectionInternal) + 0x5e
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection, UInt32, Boolean, Boolean, DbConnectionOptions, DbConnectionInternal&) + 0x35b
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection, TaskCompletionSource1, DbConnectionOptions, DbConnectionInternal&) + 0x8b at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection, TaskCompletionSource1, DbConnectionOptions, DbConnectionInternal, DbConnectionInternal&) + 0x16c
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection, DbConnectionFactory, TaskCompletionSource1, DbConnectionOptions) + 0x118 at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1) + 0x101
at System.Data.SqlClient.SqlConnection.Open() + 0xf8
at MyFirstAotWebApi.SQLQuery.GetTodos() + 0x9c

@ManickaP ManickaP transferred this issue from dotnet/core Aug 1, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Aug 1, 2023
@ghost
Copy link

ghost commented Aug 1, 2023

Tagging subscribers to this area: @DavoudEshtehari, @David-Engel, @JRahnama
See info in area-owners.md if you want to be subscribed.

Issue Details

Hello, using .net 8 preview 6 and trying to use the latest system.data.sqlclient nuget package to connect to a sql database. the project runs fine from visual studio, connects to my database using sql server id and password and returns data. i am building off the new aot api project created from command line. However after publish and running the exe in the publish folder i put a try catch on my db connection to catch the error and get this. Mind you its not my connection string as that works fine running the same app from visual studio.
A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0x14d
at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnNonGCStaticBase(StaticClassConstructionContext*, IntPtr) + 0xd
at System.Data.SqlClient.TdsParser..ctor(Boolean, Boolean) + 0x60
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo, String, SecureString, Boolean, SqlConnectionString, SqlCredential, TimeoutTimer) + 0x144
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer, SqlConnectionString, SqlCredential, String, SecureString, Boolean) + 0x1b2
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity, SqlConnectionString, SqlCredential, Object, String, SecureString, Boolean, SqlConnectionString, SessionData, Boolean, String) + 0x362
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions, DbConnectionPoolKey, Object, DbConnectionPool, DbConnection, DbConnectionOptions) + 0x2e7
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool, DbConnection, DbConnectionOptions, DbConnectionPoolKey, DbConnectionOptions) + 0x33
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection, DbConnectionOptions, DbConnectionInternal) + 0x2a8
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection, DbConnectionOptions, DbConnectionInternal) + 0x5e
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection, UInt32, Boolean, Boolean, DbConnectionOptions, DbConnectionInternal&) + 0x35b
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection, TaskCompletionSource1, DbConnectionOptions, DbConnectionInternal&) + 0x8b at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection, TaskCompletionSource1, DbConnectionOptions, DbConnectionInternal, DbConnectionInternal&) + 0x16c
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection, DbConnectionFactory, TaskCompletionSource1, DbConnectionOptions) + 0x118 at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1) + 0x101
at System.Data.SqlClient.SqlConnection.Open() + 0xf8
at MyFirstAotWebApi.SQLQuery.GetTodos() + 0x9c

Author: jdurnil
Assignees: -
Labels:

area-System.Data.SqlClient, untriaged

Milestone: -

@vitek-karas
Copy link
Member

When you publish the app for NativeAOT - does it produce warnings?
I don't think SqlClient is AOT compatible yet.

@jdurnil
Copy link
Author

jdurnil commented Aug 1, 2023 via email

@vitek-karas
Copy link
Member

Any chance for a repro? Or at least how you're using SqlClient.

@Wraith2
Copy link
Contributor

Wraith2 commented Aug 2, 2023

It's likely that the native component required for network connectivity has not been restored, look for System.Data.SqlClient.*.sni.dll. Also, as @vitek-karas says System.Data.SqlClient has not been audited and made AOT safe because it no longer ships as part of the framework. The replacement is Microsoft.Data.SqlClient and work to make it AOT safe is on-going.

@jdurnil
Copy link
Author

jdurnil commented Aug 2, 2023 via email

@jdurnil
Copy link
Author

jdurnil commented Aug 2, 2023

Ok, maybe getting somewhere, I switched to Microsoft.data.sqlclient and am now getting the following error which someone may be able to tell me how to fix, the Microsoft.data.sqlclient.sni.dll is showing up in the publish folder. I am now getting this

Only the invariant culture is supported in globalization-invariant mode. See https://aka.ms/GlobalizationInvariantMode for more information. (Parameter 'name')
en-us is an invalid culture identifier.

at System.Globalization.CultureInfo.GetCultureInfo(String) + 0x189
at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1, SqlConnectionOverrides) + 0x73
at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides) + 0x1ed
at MyFirstAotWebApi.SQLQuery.GetTodos() + 0x96

@Wraith2
Copy link
Contributor

Wraith2 commented Aug 2, 2023

@jdurnil
Copy link
Author

jdurnil commented Aug 2, 2023

Actually, thanks for all the help again, this was set to true in my project file
<InvariantGlobalization>false</InvariantGlobalization>
so i set it to false and it worked!!!
I do get a ssl error and have to set Encrypt=False in my connection string which i do not get when running in debug mode. Any ideas how to set this to use the local certificate store in the connection, forgive my ignorance. Otherwise all the sql stuff is working in my executable. I love the new aot web api but it is pretty useless without being able to connect to a database. Anyway if i can get the necrytpion thing working im off and running.

@Wraith2
Copy link
Contributor

Wraith2 commented Aug 2, 2023

Set TustServerCertificate to true in your connection string. Microsoft.Data.SqlClient started being more secure a few versions ago and no longer defaults to allowing no encryption or automatically trusting server certificates.

You can also try it with AOT and see what works. It's been at least partially annotated and using most features should work. If there are things that don't work open issues on the the https://github.com/dotnet/SqlClient repo with a reproduction and it'll get looked into.

@ghost
Copy link

ghost commented Aug 2, 2023

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Issue Details

Hello, using .net 8 preview 6 and trying to use the latest system.data.sqlclient nuget package to connect to a sql database. the project runs fine from visual studio, connects to my database using sql server id and password and returns data. i am building off the new aot api project created from command line. However after publish and running the exe in the publish folder i put a try catch on my db connection to catch the error and get this. Mind you its not my connection string as that works fine running the same app from visual studio.
A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0x14d
at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnNonGCStaticBase(StaticClassConstructionContext*, IntPtr) + 0xd
at System.Data.SqlClient.TdsParser..ctor(Boolean, Boolean) + 0x60
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo, String, SecureString, Boolean, SqlConnectionString, SqlCredential, TimeoutTimer) + 0x144
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer, SqlConnectionString, SqlCredential, String, SecureString, Boolean) + 0x1b2
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity, SqlConnectionString, SqlCredential, Object, String, SecureString, Boolean, SqlConnectionString, SessionData, Boolean, String) + 0x362
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions, DbConnectionPoolKey, Object, DbConnectionPool, DbConnection, DbConnectionOptions) + 0x2e7
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool, DbConnection, DbConnectionOptions, DbConnectionPoolKey, DbConnectionOptions) + 0x33
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection, DbConnectionOptions, DbConnectionInternal) + 0x2a8
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection, DbConnectionOptions, DbConnectionInternal) + 0x5e
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection, UInt32, Boolean, Boolean, DbConnectionOptions, DbConnectionInternal&) + 0x35b
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection, TaskCompletionSource1, DbConnectionOptions, DbConnectionInternal&) + 0x8b at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection, TaskCompletionSource1, DbConnectionOptions, DbConnectionInternal, DbConnectionInternal&) + 0x16c
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection, DbConnectionFactory, TaskCompletionSource1, DbConnectionOptions) + 0x118 at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1) + 0x101
at System.Data.SqlClient.SqlConnection.Open() + 0xf8
at MyFirstAotWebApi.SQLQuery.GetTodos() + 0x9c

Author: jdurnil
Assignees: -
Labels:

untriaged, area-NativeAOT-coreclr

Milestone: -

@ericstj
Copy link
Member

ericstj commented Aug 2, 2023

Seems unusual that AOT can't handle this. Here's a repro (different stack, but I suspect that's just due to my connection string).
sqlClient.zip

That'll fail with

Unhandled Exception: System.DllNotFoundException: Unable to load DLL 'sni.dll' or one of its dependencies: The specified module could not be found.
   at System.Runtime.InteropServices.NativeLibrary.LoadLibErrorTracker.Throw(String) + 0x6f
   at Internal.Runtime.CompilerHelpers.InteropHelpers.FixupModuleCell(InteropHelpers.ModuleFixupCell*) + 0xfd
   at Internal.Runtime.CompilerHelpers.InteropHelpers.ResolvePInvokeSlow(InteropHelpers.MethodFixupCell*) + 0x2f
   at System.Data.SqlClient.SNINativeMethodWrapper.UnmanagedIsTokenRestricted(IntPtr, Boolean&) + 0x31
   at System.Data.Win32NativeMethods.IsTokenRestrictedWrapper(IntPtr) + 0x19
   at System.Data.ProviderBase.DbConnectionPoolIdentity.GetCurrentNative() + 0xb1
   at System.Data.ProviderBase.DbConnectionPoolGroup.GetConnectionPool(DbConnectionFactory) + 0x51
   at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPool(DbConnection, DbConnectionPoolGroup) + 0x5f
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection, TaskCompletionSource`1, DbConnectionOptions, DbConnectionInternal, DbConnectionInternal&) + 0xad
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection, DbConnectionFactory, TaskCompletionSource`1, DbConnectionOptions) + 0x118
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1) + 0x101
   at System.Data.SqlClient.SqlConnection.Open() + 0xf8
   at Program.<Main>$(String[] args) + 0x45
   at sqlClient!<BaseAddress>+0x411937

Seems odd that we can't load sni.dll - it's present and next to the app.

@Wraith2
Copy link
Contributor

Wraith2 commented Aug 2, 2023

It is odd. I know that Microsoft.Data.SqlClient got some work in this area though so it's better to move to it.

/cc @David-Engel in case you want to investigate

@David-Engel
Copy link
Contributor

The S.D.SqlClient NuGet package is legacy. It was a bridge for people coming from .NET Framework to .NET Core. I wouldn't be surprised if there are issues with it that mean it will never be suitable against .NET 8. If you are on a modern version of .NET, move to M.D.SqlClient.

@vitek-karas
Copy link
Member

I create #89874 to look into the native library loading issue in NativeAOT.

@jdurnil
Copy link
Author

jdurnil commented Aug 2, 2023 via email

@MichalStrehovsky
Copy link
Member

Seems unusual that AOT can't handle this. Here's a repro (different stack, but I suspect that's just due to my connection string).

This can be likely worked around by adding <ItemGroup><DirectPInvoke Include="sni" /></ItemGroup> to the project file for now.

@jdurnil
Copy link
Author

jdurnil commented Aug 2, 2023 via email

@jkotas
Copy link
Member

jkotas commented Aug 3, 2023

I’m thought my messages made it over. It is all working now

Thank you for letting us know.

@jkotas jkotas closed this as completed Aug 3, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Aug 3, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Sep 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants