因为在使用NuGet包时它与.NET Standard 2.0兼容,所以我想尝试切换到System.Text.Json,而不是使用NewtonSoft。但是,在某些平台上,它似乎没有特定程序集所需的版本。
我的环境: Windows 10 PowerShell 5.1桌面版 .NET Framework 4.8 PowerShell Core 6.2.2 dotnet版本3.0.100
在PowerShell Desktop上,当它必须反序列化任何内容时,我会遇到以下问题:
PS dir:\> Import-Module '.\file.dll'
PS dir:\> [namespace.class]::TestMethod($string, $anotherString) # Test method to return string
{"attribute":"value"}
PS dir:\> [namespace.class]::Method($string, $anotherString) # Same as above, but uses System.Text.Json to deserialise
Could not load file or assembly 'System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system
cannot find the file specified.
# System.Buffers.dll is with the System.Text.Json package, but seems to be the wrong version
PS dir:\> (Get-Item .\System.Buffers.dll).VersionInfo.FileVersion
4.6.26515.06
PS dir:\> [System.Reflection.Assembly]::LoadFile("$pwd\System.Buffers.dll").GetName().Version
Major Minor Build Revision
----- ----- ----- --------
4 0 3 0
在PowerShell Core中,对于另一个程序集/文件也存在相同的异常。
PS dir:\> Import-Module '.\file.dll'
PS dir:\> [namespace.class]::Method($string, $anotherString)
"Could not load file or assembly 'System.Text.Encodings.Web, Version=4.0.5.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621)"
#System.Text.Encodings.Web.dll is with the System.Text.Json package and appears to be the required version...
PS dir:\> (Get-Item .\System.Text.Encodings.Web.dll).VersionInfo.FileVersion
4.700.19.56404
[System.Reflection.Assembly]::LoadFile("$pwd\System.Text.Encodings.Web.dll").GetName().Version
Major Minor Build Revision
----- ----- ----- --------
4 0 5 0
有没有人能给我一些建议,让我不用切换到Newtonsoft的JSON包就能解决这个问题?从System.Text.Json 4.7.1回退到4.7.0或4.6.0会引入与NuGet包中System.Text.Json的其他程序集相关的问题。我已经在这里读到了建议,但要么我无法在这里应用它,要么我根本不理解。
提前感谢您的帮助。如果需要更多信息,请告诉我,我会提供。
编辑:
我按照Gokhan的建议更新了csproj。
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
这将在appName.dll.config文件中生成以下代码
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
所以自动生成所有绑定重定向并不起作用,如上所述,至少还有两个不起作用。我会尝试手动创建它们,但据我所知,现在没有源配置文件可放置它们。如果有人对此有任何指导,我将不胜感激。
net472
或更低版本,我是否仍然可以使用PowerShellStandard.Library
包,以便库可以在PowerShell桌面版和核心版上运行?如果它需要在两个运行时上运行,是否需要为两个运行时发布dlls?我有另一个模块,它针对4.7.2与PowerShell 5库,跨平台运行良好,但不知道我是否只是“幸运”。如果我还是没有理解,请原谅。 - Ashdotnet publish -f netcoreapp2.0
和dotnet publish -f net461
。这将创建两个单独的发布目录,一个用于每个框架。然后,在从完整 PowerShell 运行时从 net461 文件夹加载您的模块,在从 PowerShell Core 运行时从 netcoreapp2.0 文件夹加载。这样应该可以使两种情况都能正常工作,同时只保留一个代码库。 - Jose Perez Rodriguez