【发布时间】:2016-09-10 03:31:09
【问题描述】:
这与我的问题 HTTPClient Buffer Exceeded 2G; Cannot write more bytes to the buffer 相关,但差异很大,以至于 IMO 需要一个单独的问题。
在另一个问题中,我试图弄清楚如何处理破坏 2G 请求缓冲区。这个想法是使用流媒体,但我需要反序列化。在与 Google 教授交谈时,我发现我必须使用 TextReader 进行流式处理/反序列化。所以我的代码是:
public async Task<API_Json_Special_Feeds.RootObject> walMart_Special_Feed_Lookup(string url)
{
special_Feed_Lookup_Working = true;
HttpClientHandler handler = new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
using (HttpClient http = new HttpClient(handler))
{
http.DefaultRequestHeaders.AcceptEncoding.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("gzip"));
http.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
url = String.Format(url);
using (var response = await http.GetAsync(url, HttpCompletionOption.ResponseHeadersRead))
{
Console.WriteLine(response);
var serializer = new JsonSerializer();
using (StreamReader sr = new StreamReader(await response.Content.ReadAsStreamAsync()))
{
using (var jsonTextReader = new JsonTextReader(sr))
{
API_Json_Special_Feeds.RootObject root = (API_Json_Special_Feeds.RootObject)serializer.Deserialize(jsonTextReader);
return root;
}
}
}
}
}
现在,如您所见,返回类型是强类型的。方法的返回类型匹配。现在,我去呼叫线路:
API_Json_Special_Feeds.RootObject Items = await net.walMart_Special_Feed_Lookup(specialFeedsURLs[i].Replace("{apiKey}", Properties.Resources.API_Key_Walmart));
所以,我们一直都有匹配类型 API_Json_Special_Feeds.RootMethod。
运行时,调用行抛出 InvalidCastException:
不良结果:
Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'RootObject'
我在返回之前检查了方法的末尾,结果确实在返回之前从对象转换为 API_Json_Special_Feeds.RootMethod。
问题:在返回语句和调用行之间的某个位置,正在返回的对象正在从 API_Json_Special_Feeds.RootMethod 转换为 Newtonsoft.Json.Linq.JObject。我无法调试它,因为两者之间没有代码。如果我在调用行中再次投射,我会收到“无法投射”错误。如何防止此对象类型的降级/更改?
很多很多人都在考虑您的时间、考虑以及您可以提供的任何想法或建议!
【问题讨论】: