Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestSharp showing Error of Cannot create an instance of an interface have to manually deserialize

I have RestSharp (which is like HttpClient) call and return data from a Web Api method

I'm getting this error {"Cannot create an instance of an interface."}

My code looks like this:

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;
}

My response.Content shows below.

Now the problem seems to be that the DATA is there as JSON in the Content property of the response object. I can manually deserialize it from there. But is was automatically deserializing it into the Data property before, I'm not sure what is happening now.

If I manually deserialize the json, it works - but why do I need to do this?

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

Response:

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

JSON code returned

[
   {
      "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
   }
]

If I manually deserialize the json, it works - but why do I need to do this?

List<Interest> results = JsonConvert.DeserializeObject<List<Interest>>(response.Content); 
like image 951
Timothy Fisher Avatar asked Aug 09 '16 20:08

Timothy Fisher


People also ask

What is RestSharp DLL?

RestSharp is a comprehensive, open-source HTTP client library that works with all kinds of DotNet technologies. It can be used to build robust applications by making it easy to interface with public APIs and quickly access data without the complexity of dealing with raw HTTP requests.

Can I use RestSharp in .NET core?

Consume the ASP.NET Core API using RestSharp. Once you've installed RestSharp into your project, you can start using it. First, you will need to create an instance of RestClient. The following code snippet shows how you can instantiate and initialize the RestClient class.

Does RestSharp use HttpClient?

Since RestSharp uses the HttpClient, we should consider similar issues regarding the RestClient instantiation.

Is an interface or abstract class and Cannot be instantiated?

Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.


2 Answers

I ran into the same problem. In my case I created a custom object (in your case is Interest object), which had a ICollection list of objects. So, I remove that ICollection and make it as a List of objects. Check whether the Interest has any Interface related property and try make as concrete object property.

Check InternInterests and OfficeInterests property it might be using any kind of interface collection.

like image 157
Zahid Ali Mohammed Avatar answered Oct 16 '22 21:10

Zahid Ali Mohammed


In my case I fix the issue with the following code: var client = new RestClient(); client.AddHandler("application/json", NewtonsoftJsonSerializer.Default); client.BaseUrl = new System.Uri(Host);

Where the class NewtonsoftJsonSerializer is a custom deserializer:

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,
                });
            }
        }
    }
}

I hope this can help.

like image 4
JUVER MALPARTIDA Avatar answered Oct 16 '22 21:10

JUVER MALPARTIDA