zoukankan      html  css  js  c++  java
  • WCF传输大数据的设置

    在从客户端向WCF服务端传送较大数据(>65535B)的时候,发现程序直接从Reference的BeginInvoke跳到EndInvoke,没有进入服务端的Service实际逻辑中,怀疑是由于数据过大超出限定导致的。

    报错信息:远程服务器返回了意外响应: (400) Bad Request。

    问题是我实际发送的数据是刚刚从WCF服务端接收过来的,一来一去,数据量差别并不大。

    然后发现,在客户端和服务端实际使用的是不同的配置,对于客户端,在添加ServiceReference时自动生成的ServiceReferences.ClientConfig文件中system.serviceModel节下有这样的设置:

    <bindings>    <basicHttpBinding>        <binding name="BasicHttpBinding_WcfService" maxBufferSize="2147483647"            maxReceivedMessageSize="2147483647">            <security mode="None" />        </binding>    </basicHttpBinding></bindings>

    然后在Client节里应用Binding Configuration:

    <client>            <endpoint address="http://localhost:22000/Service/WcfService.svc"                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_WcfService"                contract="WcfServiceReference.WcfService" name="BasicHttpBinding_WcfService" />
    </client>

    在Binding里指定了最大缓存字节数和最大接受字节数,相当于2G的大小!除非传一整套连续剧,一般是够用了。

    而在服务端,Web.config文件里,Bindings节是空的,而Service也没有指定bindingConfiguration属性,那么它们采用的就是默认的65535的大小。

    问题找到,解决就比较容易了:

    在Bindings节添加新的Binding设置,指定最大接受数据:

    <bindings>    <basicHttpBinding>        <binding name="LargeDataTransferServicesBinding" maxReceivedMessageSize="2147483647"  messageEncoding="Text" transferMode="Streamed" sendTimeout="00:10:00" />    </basicHttpBinding></bindings>
    之后给相应的Service指定bindingConfiguration属性:
    <service behaviorConfiguration="Server.Service.WcfServiceBehavior"  name="Server.Service.WcfService">  <endpoint address="" binding="basicHttpBinding" bindingConfiguration="LargeDataTransferServicesBinding" contract="Server.Service.WcfService" />  <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /></service>

    这样就可以从客户端发送足够大的数据了。

    P.S.:

    .net默认只能传4M的文件,所以尽管设定了Wcf两端的配置,还是超不出.net的限定,所以如果要传输大文件,还需要在System.Web节下加上

        <httpRuntimemaxRequestLength="102400" />

    这里的单位是KB,这样就可以传100M的文件了。当然,这么大的文件,最好还是分段传输比较好。

  • 相关阅读:
    strspn实现 查找第一个不在指定字符串中出现的字符
    strstr 的实现 即 字符串中查找子字符串第一次出现的位置
    strcspn
    将字符串中的大写字母变成小写字母
    strrchr 字符在字符串中最后一次出现的位置
    strchr 字符c在字符串中出现的位置 实现
    qsort库函数 排序
    strncmp实现
    strcmp实现
    ACM/ICPC题目输入情形及其处理方法
  • 原文地址:https://www.cnblogs.com/soundcode/p/1911832.html
Copyright © 2011-2022 走看看