无法解析类型:System.String、System.Private.CoreLib、版本 4.0.0.0 - Asp.net Core (.net core 2.1) 服务器、.NET 4.6.1 客户端
我有一个用 .net 4.6.1 编写的旧客户端应用程序,它使用 WCF 服务器的数据.我正在尝试用 ASP.NET Core 应用程序和 Protobuf 序列化替换 WCF 服务器.我正在使用库:
I've got a legacy client application written in .net 4.6.1 consuming data of a WCF server. I am trying to replace the WCF server with an ASP.NET Core application and Protobuf serialization. I am using libraries:
- Protobuf-net 在客户端和服务器中用于序列化/去-序列化
- 核心 WebApi 格式化程序在服务器中
- Protobuf-net in both the client and server for serialization/de-serialization
- Core WebApi Formatters in the server
在客户端尝试反序列化返回的内容,如下所示:
Trying in the client side to deserialize the returned content like below:
var resultData = ProtoBuf.Serializer.Deserialize<ExcelDropdownNode>(response.Content.ReadAsStreamAsync().Result);
但是我在 protobuf 库中遇到错误:
But I am getting an error in the protobuf library:
无法解析类型:System.String、System.Private.CoreLib、Version=4.0.0.0、Culture=neutral、PublicKeyToken=7cec85d7bea7798e(您可以使用 TypeModel.DynamicTypeFormatting 事件提供自定义映射)
Unable to resolve type: System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e (you can use the TypeModel.DynamicTypeFormatting event to provide a custom mapping)
知道我做错了什么吗?
顺便说一句,我必须在服务器中返回动态数据,protobuf 对此并不满意.我用 DynamicType=True
标记了一个 List
属性,并将所有 ValueTypes 更改为字符串(即 4 -> "4").这允许服务器端的序列化工作.
As a side note, I have to return dynamic data in the server and protobuf isn't very happy about it. I have marked a List<object>
property with DynamicType=True
and changing all ValueTypes to string (i.e 4 -> "4"). This allowed the serialization on the server-side to work.
ExcelDropDownCode 中的动态属性如下:
The dynamic property in the ExcelDropDownCode is as below:
[ProtoMember(1, DynamicType = true)]
public List<object> DataItems
{
get { return dataItems; }
set { dataItems = value; }
}
通过在客户端添加类型解析代码来解决:
Managed to solve by adding in the client side the type resolution code below:
RuntimeTypeModel.Default.DynamicTypeFormatting += (sender, args) => {
if (args.FormattedName.Contains("System.String, System.Private.CoreLib"))
{
args.Type = typeof(string);
}};