当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - ASP.Net Web API 2 async method fails for JSON response

问题描述:

I have below async method in Web API 2:

[System.Web.Http.HttpGet]

public async Task<TestResultResponse> GetAll()

{

DBService repo = new DBService();

TestResultResponse response = new TestResultResponse();

response.Tests = await repo.GetAll(); // call to async method

return response;

}

This API method works fine if request content type is XML ,i.e, response is returned in XML format. However, when I try to get JSON response, I get StackOverflow exception. In VS 2013, I get only below details as part of exception:

An unhandled exception of type 'System.StackOverflowException'

occurred in Newtonsoft.Json.dll

I get this exception only when I request for response in JSON. It works fine for XML response. For sync methods, JSON works fine. It seems to be issue only with JSON and aync methods.

Any suggestions on how to fix this error for JSON response ?

Update 1: As requested below is list of classes used for response:

public class TestResultResponse

{

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]

public List<Test> Tests { get; set; }

}

public class Test : BaseEntity

{

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]

public string Content { get; set; }

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]

public Test InnerDoc { get; set; }

}

public class BaseEntity

{

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]

public ObjectId Id { get; set; }

public int Status { get; set; }

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]

public string CreatedBy { get; set; }

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]

public string UpdatedBy { get; set; }

public DateTime CreatedDate { get; set; }

public DateTime UpdatedDate { get; set; }

}

You can find XML response at http://pastebin.com/Ys7DeZPc

分享给朋友:
您可能感兴趣的文章:
随机阅读: