为WCF WebHttp设置最大消息和缓冲区大小

8

我目前有一个使用webHttp绑定的WCF服务,我试图通过在配置文件中覆盖默认设置来增加可以输入到服务中的最大大小。我已经尝试过像下面这样的做法:

  <system.serviceModel>
<bindings>
<webHttpBinding>
  <binding name="webHttp" >
  <security mode="Transport">
      <transport clientCredentialType = 
             "None"
            proxyCredentialType="None"
            realm="string" />
  </security>
  </binding>

</webHttpBinding>
</bindings>
<services>

  <service name="PrimeStreamInfoServices.Service1" behaviorConfiguration="PrimeStreamInfoServices.Service1Behavior">
    <!-- Service Endpoints -->
    <endpoint address="" binding="webHttpBinding"  contract="PrimeStreamInfoServices.IService1">
      <!-- 
          Upon deployment, the following identity element should be removed or replaced to reflect the 
          identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
          automatically.
      -->
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="PrimeStreamInfoServices.Service1Behavior">
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<diagnostics>

我想知道如何更改webHttp绑定的消息大小,我尝试设置与消息大小相关的其他属性,但似乎没有起作用。您有什么建议吗?谢谢!


你能否发布你配置文件的相关部分? - Shiraz Bhaiji
3个回答

13

根据您的设置,有许多可能会产生影响的设置 - 请尝试以下方法:

<bindings>
  <webHttpBinding>
    <binding name="LargeWeb"
             maxBufferPoolSize="1500000"
             maxReceivedMessageSize="1500000"
             maxBufferSize="1500000">
      <readerQuotas 
            maxArrayLength="656000"
            maxBytesPerRead="656000"
            maxDepth="32"
            maxNameTableCharCount="656000"
            maxStringContentLength="656000"
            />
    </binding>
  </webHttpBinding>
</bindings>

通过定义您的“version” webHttpBinding,并将所有这些参数设置为更高的值,您应该能够通过任何消息大小(几乎)。

请注意:这会打开您的系统可能被大量消息淹没并因此降至无法承受负荷的风险(经典的拒绝服务攻击)。这就是这些限制设置得相当低的原因 - 这是故意设计的。

您可以将它们更改为更高的值 - 只要注意您正在做什么以及如果这样做时存在的安全风险!

Marc

PS:为了利用这些设置,您当然必须在服务器和客户端配置中引用该绑定配置:

<client>
  <endpoint address="http://localhost"
            binding="webHttpBinding" bindingConfiguration="LargeWeb"
            contract="IMyService" />
</client>
<services>
  <service>
    <endpoint address="http://localhost"
              binding="webHttpBinding" bindingConfiguration="LargeWeb"
              contract="IMyService" />
  </service>
</services>

这个不行,它不允许我发送大于64k的任何东西。 - Daniel
你能展示一下你的配置吗?你是否引用了这个绑定配置? - marc_s
是的,我意识到这是我的问题了,我刚刚添加了整个配置,但是当我添加bindingConfiguration属性时,出现了异常,原因不明。 - Daniel
“WCF Essentials”是一篇相当不错的介绍文章 - http://www.code-magazine.com/Article.aspx?quickid=0605051 - marc_s
或者你可以看一下Michele Leroux Bustamante的书《学习WCF》- 这本书真正让我开始了解WCF并理解其中的许多细节 - 你可以在亚马逊或其他好的书店找到它。 - marc_s
显示剩余3条评论

1
如果您在模型中使用[DataContract]装饰器,则需要将dataContractSerializer添加到您的web.config中。
例如:
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpsGetEnabled="false"   httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </behavior>
  </serviceBehaviors>

0

为 WCF Rest 服务 webhttpbinding 设置最大消息和缓冲区大小

<bindings>
  <webHttpBinding>
    <binding maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
      <readerQuotas maxDepth="200" maxStringContentLength="83886089" maxArrayLength="163841" maxBytesPerRead="2147483647" maxNameTableCharCount="16384"/>
    </binding>
  </webHttpBinding>
</bindings>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接