服务器代码如下(通过数据访问组件访问数据库返回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);
}
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;
}
{
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,大于
<configuration>
<microsoft.web.services2>
<messaging>
<maxRequestLength>128000</maxRequestLength>
</messaging>
</microsoft.web.services2>
</configuration>
<microsoft.web.services2>
<messaging>
<maxRequestLength>128000</maxRequestLength>
</messaging>
</microsoft.web.services2>
</configuration>
重新编译后运行程序,正常返回大数据量的DataSet。