控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

怎样提高WebService的性能

作者:佚名 出处:互联网 2011年11月16日 阅读:

怎样提高WebService的性能

服务器端WebService程序
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 DB";
        SqlConnection conn = new SqlConnection("Server=60.28.25.58;DataBase=s168593;user id=s168593;password=*****;");
        conn.Open();
        SqlDataAdapter dataadapter = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        dataadapter.Fill(ds, "DB");
        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程序

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程序
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;
        }
    }
}
 

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统