首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
Web开发
> 详细
怎样提高WebService的性能
时间:
2015-02-04 09:20:41
阅读:
138
评论:
0
收藏:
0
[点我收藏+]
标签:
服务器端WebService程序:
C#代码
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.IO.Compression;
using System.Data.SqlClient;
………
public
class Service1 : System.Web.Services.WebService
{
[WebMethod(Description =
"直接返回 DataSet 对象。")]
public DataSet GetNorthwindDataSet()
{
string sql =
"SELECT * FROM XT_TEXT";
SqlConnection conn =
new SqlConnection(
"Server=60.28.25.58;DataBase=s168593;user id=s168593;password=h0y+FeC*;");
conn.Open();
SqlDataAdapter dataadapter =
new SqlDataAdapter(sql, conn);
DataSet ds =
new DataSet();
dataadapter.Fill(ds,
"XT_TEXT");
conn.Close();
return ds;
}
[WebMethod(Description =
"返回 DataSet 对象用 Binary 序列化后的字节数组。")]
public
byte[] GetDataSetBytes()
{
DataSet dataSet = GetNorthwindDataSet();
BinaryFormatter ser =
new BinaryFormatter();
MemoryStream ms =
new MemoryStream();
ser.Serialize(ms, dataSet);
byte[] buffer = ms.ToArray();
return buffer;
}
[WebMethod(Description =
"返回 DataSetSurrogate 对象用 Binary 序列化后的字节数组。")]
public
byte[] GetDataSetSurrogateBytes()
{
DataSet dataSet = GetNorthwindDataSet();
DataSetSurrogate dss =
new DataSetSurrogate(dataSet);
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 dataSet = GetNorthwindDataSet();
DataSetSurrogate dss =
new DataSetSurrogate(dataSet);
BinaryFormatter ser =
new BinaryFormatter();
MemoryStream ms =
new MemoryStream();
ser.Serialize(ms, dss);
byte[] buffer = ms.ToArray();
byte[] zipBuffer = Compress(buffer);
return zipBuffer;
}
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;
}
}
}
客户端WebService程序
[code =
"C#"]
private
void button1_Click(
object sender, EventArgs e)
{
com.dzbsoft.www.Service1 ds =
new com.dzbsoft.www.Service1();
DateTime dtBegin = DateTime.Now;
DataSet dataSet = ds.GetNorthwindDataSet();
this.label1.Text =
string.Format(
"耗时:{0}", DateTime.Now - dtBegin);
binddata(dataSet);
}
private
void button2_Click(
object sender, EventArgs e)
{
com.dzbsoft.www.Service1 ds =
new com.dzbsoft.www.Service1();
DateTime dtBegin = DateTime.Now;
byte[] buffer = ds.GetDataSetBytes();
BinaryFormatter ser =
new BinaryFormatter();
DataSet dataSet = ser.Deserialize(
new MemoryStream(buffer))
as DataSet;
this.label2.Text =
string.Format(
"耗时:{0}", DateTime.Now - dtBegin) +
" " + buffer.Length;
binddata(dataSet);
}
private
void button3_Click(
object sender, EventArgs e)
{
com.dzbsoft.www.Service1 ds =
new com.dzbsoft.www.Service1();
DateTime dtBegin = DateTime.Now;
byte[] buffer = ds.GetDataSetSurrogateBytes();
BinaryFormatter ser =
new BinaryFormatter();
DataSetSurrogate dss = ser.Deserialize(
new MemoryStream(buffer))
as DataSetSurrogate;
DataSet dataSet = dss.ConvertToDataSet();
this.label3.Text =
string.Format(
"耗时:{0}", DateTime.Now - dtBegin) +
" " + buffer.Length;
binddata(dataSet);
}
private
void button4_Click(
object sender, EventArgs e)
{
com.dzbsoft.www.Service1 ds =
new com.dzbsoft.www.Service1();
DateTime dtBegin = DateTime.Now;
byte[] zipBuffer = ds.GetDataSetSurrogateZipBytes();
byte[] buffer = UnZipClass.Decompress(zipBuffer);
BinaryFormatter ser =
new BinaryFormatter();
DataSetSurrogate dss = ser.Deserialize(
new MemoryStream(buffer))
as DataSetSurrogate;
DataSet dataSet = dss.ConvertToDataSet();
this.label4.Text =
string.Format(
"耗时:{0}", DateTime.Now - dtBegin) +
" " + zipBuffer.Length;
binddata(dataSet);
}
private
void binddata(DataSet dataSet)
{
this.dataGridView1.DataSource = dataSet.Tables[0];
this.label5.Text =
"共计:" + dataSet.Tables[0].Rows.Count +
"条记录";
}
客户端UnZipClass程序
[code =
"C#"]
public
static
class UnZipClass
{
public
static
byte[] Decompress(
byte[] data)
{
try
{
MemoryStream ms =
new MemoryStream(data);
Stream zipStream =
null;
zipStream =
new GZipStream(ms, CompressionMode.Decompress);
byte[] dc_data =
null;
dc_data = ExtractBytesFromStream(zipStream, data.Length);
return dc_data;
}
catch
{
return
null;
}
}
public
static
byte[] ExtractBytesFromStream(Stream zipStream,
int dataBlock)
{
byte[] data =
null;
int totalBytesRead = 0;
try
{
while (
true)
{
Array.Resize(
ref data, totalBytesRead + dataBlock + 1);
int bytesRead = zipStream.Read(data, totalBytesRead, dataBlock);
if (bytesRead == 0)
{
break;
}
totalBytesRead += bytesRead;
}
Array.Resize(
ref data, totalBytesRead);
return data;
}
catch
{
return
null;
}
}
}
怎样提高WebService的性能
标签:
原文地址:http://www.cnblogs.com/ranran/p/4271515.html
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
36.VUE — 认识 Webpack 和 安装
2021-07-28
【PHP】上传图片翻转问题
2021-07-28
php对数字进行万。亿的转化
2021-07-28
五个 .NET 性能小贴士
2021-07-28
Three.js中显示坐标轴、平面、球体、四方体
2021-07-28
.net 5+ 知新:【1】 .Net 5 基本概念和开发环境搭建
2021-07-27
1.html,css
2021-07-27
基于Docker搭建 Php-fpm + Nginx 环境
2021-07-27
nginx + http + svn
2021-07-27
kubernets kube-proxy的代理 iptables和ipvs
2021-07-26
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!