dotnet / SqlClient

Microsoft.Data.SqlClient provides database connectivity to SQL Server for .NET applications.
MIT License
846 stars 282 forks source link

native aot deployment error using Microsoft Entra authentication with Azure SQL #2742

Closed guillermoright closed 2 months ago

guillermoright commented 2 months ago

Native Aot

using native AOT with ASP.NET Core, minimal api, with azure sql server with Microsoft Entra authentication with Azure SQL,

this is the connection string for Microsoft Entra MFA Serve authentication: {servername};Authentication=Active Directory Interactive;Database={databaseName}

when we deploy the aplication dotnet publish -r win-x64 -c Release, we are getting this error , seems the microsoft entra authentication is not supported,

Exception message:
Stack trace:

 Connection id "0HN5HAGUR6598", Request id "0HN5HAGUR6598:00000001": An unhandled exception was thrown by the application.
      Microsoft.Data.SqlClient.SqlException (0x80131904): A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
       ---> System.TypeInitializationException: A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
       ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize
       ---> System.MissingMethodException: No parameterless constructor defined for type 'System.Configuration.ClientConfigurationHost'.
         at System.ActivatorImplementation.CreateInstance(Type, Boolean) + 0x119
         at System.Configuration.Internal.ConfigSystem.System.Configuration.Internal.IConfigSystem.Init(Type, Object[]) + 0x43
         at System.Configuration.ClientConfigurationSystem..ctor() + 0x47
         at System.Configuration.ConfigurationManager.EnsureConfigurationSystem() + 0x56
         --- End of inner exception stack trace ---
         at System.Configuration.ConfigurationManager.PrepareConfigSystem() + 0x35
         at System.Configuration.ConfigurationManager.GetSection(String) + 0x18
         at System.Runtime.Caching.MemoryCacheStatistics.InitializeConfiguration(NameValueCollection) + 0x2c
         at System.Runtime.Caching.MemoryCacheStatistics..ctor(MemoryCache, NameValueCollection) + 0x4a
         at System.Runtime.Caching.MemoryCache.InitDisposableMembers(NameValueCollection) + 0xf1
         at Microsoft.Data.SqlClient.ActiveDirectoryAuthenticationProvider..cctor() + 0x8c
         at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xb9
         --- End of inner exception stack trace ---
         at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0x14a
         at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnGCStaticBase(StaticClassConstructionContext*, Object) + 0xd
         at Microsoft.Data.SqlClient.ActiveDirectoryAuthenticationProvider.<AcquireTokenAsync>d__19.MoveNext() + 0x4c4
      --- End of stack trace from previous location ---
         at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xb2
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4b
         at Microsoft.Data.SqlClient.SqlInternalConnectionTds.<>c__DisplayClass148_1.<<GetFedAuthToken>b__1>d.MoveNext() + 0xc8
      --- End of stack trace from previous location ---
         at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xb2
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4b
         at Microsoft.Data.SqlClient.SqlInternalConnectionTds.GetFedAuthToken(SqlFedAuthInfo) + 0x3b8
         at Microsoft.Data.SqlClient.SqlInternalConnectionTds.GetFedAuthToken(SqlFedAuthInfo) + 0xb0a
         at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OnFedAuthInfo(SqlFedAuthInfo) + 0x30f
         at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior, SqlCommand, SqlDataReader, BulkCopySimpleResultSet, TdsParserStateObject, Boolean&) + 0xa31
         at Microsoft.Data.SqlClient.TdsParser.Run(RunBehavior, SqlCommand, SqlDataReader, BulkCopySimpleResultSet, TdsParserStateObject) + 0x43
         at Microsoft.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean) + 0x34
         at Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo, String, SecureString, TimeoutTimer, Boolean) + 0x18a
         at Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo, String, SecureString, Boolean, SqlConnectionString, SqlCredential, TimeoutTimer) + 0x5b8
         at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer, SqlConnectionString, SqlCredential, String, SecureString, Boolean) + 0x1c6
         at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity, SqlConnectionString, SqlCredential, Object, String, SecureString, Boolean, SqlConnectionString, SessionData, Boolean, String, DbConnectionPool, Func`3) + 0x4c6
         at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions, DbConnectionPoolKey, Object, DbConnectionPool, DbConnection, DbConnectionOptions) + 0x30e
         at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool, DbConnection, DbConnectionOptions, DbConnectionPoolKey, DbConnectionOptions) + 0x40
         at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection, DbConnectionOptions, DbConnectionInternal) + 0x270
         at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection, DbConnectionOptions, DbConnectionInternal) + 0x5e
         at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection, UInt32, Boolean, Boolean, DbConnectionOptions, DbConnectionInternal&) + 0x4be
         at Microsoft.Data.ProviderBase.DbConnectionPool.WaitForPendingOpen() + 0x156
      --- End of stack trace from previous location ---
         at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xb2
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4b
         at Microsoft.Data.SqlClient.SqlRetryLogicProvider.<ExecuteAsync>d__7.MoveNext() + 0x138
      --- End of stack trace from previous location ---
         at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
         at Microsoft.Data.SqlClient.SqlRetryLogicProvider.<ExecuteAsync>d__7.MoveNext() + 0x3cd
      --- End of stack trace from previous location ---
         at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xb2
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4b
arellegue commented 2 months ago

Thank you for reporting this issue.

We tried to reproduce the issue by creating test application using the template ASP.NET Core WebAPI (native AOT). We then tried to add connection string with Authentication=Active Directory Interactive. The test run succeeded. We then tried to publish using dotnet publish -r win-x64 -c Release. However, we were not able to reproduce the issue you reported.

Could you provide an example reproduction code, please? This will help us to investigate this issue further.

guillermoright commented 2 months ago

Thank you for reporting this issue.

We tried to reproduce the issue by creating test application using the template ASP.NET Core WebAPI (native AOT). We then tried to add connection string with Authentication=Active Directory Interactive. The test run succeeded. We then tried to publish using dotnet publish -r win-x64 -c Release. However, we were not able to reproduce the issue you reported.

Could you provide an example reproduction code, please? This will help us to investigate this issue further.

native.zip

this an example you can use to reproduce the errorr , i just added small code to open a database connection ,
I use Azure Hyperscale at work. You should replace the databaseserver and databasename parameters with the ones you'll use. you need to publish the api dotnet publish -r win-x64 and then call the get endpoint

The web API works in development mode, but once deployed, it throws an error.

in development mode when trying to connect to the server, a browser session opens to enter your credentials. However, after deployment, this process no longer works an throw an error

Can you send me your example so I can review it?

arellegue commented 2 months ago

Thank you for the repro. I tried the provided repro and identified that the issue happens when using MDS version 5.2.1.

A hotfix is scheduled to be release within the first 2 weeks of August. We'll let you know when it becomes available.

guillermoright commented 2 months ago

any update ??

DavoudEshtehari commented 2 months ago

@guillermoright Can you verify the fix in MDS 5.2.2?

guillermoright commented 2 months ago

NativeApi.zip

@guillermoright Can you verify the fix in MDS 5.2.2?

@DavoudEshtehari The fix in version 5.2.2 does not resolve the issue; we are still encountering the same error when releasing the application using dotnet publish -r win-x64 -c Release. If you go to the publish folder (bin\Release\net8.0\win-x64\publish) and execute NativeApi.exe, it throws the same error reported earlier, but in development mode works well

In the example application, you need to replace the values of "database name" and "database server" with real ones. in this line " Repository.Test("{ //here use real database server }", "{ //here use real database name }"); " to connect using Microsoft Entra MFA Server authentication

then go to the browser /weatherforecast

Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HN678PMD990A", Request id "0HN678PMD990A:00000001": An unhandled exception was thrown by the application. Microsoft.Data.SqlClient.SqlException (0x80131904): A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property. ---> System.TypeInitializationException: A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property. ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.MissingMethodException: No parameterless constructor defined for type 'System.Configuration.ClientConfigurationHost'. at System.ActivatorImplementation.CreateInstance(Type, Boolean) + 0x119 at System.Configuration.Internal.ConfigSystem.System.Configuration.Internal.IConfigSystem.Init(Type, Object[]) + 0x43 at System.Configuration.ClientConfigurationSystem..ctor() + 0x47 at System.Configuration.ConfigurationManager.EnsureConfigurationSystem() + 0x56 --- End of inner exception stack trace --- at System.Configuration.ConfigurationManager.PrepareConfigSystem() + 0x35 at System.Configuration.ConfigurationManager.GetSection(String) + 0x18 at System.Runtime.Caching.MemoryCacheStatistics.InitializeConfiguration(NameValueCollection) + 0x2c at System.Runtime.Caching.MemoryCacheStatistics..ctor(MemoryCache, NameValueCollection) + 0x4a at System.Runtime.Caching.MemoryCache.InitDisposableMembers(NameValueCollection) + 0xf1 at Microsoft.Data.SqlClient.ActiveDirectoryAuthenticationProvider..cctor() + 0x8c at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext) + 0xb9 --- End of inner exception stack trace --- at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext) + 0x14a at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnGCStaticBase(StaticClassConstructionContext*, Object) + 0xd at Microsoft.Data.SqlClient.ActiveDirectoryAuthenticationProvider.d19.MoveNext() + 0x4c4 --- End of stack trace from previous location --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xb2 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4b at Microsoft.Data.SqlClient.SqlInternalConnectionTds.<>cDisplayClass148_1.<b1>d.MoveNext() + 0xc8 --- End of stack trace from previous location --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x20 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xb2 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4b at Microsoft.Data.SqlClient.SqlInternalConnectionTds.GetFedAuthToken(SqlFedAuthInfo) + 0x3b8 at Microsoft.Data.SqlClient.SqlInternalConnectionTds.GetFedAuthToken(SqlFedAuthInfo) + 0xb0a at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OnFedAuthInfo(SqlFedAuthInfo) + 0x30f at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior, SqlCommand, SqlDataReader, BulkCopySimpleResultSet, TdsParserStateObject, Boolean&) + 0xa31 at Microsoft.Data.SqlClient.TdsParser.Run(RunBehavior, SqlCommand, SqlDataReader, BulkCopySimpleResultSet, TdsParserStateObject) + 0x43 at Microsoft.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean) + 0x34 at Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo, String, SecureString, TimeoutTimer, Boolean) + 0x18a at Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo, String, SecureString, Boolean, SqlConnectionString, SqlCredential, TimeoutTimer) + 0x5b8 at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer, SqlConnectionString, SqlCredential, String, SecureString, Boolean) + 0x1c6 at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity, SqlConnectionString, SqlCredential, Object, String, SecureString, Boolean, SqlConnectionString, SessionData, Boolean, String, DbConnectionPool, Func3) + 0x4c6 at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions, DbConnectionPoolKey, Object, DbConnectionPool, DbConnection, DbConnectionOptions) + 0x30e at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool, DbConnection, DbConnectionOptions, DbConnectionPoolKey, DbConnectionOptions) + 0x40 at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection, DbConnectionOptions, DbConnectionInternal) + 0x270 at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection, DbConnectionOptions, DbConnectionInternal) + 0x5e at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection, UInt32, Boolean, Boolean, DbConnectionOptions, DbConnectionInternal&) + 0x4be at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection, TaskCompletionSource1, DbConnectionOptions, DbConnectionInternal&) + 0xc0 at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection, TaskCompletionSource1, DbConnectionOptions, DbConnectionInternal, DbConnectionInternal&) + 0x11e at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection, DbConnectionFactory, TaskCompletionSource1, DbConnectionOptions) + 0x118 at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1, SqlConnectionOverrides) + 0x24c at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides) + 0x1ed at NativeApi.Repository.Test(String, String) + 0x211 at Program.<>cDisplayClass0_0.

b2() + 0x1d at Microsoft.AspNetCore.Http.Generated.FF19069A447510870CDDB625AC2BDBA4994CB0D29ABA3059A7673F4741B94F7CBGeneratedRouteBuilderExtensionsCore.<>cDisplayClass3_0.gRequestHandler|4(HttpContext httpContext) + 0x18 at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext) + 0x299 at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext) + 0x38d at Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware.Invoke(HttpContext context) + 0x109 at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.d__238`1.MoveNext() + 0x36f ClientConnectionId:a06399bb-9689-4f86-98a7-0c32b113baa4

arellegue commented 2 months ago

@guillermoright How are you publishing the application? Have you tried a Deployment Mode of Self Contained? Is there a reason why you would not deploy using Deployment mode Self Contained?

guillermoright commented 2 months ago

@guillermoright How are you publishing the application? Have you tried a Deployment Mode of Self Contained? Is there a reason why you would not deploy using Deployment mode Self Contained?

@arellegue It is Native Aot , just like self-contained apps, using this instruccion "PublishAot true PublishAot" so when i run dotnet publish -r win-x64 -c Release , the app include all required runtime libraries

arellegue commented 2 months ago

@guillermoright I see that the issue was closed. Could you try to reference the MDS 6.0-preview1 to see if it fixes the issue, please?

guillermoright commented 1 month ago

@guillermoright I see that the issue was closed. Could you try to reference the MDS 6.0-preview1 to see if it fixes the issue, please?

@arellegue The MDS 6.0-preview1 dosent solve the issue , it is imposible to connect to azure SQL Database HyperScale, for an application when it is released