简体   繁体   中英

.NET 5 MySql TypeInitializationException: CodeBase is not supported on assemblies loaded from a single-file bundle

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 ?

您需要使用/p:SelfContained=True /p:PublishProtocol=FileSystem代替/p:PublishSingleFile=true

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM