I tried to setup a Web API service and a client Web API client. The service is simple and returns a concrete type:
[HttpGet]
public IEnumerable<MyDataClass> GetMyDataClasses()
{
return _repository.GetMyDataClasses();
}
That works great. I can see the XML in the browser, and I can get JSON back in fiddler. The problem comes when I try and consume the service and deserialize the data back into my concrete. The Client has access to the same "MyDataClass" and passes it as the generic parameter into this method:
public T GetDataFromService<T>()
{
using (var httpClient = new HttpClient())
{
T result = default(T);
Task<HttpResponseMessage> responseTask = null;
httpClient.GetAsync(_endpoint).ContinueWith((requestTask) =>
{
responseTask = requestTask;
HttpResponseMessage response = requestTask.Result;
response.EnsureSuccessStatusCode();
response.Content.ReadAsAsync<T>().ContinueWith((readTask) =>
{
result = readTask.Result;
});
});
// HACK: My version of the await keyword
while (responseTask == null || !responseTask.IsCompleted || result == null) { }
return result;
}
}
When the code hits "result = readTask.Result", it bombs saying:
"The input stream contains too many delimiter characters which may be a sign that the incoming data may be malicious."
I tried out the JSON library from Newtonsoft and it works like a charm, just need to substitute the inner async call with:
response.Content.ReadAsStringAsync().ContinueWith((readTask) =>
{
result = JsonConvert.DeserializeObject<T>(readTask.Result);
});
This was my previous post which showed the complete code that works.
I get the same error whether I use "ReadAsAsync" or "ReadAsOrDefaultAsync". Here is the complete code I'm using:
public T GetDataFromService<T>()
{
using (var httpClient = new HttpClient())
{
var response = httpClient.GetAsync(_endpoint).Result;
// This doesn't work:
return response.Content.ReadAsAsync<T>().Result;
// This doesn't work:
return response.Content.ReadAsOrDefaultAsync<T>().Result;
// This DOES work:
return JsonConvert.DeserializeObject<T>(response.Content.ReadAsStringAsync().Result);
}
}
skippyfire
Member
34 Points
31 Posts
Bug with Web API deserialization?
Feb 22, 2012 12:38 PM|LINK
I tried to setup a Web API service and a client Web API client. The service is simple and returns a concrete type:
[HttpGet] public IEnumerable<MyDataClass> GetMyDataClasses() { return _repository.GetMyDataClasses(); }That works great. I can see the XML in the browser, and I can get JSON back in fiddler. The problem comes when I try and consume the service and deserialize the data back into my concrete. The Client has access to the same "MyDataClass" and passes it as the generic parameter into this method:
public T GetDataFromService<T>() { using (var httpClient = new HttpClient()) { T result = default(T); Task<HttpResponseMessage> responseTask = null; httpClient.GetAsync(_endpoint).ContinueWith((requestTask) => { responseTask = requestTask; HttpResponseMessage response = requestTask.Result; response.EnsureSuccessStatusCode(); response.Content.ReadAsAsync<T>().ContinueWith((readTask) => { result = readTask.Result; }); }); // HACK: My version of the await keyword while (responseTask == null || !responseTask.IsCompleted || result == null) { } return result; } }When the code hits "result = readTask.Result", it bombs saying:
"The input stream contains too many delimiter characters which may be a sign that the incoming data may be malicious."
I tried out the JSON library from Newtonsoft and it works like a charm, just need to substitute the inner async call with:
response.Content.ReadAsStringAsync().ContinueWith((readTask) => { result = JsonConvert.DeserializeObject<T>(readTask.Result); });This was my previous post which showed the complete code that works.
http://forums.asp.net/p/1772530/4845207.aspx/1?p=True&t=634654962270492783
SixiS
Member
50 Points
16 Posts
Re: Bug with Web API deserialization?
Feb 22, 2012 01:24 PM|LINK
You have to add the namespace System.Net.Http and use the ReadAsOrDefaultAsync<T>() method to parse the response.
skippyfire
Member
34 Points
31 Posts
Re: Bug with Web API deserialization?
Feb 22, 2012 01:35 PM|LINK
I get the same error whether I use "ReadAsAsync" or "ReadAsOrDefaultAsync". Here is the complete code I'm using:
public T GetDataFromService<T>() { using (var httpClient = new HttpClient()) { var response = httpClient.GetAsync(_endpoint).Result; // This doesn't work: return response.Content.ReadAsAsync<T>().Result; // This doesn't work: return response.Content.ReadAsOrDefaultAsync<T>().Result; // This DOES work: return JsonConvert.DeserializeObject<T>(response.Content.ReadAsStringAsync().Result); } }Any ideas?
SixiS
Member
50 Points
16 Posts
Re: Bug with Web API deserialization?
Feb 22, 2012 01:58 PM|LINK
i think that is a limitation in the System.Net.Http.Formatting.dll. so go ahead with json.net. you can also replace the default json formatter with json.net if you like. https://github.com/ChristianWeyer/Thinktecture.Web.Http/blob/master/Thinktecture.Web.Http/Formatters/JsonNetFormatter.cs
Kiran Challa
Participant
1442 Points
281 Posts
Microsoft
Re: Bug with Web API deserialization?
Feb 22, 2012 02:12 PM|LINK
You can modify the following static property on the MediaTypeFormatter class (default is 'false') to skip the error:
public static bool SkipStreamLimitChecks { get; set; }
BTW, this property is not visible in VS Intellisense, but you should still be able to use it. Please let us know otherwise.
Kiran Challa
skippyfire
Member
34 Points
31 Posts
Re: Bug with Web API deserialization?
Feb 22, 2012 02:24 PM|LINK
Where is that property? I don't see any statics on the MediaTypeFormatter class. I can't find that property anywhere.
Kiran Challa
Participant
1442 Points
281 Posts
Microsoft
Re: Bug with Web API deserialization?
Feb 22, 2012 02:26 PM|LINK
It's on the the MediaTypeFormatter class only. But this property is not visible in VS Intellisense, but you should still be able to use it.
Kiran Challa
skippyfire
Member
34 Points
31 Posts
Re: Bug with Web API deserialization?
Feb 22, 2012 02:27 PM|LINK
OK cool, I see it now. What exactly does that property do? Is there a size limit on the stream? Or some other kind of limitation?
SixiS
Member
50 Points
16 Posts
Re: Bug with Web API deserialization?
Feb 22, 2012 02:34 PM|LINK
the property enable or disable the delimeter counter during reading the stream. the current limit is 1000 delimeters.
skippyfire
Member
34 Points
31 Posts
Re: Bug with Web API deserialization?
Feb 22, 2012 02:55 PM|LINK
Is that a rudimentary protection mechanism against DDOS attacks?