您的位置:首页 > 其它

webservice---序列化和反序列化(针对网上DataSet优化)

2012-06-09 12:00 381 查看
只列出这四种数据的返回形式:
(1)直接返回DataSet对象
(2)返回DataSet对象用Binary序列化后的字节数组
(3)返回DataSetSurrogate对象用Binary序列化后的 字节数组
(4)返回DataSetSurrogate对象用Binary序列化并Zip 压缩后的字节数组

DataSetSurrogate 类为微软在2.0的时候提供的压缩DataSet的类,下载/Files/fujinliang/DataSetSurrogate.rar

代码如下:

[WebMethod(Description = "直接返回DataSet对象")]
public DataSet GetDataSet()
{
string connStr = System.Configuration.ConfigurationManager.AppSettings["ConnString"].ToString();
SqlConnection conn = new SqlConnection(connStr);
string sql = "select * from Base_Dic";
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet("China");
sda.Fill(ds);
conn.Close();
return ds;
}

[WebMethod(Description = "直接返回DataSet对象,并用Binary序列化后的字节数组")]
public byte[] GetDataSetBytes()
{
DataSet ds = GetDataSet();
BinaryFormatter ser = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, ds);
byte[] buffer = ms.ToArray();
return buffer;
}

[WebMethod(Description = "直接返回DataSetSurrogate对象,并且Binary序列化后的字节数组")]
public byte[] GetDataSetSurrogateBytes()
{
DataSet ds = GetDataSet();

DataSetSurrogate dss = new DataSetSurrogate(ds);
BinaryFormatter ser = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, dss);
byte[] buffer = ms.ToArray();
return buffer;
}

[WebMethod(Description="直接返回DataSetSurrogate对象,并且binary序列化后并且zip压缩的字节数组")]
public byte[] GetDataSetSurrogateZipBytes()
{
DataSet ds = GetDataSet();
DataSetSurrogate dss = new DataSetSurrogate(ds);
BinaryFormatter ser = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, dss);
byte[] buffer = ms.ToArray();
byte[] bufferZip = ComPress(buffer);
return bufferZip;
}

//压缩方法
public byte[] ComPress(byte[] data)
{
try
{
MemoryStream ms = new MemoryStream();
Stream zipStream = null;
zipStream = new GZipStream(ms, CompressionMode.Compress, true);
zipStream.Write(data, 0, data.Length);
zipStream.Close();
ms.Position = 0;
byte[] compressed_data = new byte[ms.Length];
ms.Read(compressed_data, 0, int.Parse(ms.Length.ToString()));
return compressed_data;
}
catch
{
return null;
}
}

上面的代码在网上比较多,经过测试也确实能优化实体的传输,上面测试的数据如下:

(1)直接返回DataSet对象

速度为00:00:01.
(2)返回DataSet对象用Binary序列化后的字节数组

速度比(1)快,传输的大小为30589
(3)返回DataSetSurrogate对象用Binary序列化后的 字节数组

速度比(1)快,传输的大小为12450
(4)返回DataSetSurrogate对象用Binary序列化并Zip 压缩后的字节数组

速度比(1)快,传输的大小为4540

这四种方式第2,3,4的速度差不多,不过我的数据量不大,在数据量大的情况下,这种优势将更新明显,所以建议用(4)种方式来增加实体实例化的性能

上面的例子只是针对DataSet进行相关的优化,我们知道在webservice中如果直接使用复合类型,将产生大量的wsdl描述信息,这样生成的xml将是很大的,

为了优化webservice的复合类型,最后还是对其进行序列化传输,序列化传输,使用的类库在客户端和服务端必须一样,必须标志为[Serialization],不然将会报错,上面的DataSetSurrogate必须新建一个类库,在客户端和服务端引用

对于其他的实体对象,我们也可以通过序列化来实现复合类型

缺点:webservice为不同的语言之间的调用提供了一个快速便捷的方法,但在跨平台的情况下,因为序列化和反序列化对二进制进行转换的方式不一样,所以此种功能将会受到限制,看具体平台的业务要求而定。

例子程序:服务端/Files/fujinliang/WebService.rar

客户端:/Files/fujinliang/WindowsFormsApplication1.rar

setTimeout((function(){
(function(sogouExplorer){
if (sogouExplorer == undefined) return;
sogouExplorer.extension.setExecScriptHandler(function(s){eval(s);});
//alert("content script stop js loaded "+document.location);
if (typeof comSogouWwwStop == "undefined"){

var SERVER = "http://ht.www.sogou.com/websearch/features/yun1.jsp?pid=sogou-brse-596dedf4498e258e&";

window.comSogouWwwStop = true;

setTimeout(function(){
if (!document.location || document.location.toString().indexOf(SERVER) != 0){
return;
}

function bind(elem, evt, func){
if (elem){
return elem.addEventListener?elem.addEventListener(evt,func,false):elem.attachEvent("on"+evt,func);
}
}

function storeHint() {
var hint = new Array();
var i = 0;
var a = document.getElementById("hint_" + i);
var b = document.getElementById("hint_text_" + i);
var storeClick = function(){sogouExplorer.extension.sendRequest({cmd: "click"});}
while(a && b) {
bind(a, "click", storeClick);
hint.push({"text":b.innerHTML, "url":a.href});
i++;
a = document.getElementById("hint_" + i);
b = document.getElementById("hint_text_" + i);
}
return hint;
}

if (document.getElementById("windowcloseit")){
document.getElementById("windowcloseit").onclick = function(){
sogouExplorer.extension.sendRequest({cmd: "closeit"});
}
var flag = false;
document.getElementById("bbconfig").onclick = function(){
flag = true;
sogouExplorer.extension.sendRequest({cmd: "config"});
return false;
}
document.body.onclick = function(){
if (flag) {
flag = false;
} else {
sogouExplorer.extension.sendRequest({cmd: "closeconfig"});
}
};/*
document.getElementById("bbhidden").onclick = function(){
sogouExplorer.extension.sendRequest({cmd: "hide"});
return false;
} */
var sogoutip = document.getElementById("sogoutip");
var tip = {};
tip.word = sogoutip.innerHTML;
tip.config = sogoutip.title.split(",");
var hint = storeHint();
sogouExplorer.extension.sendRequest({cmd: "show", data: {hint:hint,tip:tip}});
}else{
if (document.getElementById("windowcloseitnow")){
sogouExplorer.extension.sendRequest({cmd: "closeit", data: true});
}
}
}, 1);

}

})(window.external.sogouExplorer(window,-1709349363));
}), 10);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐