c# - .NET 5 MySql 类型初始化异常 : CodeBase is not supported on assemblies loaded from a single-file bundle

标签 c# .net .net-5 .net-core-publishsinglefile typeinitializationexception

如果使用单文件配置发布,应该将数据插入 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 启动它运行良好。

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

最佳答案

精简版

使用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 万次下载与 2900 万次下载),
  • 真正的异步方法更快
  • 它被最流行的 Entity Framework 提供商使用,Pomelo.EntityFrameworkCore.MySql - 1500 万次下载到 Oracle 的 300 万次下载。
  • 这两个包都得到积极维护,并且已经发布了 EF Core 6 和 .NET Core 6 的预览版。
  • 值得重复 - 不依赖于 .NET Core/.NET 5/6。

关于c# - .NET 5 MySql 类型初始化异常 : CodeBase is not supported on assemblies loaded from a single-file bundle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69561799/

相关文章:

binaryformatter - 有没有一种高性能的方法来替换 .NET5 中的 BinaryFormatter?

c# - 当基本词以 I 开头时,如何命名接口(interface)?

c# - UWP 使用 JSON 数据创建列表

c# - 如何在azure wcf中继中处理双工wcf

c# - PInvoke、指针和数组复制

c# - 使用 CDK : Internal Server Error 使用容器镜像部署 .NET 5 AWS Lambda

c# - 创建符合 XSD 架构的 XML 文档的最佳方法是什么?

c# - 这个算法线程安全吗?

c# - 我可以在加载 XDocument 时使用预定义的命名空间吗?

.net - 如何知道 .fsx 脚本是由 .NETCore/.NET5 还是由遗留框架运行的?