• WCF 第六章 序列化和编码 大数据流


    WCF支持两种消息处理模式: 缓冲和流模式。缓冲是WCF中处理消息的默认模式。在这个模式下,整个消息在发送和接收之前被放入内存中。在大多数场景,缓冲消息是重要的而且有时需要支 持一些诸如可信赖消息和数字签名的特性。然而,缓冲大消息将很容易导致系统资源耗尽并限制可扩展性。WCF支持另外一种使用流处理消息的模式。在这个模式 中,在客户端和服务端的数据使用一个System.IO.Stream.Streaming。流模式一般在一个绑定或一个传输信道上使用。列表6.29 显示了如何在绑定配置中通过设置transferMode属性在netTcpBinding绑定打开流处理。transferMode属性的可以使用的值 有Buffer,Streamed,SteamResponse和StreamRequest.这允许在客户端和服务端之间细粒度的流控制。

    列表6.29 在netTcpBinding上允许流

    01<?xml version="1.0" encoding="utf-8" ?>
    02<configuration>
    03    <system.serviceModel>
    04        <client>
    05            <endpoint address="net.tcp://localhost/FileDownloadService" binding="netTcpBinding"
    06                bindingConfiguration="EnableStreamingOnNetTcp" contract="Contract.IFileDownload"
    07                name="FileDownload_netTcpEndpoint">
    08               <span style="color: #ff0000;"> <identity>
    09                    <userPrincipalName value="administrator@localhost.com" />
    10                </identity>
    11</span>            </endpoint>
    12        </client>
    13        <bindings>
    14            <netTcpBinding>
    15                <binding name="EnableStreamingOnNetTcp" transferMode="Streamed"
    16                    maxReceivedMessageSize="<span style="color: #ff0000;">2147483647</span>">
    17                    <<span style="color: #ff0000;">security mode="None">
    18</span>                        <transport clientCredentialType="Windows">
    19                            <extendedProtectionPolicy policyEnforcement="Never" />
    20                        </transport>
    21                    </security>
    22                </binding>
    23            </netTcpBinding>
    24        </bindings>
    25        <services />
    26    </system.serviceModel>
    27</configuration>

      为了充分利用流的优势,操作契约需要使用一个System.IO.Stream的实例或者返回一个使用流的消息契约。列表6.30 显示了一个返回一个System.IO.Stream的文件下载服务契约的例子。

    列表6.30 FileDownload 服务契约

    01using System.ServiceModel;
    02using System.IO;
    03 
    04namespace Contract
    05{
    06    [ServiceContract]
    07    public interface IFileDownload
    08    {
    09        [OperationContract]
    10        Stream GetFile(string fileName);
    11    }
    12}

      当大量数据使用时流不能在所有场景中工作。例如,如果在失败后需要可信赖消息,数字签名或者恢复,流方式是不能接受的。在这些场景,手动将数据切分成小块消息然后发送很多小块消息,最终由接收方对小块消息重组的方法是首选。这可以很容易的在WCF上层应用中使用。


    =======
    转载自
    作者:DanielWise
    出处:http://www.cnblogs.com/danielWise/
     

  • 相关阅读:
    Django——基于类的视图源码分析 三
    python——深刻理解Python中的元类(metaclass)
    Django——静态文件配置
    Django——如何使用Template以及如何向template传递变量
    Django—— 缓存框架
    Django——META内部类选项
    Django——20141014深入理解Django HttpRequest HttpResponse的类和实例
    Django——如何在Django模板中注入全局变量?——part2
    Mysql查找如何判断字段是否包含某个字符串
    Mysql分表和分区的区别
  • 原文地址:https://www.cnblogs.com/llbofchina/p/2094160.html
Copyright © 2020-2023  润新知