Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用.NET Core 3.0中的AssemblyLoadContext加载插件依赖项_C#_.net Core_Npgsql_.net Core 3.0 - Fatal编程技术网

C# 无法使用.NET Core 3.0中的AssemblyLoadContext加载插件依赖项

C# 无法使用.NET Core 3.0中的AssemblyLoadContext加载插件依赖项,c#,.net-core,npgsql,.net-core-3.0,C#,.net Core,Npgsql,.net Core 3.0,我有一个依赖性失败的插件,尽管我试图在.NETCore3.0上尽可能多地遵循微软的建议。两天来一直在试图找出问题所在:/。欣赏一个输入或者去哪里看。谢谢 详情: 入口点是由.NET Core 3.0自包含应用程序发布的CLI应用程序 //publishes as EXE file dotnet publish -c release -r win-x64 /p:publishsinglefile=true /p:publishtrimmed=true 在Win-x64和Linux-x64上测

我有一个依赖性失败的插件,尽管我试图在.NETCore3.0上尽可能多地遵循微软的建议。两天来一直在试图找出问题所在:/。欣赏一个输入或者去哪里看。谢谢

详情:

  • 入口点是由.NET Core 3.0自包含应用程序发布的CLI应用程序
    //publishes as EXE file
    dotnet publish -c release -r win-x64 /p:publishsinglefile=true /p:publishtrimmed=true
    
  • 在Win-x64和Linux-x64上测试
  • .NET核心3.0程序集
  • 使用此命令发布插件DLL

    //publishes as DLL files
    dotnet publish -c release -r linux-x64 /p:publishtrimmed=true -o ./app
    
日志显示文件存在,但依赖项解析程序未能加载该文件,并引发FileNotFoundException。奇怪


C:\play\yuniql\yuniql-cli\bin\release\netcoreapp3.0\win-x64\publish>yuniql.exe run --plugins-path C:\temp\aztools\yuniql\0.0.0\x64\.plugins -p C:\play\yuniql\yuniql-plugins\postgresql\samples -c "Host=localhost;Port=5432;Username=xxx;Password=xxx;Database=yuniqldb" --platform postgresql -a 1 --debug
...
DBG   platformLowerCased: postgresql
DBG   pluginsPathParameter: C:\temp\aztools\yuniql\0.0.0\x64\.plugins
DBG   defaultAssemblyBasePath: C:\temp\aztools\yuniql\0.0.0\x64\.plugins\postgresql
...
DBG   Found plugin dir: C:\temp\aztools\yuniql\0.0.0\x64\.plugins\postgresql
DBG   Found plugin file: C:\temp\aztools\yuniql\0.0.0\x64\.plugins\postgresql\Npgsql.dll. ProductVersion: 4.1.1, FileVersion: 4.1.1.0
DBG   Found plugin file: C:\temp\aztools\yuniql\0.0.0\x64\.plugins\postgresql\Yuniql.Extensibility.dll. ProductVersion: 0.302.0, FileVersion: 0.302.0.0
DBG   Found plugin file: C:\temp\aztools\yuniql\0.0.0\x64\.plugins\postgresql\Yuniql.PostgreSql.deps.json. ProductVersion: , FileVersion:
DBG   Found plugin file: C:\temp\aztools\yuniql\0.0.0\x64\.plugins\postgresql\Yuniql.PostgreSql.dll. ProductVersion: 0.305.0, FileVersion: 0.305.0.0
DBG   Found plugin file: C:\temp\aztools\yuniql\0.0.0\x64\.plugins\postgresql\Yuniql.PostgreSql.pdb. ProductVersion: , FileVersion:

DBG   pluginAssemblyFilePath: C:\temp\aztools\yuniql\0.0.0\x64\.plugins\postgresql\Yuniql.PostgreSql.dll
DBG   loadedAssembly: Yuniql.PostgreSql, Version=0.305.0.0, Culture=neutral, PublicKeyToken=null
DBG   retryResolving: Npgsql, Version=4.1.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7
DBG   failedAssemblyFileExists: True
DBG   attempting to reload Npgsql, Version=4.1.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7 via streaming from C:\temp\aztools\yuniql\0.0.0\x64\.plugins\postgresql\Npgsql.dll

ERR   Failed to execute run function. Target database will be rolled back to its previous state.
System.IO.FileNotFoundException: Could not load file or assembly 'Npgsql, Version=4.1.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'. The system cannot find the file specified.
File name: 'Npgsql, Version=4.1.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'
   at Yuniql.PostgreSql.PostgreSqlDataService.GetConnectionInfo()
   at Yuniql.Core.MigrationService.Run(String workingPath, String targetVersion, Boolean autoCreateDatabase, List`1 tokenKeyPairs, Boolean verifyOnly, String delimeter)
   at Yuniql.CLI.CommandLineService.RunMigration(RunOption opts)

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Npgsql, Version=4.1.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'. The system cannot find the file specified.
File name: 'Npgsql, Version=4.1.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'
   at Yuniql.PostgreSql.PostgreSqlDataService.GetConnectionInfo()
   at Yuniql.Core.MigrationService.Run(String workingPath, String targetVersion, Boolean autoCreateDatabase, List`1 tokenKeyPairs, Boolean verifyOnly, String delimeter)
   at Yuniql.CLI.CommandLineService.RunMigration(RunOption opts)
   at Yuniql.Program.<>c__DisplayClass0_0.<Main>b__1(RunOption opts)
   at CommandLine.ParserResultExtensions.MapResult[T1,T2,T3,T4,T5,T6,T7,T8,TResult](ParserResult`1 result, Func`2 parsedFunc1, Func`2 parsedFunc2, Func`2 parsedFunc3, Func`2 parsedFunc4, Func`2 parsedFunc5, Func`2 parsedFunc6, Func`2 parsedFunc7, Func`2 parsedFunc8, Func`2 notParsedFunc)
   at Yuniql.Program.Main(String[] args)
C:\play\yuniql\yuniql-cli\bin\release\netcoreapp3.0\win-x64\publish>
从/app/.plugins/PostgreSql加载插件

//创建不可加载的HostAssemblyLoadContext
var pluginasemblyloadcontext=新pluginasemblyloadcontext(pluginasemblybasepath);
//将插件程序集导入HostAssemblyLoadContext。
//assemblyPath必须是绝对路径。
var assembly=pluginasemblyLoadContext.LoadFromAssemblyPath(pluginasemblyFilePath);
pluginAssemblyLoadContext.Assemblies
托利斯先生()
.ForEach(a=>
{
_调试($“loadedAssembly:{a.FullName}”);
});
pluginAssemblyLoadContext.Resolving+=AssemblyContext\u Resolving;
pluginAssemblyLoadContext.Unload+=AssemblyContext\u Unload;
var sqlDataService=assembly.GetTypes()
.Where(t=>t.Name.ToLower()包含($“{platformLowerCased.ToLower()}dataservice”))
.Select(t=>Activator.CreateInstance(t,_-traceService))
.Cast()
.First();
var bulkImportService=assembly.GetTypes()
.Where(t=>t.Name.ToLower()。包含($“{platformLowerCased.ToLower()}bulkimportservice”))
.Select(t=>Activator.CreateInstance(t,_-traceService))
.Cast()
.First();
var tokenReplacementService=新的tokenReplacementService(_-traceService);
var migrationService=new migrationService(sqlDataService、bulkImportService、tokenReplacementService、directoryService、fileService、_-traceService);
返回迁移服务;
试图强迫解决

private System.Reflection.Assembly AssemblyContext_解析(System.Runtime.Loader.AssemblyLoadContext AssemblyContext,System.Reflection.AssemblyName failedAssembly)
{
var pluginasemblyloadcontext=assemblyContext作为pluginasemblyloadContext;
_traceService.Debug($“retryResolving:{failedAssembly.FullName}”);
var assemblyFilePath=Path.Combine(pluginAssemblyLoadContext.PluginPath,failedAssembly.Name+“.dll”);
_traceService.Debug($“failedAssemblyFileExists:{File.Exists(assemblyFilePath)}”);
使用(var file=file.Open(assemblyFilePath,FileMode.Open,FileAccess.Read,FileShare.Read))
{
_traceService.Debug($“试图通过从{assemblyFilePath}流重新加载{failedAssembly.FullName}”);
pluginAssemblyLoadContext.LoadFromStream(文件);
}
//assemblyContext.LoadFromAssemblyPath(assemblyFilePath);
//assemblyContext.LoadFromAssemblyName(新的System.Reflection.AssemblyName(failedAssembly.Name));
返回null;
}
2019年12月26日更新:我认为这只是linux的问题,但我能够在Windows中重现。有趣的是,当我运行直接调用工厂类但工厂仍然使用AssemblyLoadContext的平台测试时,它工作正常


不同的是,当CLI(一个.NET Core 3.0自带的EXE应用程序)调用它时失败,但当从VSTestRunner(MSTEST)调用它时,它正常。

删除publishtrimmed是否有帮助?@ShayRojansky,我今天将尝试此方法。谢谢它似乎与Npgsql无关,因为我也可以在MySql插件中复制。正如我在更新中所述,在运行自动测试时,我能够通过插件成功运行,但由于插件是在CLI中作为入口点构建的而失败。@ShayRojansky所以我在没有修剪的情况下发布了CLI和插件。此错误仍然存在。无论如何,谢谢:)
publish CLI而不修剪//dotnet publish-c release-r win-x64/p:publishsinglefile=true
publish postgresql plugin而不修剪//dotnet publish-c release-r win-x64
我将在本周找个时间尝试DotNetCorePlugins库,可能已经解决了我面临的这些问题。