3

Simple console application, that should insert data to MySql database fails if published with single-file configuration:

    dotnet publish -c Release -r linux-x64 -p:PublishSingleFile=true -o publish/ GarLoader.MySqlUploader

If I then run it (./publish/GarLoader.MySqlUploader), it fails. The stack trace is:

      System.TypeInitializationException: The type initializer for 'MySql.Data.MySqlClient.Replication.ReplicationManager' threw an exception.
       ---> System.TypeInitializationException: The type initializer for 'MySql.Data.MySqlClient.MySqlConfiguration' threw an exception.
       ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize
       ---> System.NotSupportedException: CodeBase is not supported on assemblies loaded from a single-file bundle.
         at System.Reflection.RuntimeAssembly.get_CodeBase()
         at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig)
         at System.Configuration.ClientConfigPaths.GetPaths(String exePath, Boolean includeUserConfig)
         at System.Configuration.ClientConfigurationHost.get_ConfigPaths()
         at System.Configuration.ClientConfigurationHost.GetStreamName(String configPath)
         at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp()
         at System.Configuration.Internal.DelegatingConfigHost.get_IsAppConfigHttp()
         at System.Configuration.ClientConfigurationSystem..ctor()
         at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
         --- End of inner exception stack trace ---
         at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
         at System.Configuration.ConfigurationManager.PrepareConfigSystem()
         at System.Configuration.ConfigurationManager.GetSection(String sectionName)
         at MySql.Data.MySqlClient.MySqlConfiguration..cctor()
         --- End of inner exception stack trace ---
         at MySql.Data.MySqlClient.MySqlConfiguration.get_Settings()
         at MySql.Data.MySqlClient.Replication.ReplicationManager..cctor()
         --- End of inner exception stack trace ---
         at MySql.Data.MySqlClient.Replication.ReplicationManager.IsReplicationGroup(String groupName)
         at MySql.Data.MySqlClient.MySqlConnection.Open()
         at SqlWorker.ASqlWorker`1.Exec(String command, DbParametersConstructor parameters, Nullable`1 timeout, CommandType commandType, IDbTransaction transaction)
         at GarLoader.MySqlUploader.Inserter`1.InsertAddressObjectTypes(String connectionString, IEnumerable`1 items)
         at GarLoader.MySqlUploader.Inserter`1.InsertItems(String connectionString, IEnumerable`1 items)
         at GarLoader.MySqlUploader.UploaderToMySql.InsertAddressObjectItems[T](IEnumerable`1 items)
         at GarLoader.Engine.Updater.LoadGlobalEntry[T](ZipArchive arch, String entryBeginingSubname, Func`2 prepareItem)
         at GarLoader.Engine.Updater.Update(DownloadFileInfo downloadFileInfo)
         at GarLoader.Engine.Updater.Update()

It runs fine if it is launched by dotnet run without publishing.

Is there any workaround so I could build it as single-file application and run ?

6
  • 1
    this answer can help you stackoverflow.com/questions/68298059/… The .NET MySQL library you are using (MySql.Data) is dependent on ConfigurationManager, which is causing this exception. Commented Oct 13, 2021 at 20:18
  • 1
    Let me know if it works for you. You need to use /p:SelfContained=True /p:PublishProtocol=FileSystem in place of /p:PublishSingleFile=true Commented Oct 13, 2021 at 20:25
  • thanks, @viveknuna, publishing with your parameters resulted to working application! But now there are 249 files instead of one... Commented Oct 13, 2021 at 20:38
  • @obratim it should be like this only, is there any issues with this? Commented Oct 13, 2021 at 20:46
  • @obratim which MySQL client and version are you using? .NET 5 is .NET Core 5, while System.Configuration.ConfigurationManager is a .NET Framework (aka .NET Old) class. You may need to use a newer client version or use a completely different client. Oracle's MySQL/Connector is ... idiosyncratic shall we say. After all, Oracle has no reason to make .NET's life easier. MySqlConnector may be a better alternative. It's used by the most popular EF provider for MySQL Commented Oct 14, 2021 at 10:10

2 Answers 2

7

Short Version

Use MySqlConnector instead of Oracle's MySql.Data.

Why

I suspect you're using Oracle's MySQL/Connector driver. This driver has several problems and using System.Configuration.ConfigurationManager is one of the lesser ones.

System.Configuration.ConfigurationManager is part of .NET Framework, while .NET 5 is actually .NET Core 5. To use it, you have to install a compatibility library, meant only to help migrating .NET Framework applications that used app.config. Instead of releasing a proper .NET Core package, Oracle simply retargeted the .NET Framework package using compatibility libraries.

Worse problems are the inefficient async support and the infrequent releases, which means bugs take several months if not years to get fixed.

A better option, which would solve your current problem, is to use the MySqlConnector package. It has no dependency on compatibility packages in .NET Core (actually, it has no dependencies, period).

Beyond that:

  • it's a true community-built OSS project, as popular as Oracle's own driver (24M downloads vs 29M downloads),
  • it's faster with true asynchronous methods
  • It's used by the most popular Entity Framework provider, Pomelo.EntityFrameworkCore.MySql - 15M downloads to Oracle's 3M downloads.
  • Both packages are actively maintained and have already released previews for EF Core 6 and .NET Core 6.
  • It's worth repeating - no dependencies on .NET Core/.NET 5/6.
Sign up to request clarification or add additional context in comments.

2 Comments

Sadly, MySqlConnector stopped working for me after upgrading mysql from 5.6 to 5.7.
Brilliant. Not only did I notice a speed improvement but the single file package size was cut down by half. I am not 100% sure if its because I binned the Mysql.Data or because I updated all the packages to NET 6 but the combination works well.. Plus it solved the problem the OP was saying. +1000 👍🥇🏆
1

You need to use /p:SelfContained=True /p:PublishProtocol=FileSystem in place of /p:PublishSingleFile=true

2 Comments

The question's point is how to fix the error without disabling single file publishing
@PanagiotisKanavos please guide me if you can help in providing the solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.