【发布时间】:2020-06-11 16:18:17
【问题描述】:
我正在编写一个将由二进制 PowerShell 模块使用的 .Net Standard 2.0 库。该库基本上是一个 API 客户端,其中包含许多用于处理 JSON 响应的类。在尝试反序列化字符串之前,我确认 API 提供的 JSON 编码字符串没有问题。
当使用 NuGet 包时,compatible 使用 .Net Standard 2.0,我想我会尝试切换到 System.Text.Json,而不是使用 NewtonSoft。但是,它似乎没有在某些平台上所需的特定程序集的版本。
我的环境:
Windows 10
PowerShell 5.1 桌面版
.Net 框架 4.8
PowerShell 核心 6.2.2
dotnet 3.0.100 版
在 PowerShell 桌面 上,当它必须反序列化任何内容时,我会遇到以下问题:
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 会引入其他程序集的问题,这些程序集是 System.Text.Json 的 NuGet 包的一部分。我已经阅读了here 的建议,但我在这里要么不能应用,要么就是不明白。
提前致谢。如果您需要更多信息,请告诉我,我会提供。
编辑
我按照 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>
所以所有绑定重定向的自动生成都不起作用,如上所述,至少还有两个不起作用。我会尝试手动创建它们,但据我所知,现在没有源配置文件可以放入它们。如果有人对此有任何指导,我将不胜感激。
【问题讨论】:
标签: c# .net-standard .net-standard-2.0 system.text.json