小编Tim*_*her的帖子

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

我有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;
}
Run Code Online (Sandbox Code Playgroud)

我的回答.内容如下所示.

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

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

List<Interest> results = JsonConvert.DeserializeObject<List<Interest>>(response.Content); 
Run Code Online (Sandbox Code Playgroud)

响应:

"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", …
Run Code Online (Sandbox Code Playgroud)

c# json restsharp deserialization asp.net-web-api

8
推荐指数
1
解决办法
3303
查看次数

MVC控制器需要空的构造函数,但是然后不使用接口并抛出错误

我收到此错误:

An exception of type 'System.NullReferenceException' occurred in PubStuff.Intern.Web.Internal.dll but was not handled in user code Additional information: Object reference not set to an instance of an object

public class InternController : BaseController
{
    IInternService _internService;


    public InternController() { }

    public InternController(IInternService internService)
    {
        _internService = internService;
    }


    // GET: Intern
    public ActionResult Index()
    {
        object responseObject = null;


        responseObject = _internService.GetAllSkills();

        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 它抱怨如果我没有空构造函数
  2. 一旦有一个空的构造responseObject = _internService.GetAllSkills();函数,那么这一行就会抛出错误.

_internService为null

我该如何解决? 有什么问题?

更新 我最终遇到了StructureMap的问题,无论我是否添加了IInternUnitOfWork …

c# structuremap asp.net-mvc interface inversion-of-control

1
推荐指数
1
解决办法
780
查看次数