无法解析类型:System.String、System.Private.CoreLib、版本 4.0.0.0 - Asp.net Core (.net core 2.1) 服务器、.NET 4.6.1 客户端 [英] Unable to resolve type: System.String, System.Private.CoreLib, Version 4.0.0.0 - Asp.net Core (.net core 2.1) server, .NET 4.6.1 Client

查看:22
本文介绍了无法解析类型: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 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);
}};

这篇关于无法解析类型:System.String、System.Private.CoreLib、版本 4.0.0.0 - Asp.net Core (.net core 2.1) 服务器、.NET 4.6.1 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆