簡單的控制臺應用程式,如果使用單檔案配置發布,則應該將資料插入到 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沒有發布的情況下啟動它,它運行良好。
是否有任何解決方法,以便我可以將其構建為單檔案應用程式并運行?
uj5u.com熱心網友回復:
您需要使用/p:SelfContained=True /p:PublishProtocol=FileSystem代替/p:PublishSingleFile=true
uj5u.com熱心網友回復:
精簡版
使用MySqlConnector而不是 Oracle 的MySql.Data.
為什么
我懷疑您正在使用 Oracle 的 MySQL/Connector 驅動程式。這個驅動程式有幾個問題,使用System.Configuration.ConfigurationManager是較小的問題之一。
System.Configuration.ConfigurationManager是 .NET Framework 的一部分,而 .NET 5 實際上是 .NET Core 5。要使用它,您必須安裝兼容庫,僅用于幫助遷移使用 .NET Framework 的 .NET Framework 應用程式app.config。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。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/313584.html
