RestSharp 显示无法创建接口实例的错误必须手动反序列化

Posted

技术标签:

【中文标题】RestSharp 显示无法创建接口实例的错误必须手动反序列化【英文标题】:RestSharp showing Error of Cannot create an instance of an interface have to manually deserialize 【发布时间】:2016-12-16 00:25:34 【问题描述】:

我有 RestSharp(类似于 HttpClient)调用并从 Web Api 方法返回数据

我收到此错误"Cannot create an instance of an interface."

我的代码如下所示:

public List<Interest> GetInterests()

    var request = new RestRequest(apiPath + "GetInterests", Method.GET)  RequestFormat = DataFormat.Json ;
    var response = Client.Execute<List<Interest>>(request);
    return response.Data;

我的response.Content如下所示。

现在的问题似乎是 DATA 在响应对象的 Content 属性中以 JSON 形式存在。我可以从那里手动反序列化它。但是之前它会自动将其反序列化为 Data 属性,我不确定现在发生了什么。

如果我手动反序列化 json,它可以工作 - 但我为什么需要这样做?

List<Interest> results = JsonConvert.DeserializeObject<List<Interest>>(response.Content); 

回复:

"StatusCode: OK, Content-Type: application/json; charset=utf-8, Content-Length: 5597)"

返回的 JSON 代码

[
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":1,
      "InterestName":"Nursing",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":2,
      "InterestName":"Creating/Supporting Networks of Community Partners",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":3,
      "InterestName":"Epidemiology",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":4,
      "InterestName":"Maternal",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":5,
      "InterestName":"Healthcare for the Homeless",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":6,
      "InterestName":"HIV/AIDS, STD",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":7,
      "InterestName":"Nutrition",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":8,
      "InterestName":"Physical Activity",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":9,
      "InterestName":"Oral Health",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":10,
      "InterestName":"Public Health Communication",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":11,
      "InterestName":"Emergency Preparedness and Response",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":12,
      "InterestName":"Public Health Policy",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":13,
      "InterestName":"Tuberculosis",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":14,
      "InterestName":"Teen Pregnancy Prevention",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":15,
      "InterestName":"Vital Registration",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":16,
      "InterestName":"Chronic Disease",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":17,
      "InterestName":"Finance & Management",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":18,
      "InterestName":"Quality Improvement",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":19,
      "InterestName":"Healthy Community Design",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":20,
      "InterestName":"Behavioral Health and Healthcare Integration",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":21,
      "InterestName":"Rx Drug Misuse and Abuse",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":22,
      "InterestName":"Injury Prevention",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":23,
      "InterestName":"School Health",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":24,
      "InterestName":"Worksite Wellness",
      "AddDate":"2016-07-01T17:03:21.61",
      "ModDate":"2016-07-01T17:03:21.64",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   ,
   
      "InternInterests":[

      ],
      "OfficeInterests":[

      ],
      "InterestId":25,
      "InterestName":"Access to Healthy Foods",
      "AddDate":"2016-08-03T08:10:45.4",
      "ModDate":"2016-08-03T08:10:45.4",
      "AddUser":"System",
      "ModUser":"System",
      "IsDeleted":false
   
]

如果我手动反序列化 json,它可以工作 - 但我为什么需要这样做?

List<Interest> results = JsonConvert.DeserializeObject<List<Interest>>(response.Content); 

【问题讨论】:

@Ed thx 是的,json 很讨厌 【参考方案1】:

我遇到了同样的问题。在我的例子中,我创建了一个自定义对象(在你的例子中是兴趣对象),它有一个 ICollection 对象列表。因此,我删除了该 ICollection 并将其作为对象列表。检查兴趣是否有任何与接口相关的属性,并尝试将其作为具体对象属性。

检查 InternInterests 和 OfficeInterests 属性,它可能正在使用任何类型的接口集合。

【讨论】:

如果您对执行方法有类型约束,就会发生这种情况。喜欢 Where T:new() 与 IEnumerable 相同,或者在我的情况下是 F# 中的 seq - 与 RestSharp 序列化程序相比,JSON.net 序列化程序似乎更宽容,并且适用于这些类型 我们刚刚遇到了同样的问题,这正是解决方案。显然,JsonSerializer 的 RestSharp 实现有点幼稚,无法处理接口列表属性(IListIEnumerable 等)。有点烦人——据我所知 JSON.net 处理得很好——但无论如何。至少我们知道发生这种情况时该怎么做。【参考方案2】:

就我而言,我使用以下代码解决了这个问题: var client = new RestClient(); client.AddHandler("application/json", NewtonsoftJsonSerializer.Default); client.BaseUrl = new System.Uri(Host);

NewtonsoftJsonSerializer 类是自定义反序列化器:

using Newtonsoft.Json;
using RestSharp.Deserializers;

namespace IntegrationTests.Common

    class NewtonsoftJsonSerializer : IDeserializer
    
        private Newtonsoft.Json.JsonSerializer serializer;

        public NewtonsoftJsonSerializer(Newtonsoft.Json.JsonSerializer serializer)
        
            this.serializer = serializer;
        

        public string DateFormat  get; set; 

        public string Namespace  get; set; 

        public string RootElement  get; set; 

        public T Deserialize<T>(RestSharp.IRestResponse response)
        
            var content = response.Content;
            return JsonConvert.DeserializeObject<T>(content);
        

        public static NewtonsoftJsonSerializer Default
        
            get
            
                return new NewtonsoftJsonSerializer(new Newtonsoft.Json.JsonSerializer()
                
                    NullValueHandling = NullValueHandling.Ignore,
                );
            
        
    

我希望这会有所帮助。

【讨论】:

【参考方案3】:

@JUVER 你的处理程序确实有帮助 - 谢谢。 2 厘米:

您可能需要在反序列化中进行一些错误处理,否则错误结束 被屏蔽为反序列化错误和

私有 Newtonsoft.Json.JsonSerializer 序列化器;从未使用过。有可能 与构造函数一起删除。

这是带有一些错误处理的反序列化:

public T Deserialize<T>(RestSharp.IRestResponse response)

    if (response.StatusCode == HttpStatusCode.Unauthorized) throw new System.UnauthorizedAccessException();

    if (response.StatusCode != HttpStatusCode.OK || !response.IsSuccessful) throw (new System.Exception(response.Content));

    var content = response.Content;
    return JsonConvert.DeserializeObject<T>(content);

【讨论】:

以上是关于RestSharp 显示无法创建接口实例的错误必须手动反序列化的主要内容,如果未能解决你的问题,请参考以下文章

写了一个控制台应用程序来使用RestSharp来推送数据,但是当我尝试创建一个Windows服务时它不起作用

SQL 网络接口,错误:50 - 发生本地数据库运行时错误。无法创建自动实例

无法使用 RestSharp 和 GoCardless 设置 MIME 类型

无法创建链接服务器 "ORCL" 的 OLE DB 访问接口 "OraOLEDB.Oracle" 的实例 (错误:7302)

RestSharp - 忽略 SSL 错误

无法创建链接服务器 "xxx" 的 OLE DB 访问接口 "OraOLEDB.Oracle" 的实例。 (Microsoft SQL Server,错误: 7