我们目前正在尝试使用带有PollingDuplex的WCF将大量数据移动到Silverlight 3客户端。我已经阅读了Silverlight 4中的MultiplerMessagesPerPoll,它看起来要快得多。是否有任何示例供我参考(使用MultipleMessagesPerPoll)?或者可能是一些关于使用Net.TCP的好参考?也许我应该采取完全不同的方法?任何想法或建议将不胜感激。
谢谢!
答案 0 :(得分:2)
流式序列化响应块效果很好:
您的WCF绑定配置类似于以下内容:
<binding name="myCustomBinding">
<binaryMessageEncoding />
<httpTransport transferMode="StreamedResponse"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647" />
</binding>
您的服务方法如下所示:
[OperationContract]
public Stream GetDataStream(string objectId)
{
Stream stream = new MemoryStream();
MyObject obj = Manager.GetObject(objectId);
DataContractSerializer serilizer = new DataContractSerializer(typeof(MyObject));
serilizer.WriteObject(stream, obj);
stream.Position = 0;
return stream;
}
您的客户端已完成方法将执行以下操作:
static void client_GetDataStreamCompleted(object sender, GetDataStreamCompletedEventArgs e)
{
if (e.Error == null)
{
DataContractSerializer serializer = new DataContractSerializer(typeof(MyObject));
MyObject obj = serializer.ReadObject(new MemoryStream(e.Result)) as MyObject;
}
}
答案 1 :(得分:1)
我实施了上面建议的解决方案。实施后,我找到了这个链接:
http://msdn.microsoft.com/en-us/library/ms752244.aspx
然后我实现了二进制编写器,如下所示。
服务方式:
[OperationContract]
public Stream GetAllLocationsDataStream(string customerId)
{
Stream stream = new MemoryStream();
try
{
Customer customer = ServiceEquipmentManager.GetCustomerAllLocations(customerId);
DataContractSerializer serializer = new DataContractSerializer(typeof(Customer));
XmlDictionaryWriter binaryDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(stream);
serializer.WriteObject(binaryDictionaryWriter, customer);
binaryDictionaryWriter.Flush();
}
catch (Exception ex)
{
string timestamp;
ExceptionHelper.HandleExceptionWrapper(ex, "Log Only", out timestamp);
}
stream.Position = 0;
return stream;
}
客户端已完成事件:
XmlDictionaryReader binaryDictionaryReader = XmlDictionaryReader.CreateBinaryReader(new MemoryStream(e.Argument as byte[]), XmlDictionaryReaderQuotas.Max);
Customer customer = serializer.ReadObject(binaryDictionaryReader) as Customer;
我检查了我的对象的差异,如上面的链接所示,我的结果显示如下:
文字= 68,866,216字节
二进制= 49,207,475字节(比文本少28.5%)
答案 2 :(得分:0)
我基于你的答案,并强调你要传输的数据的大小。如果要将数据块保持在4GB以下,则可以使用System.IO.Compression命名空间中的GZipStream class。根据我使用纯文本的经验,它将数据流减少到原始大小的17-20%。