Bug #111988 c# connector not working with native aot
Submitted: 7 Aug 2023 11:48 Modified: 12 Nov 2023 10:54
Reporter: Ivan Kara Email Updates:
Status: No Feedback Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:8.1.0 OS:Any
Assigned to: MySQL Verification Team CPU Architecture:Any
Tags: native aot

[7 Aug 2023 11:48] Ivan Kara
Description:
i cannot use this library to connect to mysql if i enabled native aot. i am currently using the latest .net 8.0 (asp net core). here is the error log:
```
 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.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) + 0x108
    at System.Configuration.Internal.ConfigSystem.System.Configuration.Internal.IConfigSystem.Init(Type, Object[]) + 0x47
    at System.Configuration.ClientConfigurationSystem..ctor() + 0x4c
    at System.Configuration.ConfigurationManager.EnsureConfigurationSystem() + 0x57
    --- End of inner exception stack trace ---
    at System.Configuration.ConfigurationManager.EnsureConfigurationSystem() + 0xe6
    at System.Configuration.ConfigurationManager.PrepareConfigSystem() + 0x12
    at System.Configuration.ConfigurationManager.GetSection(String) + 0x1b
    at MySql.Data.MySqlClient.MySqlConfiguration..cctor() + 0x14
    at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xb6
    --- End of inner exception stack trace ---
    at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0x14b
    at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnGCStaticBase(StaticClassConstructionContext*, Object) + 0x9
    at MySql.Data.MySqlClient.Replication.ReplicationManager..cctor() + 0x27b
    at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xb6
    --- End of inner exception stack trace ---
    at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xac
    at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnGCStaticBase(StaticClassConstructionContext*, Object) + 0x9
    at MySql.Data.MySqlClient.Replication.ReplicationManager.IsReplicationGroup(String) + 0x8e
    at MySql.Data.MySqlClient.MySqlConnection.<OpenAsync>d__94.MoveNext() + 0xc84
 --- End of stack trace from previous location ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xbe
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4e
    at server.Services.DbConnection.<CreateAsync>d__2.MoveNext() + 0x1ab
 --- End of stack trace from previous location ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xbe
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4e
    at server.Services.Registration.RegistrationRepository.<GetOtpTokenAsync>d__8.MoveNext() + 0xec
 --- End of stack trace from previous location ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xbe
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4e
    at server.Services.Registration.RegistrationService.<SendOtpAsync>d__11.MoveNext() + 0x31a
 --- End of stack trace from previous location ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xbe
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4e
    at server.Controllers.Registration.RegistrationController.<SendOtpAsync>d__9.MoveNext() + 0x14f
 --- End of stack trace from previous location ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xbe
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4e
    at Microsoft.AspNetCore.Http.Generated.<GeneratedRouteBuilderExtensions_g>FB49A76E00958A78ED8A3DC031D4149080B37ED6EB129C512F9112A3DB80F087D__GeneratedRouteBuilderExtensionsCore.<>c__DisplayClass12_22.<<-cctor>g__RequestHandler|133>d.MoveNext() + 0x24a
 --- End of stack trace from previous location ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xbe
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4e
    at server.Controllers.AuthenticationMiddleware.<Invoke>d__2.MoveNext() + 0x4d4
 --- End of stack trace from previous location ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xbe
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4e
    at server.Controllers.ExceptionHandlerMiddleware.<Invoke>d__2.MoveNext() + 0xe3
```

How to repeat:
just use the library like normal to fetch a data from mysql and in the .csproj add this:
`<PublishAot>true</PublishAot>`

to build use this command: `dotnet publish -c Release`

finally execute the generated native executable.
[8 Aug 2023 3:07] Ivan Kara
this might be the fix

https://github.com/dotnet/runtimelab/issues/2278#issuecomment-1555452279
[12 Oct 2023 10:54] MySQL Verification Team
Hello Ivan,

Thank you for the bug report.
Could you please provide repeatable test case (sample project, etc. - please make it as private if you prefer) to confirm this issue at our end?

Regards,
Ashwini Patil
[13 Nov 2023 1:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".