在應用中利用Web服務在資料庫中檢索資料產生DataSet對象,並傳輸給用戶端。為了提高效能,使用GZip對DataSet做壓縮,並以DimeAttachment作為附件的方式進行傳輸。我使用的是WSE 2.0 SP3,當傳輸的大資料量時,用戶端報錯,錯誤資訊是“未處理的“Microsoft.Web.Services2.Dime.DimeFormatException”類型的異常出現在 microsoft.web.services2.dll 中。其他資訊: WSE352: The size of the record uuid:db8c7b93-e775-4ebd-9292-17b78f8e79a6 exceed its limit. ”
伺服器代碼如下(通過資料訪問組件訪問資料庫返回DataSet,並把DataSet以GZip壓縮後以DimeAttachment的方式返回給用戶端):
[WebMethod(Description="執行SQL語句或有傳回值的預存程序並返回DataSet,供.net用戶端調用,壓縮並以附件返回以提升效能")]
public void GetDataSet(string logicalName,string commandText,bool isStoreProcedure,string paraList)
{
SoapContext sc = ResponseSoapContext.Current;
if (null == sc)
{
throw new ApplicationException("Only SOAP requests allowed");
}
DataSet ds=DataHelper.ExecuteDataSet(logicalName,commandText,isStoreProcedure,paraList);
MemoryStream memoryStream=new MemoryStream(2048);
GZipOutputStream gzipStream=new GZipOutputStream(memoryStream);
ds.WriteXml(gzipStream);
gzipStream.Finish();
memoryStream.Seek(0, SeekOrigin.Begin);
DimeAttachment dimeAttachment = new DimeAttachment("application/x-gzip",
TypeFormat.MediaType,
memoryStream);
sc.Attachments.Add(dimeAttachment);
}
用戶端調用代碼(檢索WebService傳回來的DimeAttachment,解壓後填充DataSet,並綁定到DataGrid上):
private void getDataSet()
{
ds=new DataSet();
DataAccess.DBServiceWse service=new DALPerformanceCompare.DataAccess.DBServiceWse();
service.Timeout=-1;
commandText=buildCommandText();
this.Cursor=Cursors.WaitCursor;
service.GetDataSet("Compare",commandText,false,null);
SoapContext sc=service.ResponseSoapContext;
GZipInputStream gzipInputStream = new GZipInputStream(sc.Attachments[0].Stream);
MemoryStream ms = new MemoryStream(1024);
int nSize = 2048;
byte[] writeData = new byte[2048];
while (true)
{
nSize = gzipInputStream.Read(writeData, 0, nSize);
if (nSize > 0)
ms.Write(writeData, 0, nSize);
else
break;
}
ms.Seek(0, SeekOrigin.Begin);
ds.ReadXml(ms);
dataGrid1.DataSource=ds.Tables[0];
this.Cursor=Cursors.Default;
}
在WSE的協助檔案裡尋找了一下,發現原來預設的DimeAttachment的大小是4096KB,大於4M的檔案傳輸需要在伺服器端的Web.config檔案與用戶端的app.config檔案做訊息大小的設定,在設定檔中加入下列節:
<configuration>
<microsoft.web.services2>
<messaging>
<maxRequestLength>128000</maxRequestLength>
</messaging>
</microsoft.web.services2>
</configuration>
重新編譯後運行程式,正常返回大資料量的DataSet。