纪念Google.cn 正式离我们而去...
前面两篇WCF提高传输性能的文章中提到的两个方法是可以合并在一起使用的。
另外补一下传输DataSet的代码
服务端
private byte[] buffer = null;
private int bufferSize = 10000;
private long stream_length;
private MemoryStream stream;
private void InitDataSet(DataSet ds)
{
IFormatter formatter = new BinaryFormatter();
stream = new MemoryStream();
formatter.Serialize(stream, ds);
stream.Position = 0;
stream_length = stream.Length;
}public byte[] GetBuffer()
{
if (buffer != null)
return buffer;
else
return null;
}
public bool ReadBuffer()
{
bool exist;
if (stream_length > 0)
{
if (stream_length > bufferSize)
{
buffer_currect = new byte[bufferSize];
stream.Read(buffer, 0, bufferSize);
stream_length -= bufferSize;
}
else
{
buffer_currect = new byte[stream_length];
stream.Read(buffer, 0, (int)stream_length);
stream_length = 0;
}
exist = true;
}
else
{
exist = false;
}
return exist;
}
客户端
using (DataClient client = new DataClient())
{
MemoryStream stream = new MemoryStream();
byte[] buffer;
while (client.ReadBuffer())
{
buffer = client.GetBuffer();
stream.Write(buffer, 0, buffer.Length);
}
stream.Position = 0;
IFormatter formatter = new BinaryFormatter();
DataSet ds = (DataSet)formatter.Deserialize(stream);
stream.Close();
client.Close();
return ds;
}