【问题标题】:How to transfer large amount of data using WCF?如何使用 WCF 传输大量数据?
【发布时间】:2011-02-15 01:22:37
【问题描述】:

我们目前正在尝试使用带有 PollingDuplex 的 WCF 将大量数据移动到 Silverlight 3 客户端。我已经阅读了 Silverlight 4 中的 MultiplerMessagesPerPoll ,它似乎要快得多。是否有任何示例供我参考(使用 MultipleMessagesPerPoll)?或者也许有一些关于使用 Net.TCP 的好的参考资料?也许我应该采取完全不同的方法?任何想法或建议将不胜感激。

谢谢!

【问题讨论】:

    标签: silverlight silverlight-4.0 wcf-binding


    【解决方案1】:

    流式序列化响应块效果很好:

    您的 WCF 绑定配置类似于以下内容:

    <binding name="myCustomBinding">
       <binaryMessageEncoding />
       <httpTransport transferMode="StreamedResponse" 
                      maxBufferSize="2147483647" 
                      maxBufferPoolSize="2147483647" 
                      maxReceivedMessageSize="2147483647" />
    </binding>
    

    您的 Service 方法如下所示:

    [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;
       }
    }
    

    【讨论】:

      【解决方案2】:

      我实施了上述建议的解决方案。实现后,我找到了这个链接:

      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%)

      【讨论】:

      • 谢谢,该解决方案对我有用!简单而且非常快!之前我几乎无法加载 ~300 个项目,现在我正在加载 ~4000 个项目!在更短的时间内!
      【解决方案3】:

      我是根据您的回答及其对您正在传输的数据大小的强调。如果您将传输的数据块保持在 4GB 以下,则可以使用 System.IO.Compression 命名空间中的GZipStream class。根据我使用纯文本的经验,它将数据流减少到其原始大小的 17-20% 左右。

      【讨论】:

      • 感谢奥斯汀!我一定会看看这个!
      猜你喜欢
      • 2012-11-22
      • 2012-04-29
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 1970-01-01
      • 2011-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多