怎样提高WebService的性能(打印)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
怎样提高WebService的性能
服务器端WebService程序
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using pression;
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, press, 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();
bel1.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;
bel2.Text = string.Format("耗时:{0}", DateTime.Now - dtBegin) + " " + buffer.Length;