用30个blob对象反序列化大型json数据
问题描述:
我有一个web服务,它返回一个json字符串。 Alltogehter我有432个JSON对象与blob数据。 在每个Webrequest中,我得到了30个432的对象,直到432的Iam。 对于每个反序列化,反序列化的时间增加。 30对象需要9秒钟,在第二次血化时我们需要15秒。 463对象是1,3 GB。用30个blob对象反序列化大型json数据
如何调整我的代码,30个对象不需要9秒或更多?
的字符串看起来像下面这样:
{
"d:" [
{
"id": "1",
"groesse": 2515221,
"byteasbase64string": "the base64 string"
},
{
"id": "2",
"groesse": 887748,
"byteasbase64string": "the base64 string"
}
]
}
继承人我的C#代码:
List<db.dwaprotokolldatei> getProtokolldatei = new List<db.dwaprotokolldatei>();
using (postStreamGPD = resGPD.GetResponseStream())
{
using (srGPD = new StreamReader(postStreamGPD))
{
string responseFromServerGPD = srGPD.ReadToEnd(); // Long running time
getProtokolldatei = JsonConvert.DeserializeObject<db.rootobject>(responseFromServerGPD).d;// Long running time
responseFromServerGPD = "";
}
}
reqGPD = null;
postDataAsBytesGPD = null;
postStreamGPD.Dispose();
postStreamGPD = null;
resGPD.Dispose();
resGPD = null;
srGPD.Dispose();
srGPD = null;
GC.Collect();
的WebRequest:
reqGPD = (HttpWebRequest)HttpWebRequest.Create("*****");
reqGPD.Method = "POST";
reqGPD.ContentType = "application/json";
mobileJSON = ConvertTOJSONMobile(existingMobile);
postDataAsBytesGPD = Encoding.UTF8.GetBytes(mobileJSON);
postStreamGPD = reqGPD.GetRequestStream();
postStreamGPD.Write(postDataAsBytesGPD, 0, postDataAsBytesGPD.Length);
postStreamGPD.Flush();
postStreamGPD.Dispose();
resGPD = reqGPD.GetResponse();
答
在web配置你需要:
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
</configuration>
+0
这个问题不是来自webservice。在客户端中,我从webservice调用中获取对象。这是问题,ReadToEnd的时间增加了。 – Pascal
答
你可以尝试并行化吗?
List<db.dwaprotokolldatei> getProtokolldatei = new List<db.dwaprotokolldatei();
Parallel.ForEach(urls, url =>
{
var str = GetStringFromWebService(url);
getProtokolldatei.AddRange(Deserialize(str));
DisposeAll();
});
答
ReadToEnd()要快得多。
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
reader.ReadToEnd();
}
只为您的信息 - 在包裹的东西'using'构建这样已经呼吁包裹实例'的Dispose()'方法(在你的情况,'postStreamGPD'和'srGPD')。你不需要手动完成。 –
好的,但这并不能解决我的问题。 – Pascal
不,这就是为什么我没有给它作为答案,只是一个评论。 =) –