繁体   English   中英

.NET 5 MySql TypeInitializationException:从单文件包加载的程序集不支持 CodeBase

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

简单的控制台应用程序,如果使用单文件配置发布,则应该将数据插入到 MySql 数据库失败:

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

如果我然后运行它( ./publish/GarLoader.MySqlUploader ),它会失败。 堆栈跟踪是:

      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()

如果它由dotnet run而不发布,它运行良好。

是否有任何解决方法,以便我可以将其构建为单文件应用程序并运行?

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

精简版

使用MySqlConnector而不是 Oracle 的MySql.Data

为什么

我怀疑您正在使用 Oracle 的 MySQL/Connector 驱动程序。 这个驱动程序有几个问题,使用System.Configuration.ConfigurationManager是较小的问题之一。

System.Configuration.ConfigurationManager是 .NET Framework 的一部分,而 .NET 5 实际上是 .NET Core 5。要使用它,您必须安装一个兼容性库,仅用于帮助迁移使用app.config .NET Framework 应用程序。 Oracle 没有发布适当的 .NET Core 包,而是使用兼容性库简单地重新定位了 .NET Framework 包。

更糟糕的问题是低效的异步支持和不频繁的发布,这意味着错误需要几个月甚至几年才能修复。

可以解决您当前问题的更好选择是使用MySqlConnector包。 依赖于 .NET Core 中的兼容包(实际上,它没有依赖关系,句号)。

除此之外:

  • 这是一个真正的社区构建的 OSS 项目,与 Oracle 自己的驱动程序一样受欢迎(2400 万次下载 vs 2900 万次下载),
  • 使用真正的异步方法会更快
  • 最流行的实体框架提供商Pomelo.EntityFrameworkCore.MySql 使用它- 15M 下载量到 Oracle 的 3M 下载量。
  • 这两个包都得到积极维护,并且已经发布了 EF Core 6 和 .NET Core 6 的预览版。
  • 值得重申 - 不依赖于 .NET Core/.NET 5/6。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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