【发布时间】:2021-07-15 22:56:59
【问题描述】:
类似于another question,但症状不同,我不想在问题中提问。
在所有面向 .NET Framework 4.8 的项目中使用 VS2017。尝试过最旧 (4.6.0) 和最新 (5.0.2) 版本的 System.Text.Json。还尝试针对 .NET Framework 4.6.1 中的所有项目。我建立了三个项目:
-
MyClassLib - 包含要构建的类和代码的类库 从 T-SQL 查询返回的 JSON 对象实例
-
MyServiceLib - 引用和公开某些服务库 MyClassLib 中的函数。在 Debug 中启动,典型的 WcfSvcHost.exe/WcfTestClient.exe 处理为 MyService
-
MyTestClient - WinForms 客户端同时引用 MyClassLib 和 MyService(通过调试托管 MyServiceLib)来测试功能。在引用 MyService 时重用 MyClassLib 中的对象
MyClassLib 函数在从 WinForms 应用程序调用时执行完美,但从服务库调用时会出现以下错误。失败的具体代码行:
Empl[] empls = JsonSerializer.Deserialize<Empl[]>(jsonString);
在 MyClassLib 中,两个调用路径之间的一切都完全相同相同。有什么区别?!?!
System.TypeInitializationException: The type initializer for 'System.Text.Json.JsonSerializer' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.MemoryExtensions' threw an exception. ---> System.IO.FileLoadException:
Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its
dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) at System.MemoryExtensions.MeasureStringAdjustment() at System.MemoryExtensions..cctor() --- End of inner exception stack trace --- at System.MemoryExtensions.AsSpan(String text) at System.Text.Json.JsonEncodedText.Encode(String value, JavaScriptEncoder encoder) at System.Text.Json.JsonSerializer..cctor() --- End of inner exception stack trace --- at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options) at GlobAppSecLib.GlobAppSec.RetEmplSecurity(String EmplNetworkID, String AppName) in C:\\[blah]\\MyClassLibModule.cs:line 155
System.TypeInitializationException: The type initializer for 'System.Text.Json.JsonSerializer' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.MemoryExtensions' threw an exception. ---> System.IO.FileLoadException:
Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its
dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) at System.MemoryExtensions.MeasureStringAdjustment() at System.MemoryExtensions..cctor() --- End of inner exception stack trace --- at System.MemoryExtensions.AsSpan(String text) at System.Text.Json.JsonEncodedText.Encode(String value, JavaScriptEncoder encoder) at System.Text.Json.JsonSerializer..cctor() --- End of inner exception stack trace --- at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options) at GlobAppSecLib.GlobAppSec.RetEmplSecurity(String EmplNetworkID, String AppName) in C:\\[blah]\\MyClassLibModule.cs:line 155
我注意到 System.Json.Text 拖动的 DLL 是“产品名称”Microsoft .NET Core 和 Microsoft .NET Framework 的混合体:
- System.Text.Json.dll = Microsoft .NET Core
- System.Memory.dll = Microsoft .NET 框架
- System.Runtime.CompilerServices.Unsafe.dll = Microsoft .NET Framework
刚刚注意到 WinForms 客户端以 32 位运行,而 WcfSvcHost.exe 以 64 位运行。
构建 WinForms 服务主机而不是使用 WcfSvcHost.exe 是“解决”问题的一种方法。 @sean-skelly 建议的 this answer 的第 1 部分包含指向 the System.Runtime.CompilerServices.Unsafe NuGet package 的链接。玩这个让我得出结论,你不能让 4.0.4.1 版本的 dll 与 5.0.2 版本的 System.Text.Json 共存。闻起来像微软搞砸了。将所有内容移至 VS 2019 并使用最新版本,但无济于事。 @sean-skelly 建议的答案的第 2 部分导致 an article about bindingRedirect 似乎有些帮助。
【问题讨论】:
-
您不能在 .Net 4.8 库中直接使用
System.Text.Json。System.Text.Json是 .Net 核心,与 .Net 4.8 不兼容。不确定这是否适用于.Net Standard。使用 ios 非常相似并支持 .Net Framework 的 Newtonsoft 可能会更容易 -
您是否尝试过链接中的绑定重定向解决方案?它没有工作吗?另请参阅this answer,特别是关于使用绑定重定向的第 2 部分。
-
很明显,System.Text.Json 确实在 .NET 4.8 库中工作,正如我的帖子所指出的那样。
标签: c# json system.text.json