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

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

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 19 (13 by maintainers)

Most upvoted comments

I didn’t need to do that. I just switched to Microsoft.Data.SqlClient

On Wed, Aug 2, 2023 at 4:51 PM Michal Strehovský @.***> wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/dotnet/runtime/issues/89782#issuecomment-1662948874, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB7DCF6UP3GB3ZIWCGNXGIDXTK4T7ANCNFSM6AAAAAA273CNL4 . You are receiving this because you authored the thread.Message ID: @.***>

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.