WCF:maxStringContentLength 始终设置为 8192 [英] WCF: maxStringContentLength always set to 8192

查看:15
本文介绍了WCF:maxStringContentLength 始终设置为 8192的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 maxStringContentLength 更改为大于 8192 的值,但没有成功.如果它接收的数据量大于 8192 字节,我的 WCF 服务将生成一个异常.我已经用尽了我的搜索,似乎没有任何帮助.我应该指出异常来自服务器.忘记客户端,因为我看到直接从服务器上的 WCF 生成的异常.这是我的 web.config 设置:

I need to change the maxStringContentLength to a value larger than 8192 but have not been successful in doing it. My WCF service will generate an exception if the amount of data it receives is greater than 8192 bytes. I have exhausted my searches and nothing seems to help. I should point out that the exception comes from the server. Forget about the client because I am seeing the exception generated straight from WCF on the server. Here is my web.config settings:

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior name="DevServiceBehavior" >
        <serviceMetadata httpGetEnabled="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <services>
    <service name="DeveloperService"
             behaviorConfiguration="DevServiceBehavior" >
      <endpoint address="mtom"
                binding="basicHttpBinding"
                bindingConfiguration="Binding_DevService"
                contract="DeveloperService">
        <identity>
          <dns value="localhost"/>
        </identity>
      </endpoint>
      <endpoint contract="IMetadataExchange"
                binding="mexHttpBinding"
                address="mex" />
    </service>
  </services>
  <bindings>
    <basicHttpBinding>
      <binding name="Binding_DevService"
               messageEncoding="Mtom"
               openTimeout="00:02:00"
               sendTimeout="00:02:00"
               maxBufferPoolSize ="41943040"
               maxBufferSize="2147483647"
               maxReceivedMessageSize="2147483647">
        <readerQuotas maxDepth="500"
                      maxArrayLength="20000000"
                      maxStringContentLength="20000000" />
      </binding>
    </basicHttpBinding>
  </bindings>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
                              multipleSiteBindingsEnabled="true"/>
</system.serviceModel>

推荐答案

默认情况下,最新版本的 WCF 实际上会设置默认值,而 json 是默认值.不清楚的是 WCF 使用的是哪种默认绑定.原来是webHttpBinding.您还将在 Web 上看到大量示例,这些示例显示了应用于服务方法的属性,例如 [WebGet].该方法根本不需要属性.要使 maxStringContentLength 生效,您需要正确设置绑定和行为.以下是 web.config 文件中的正确条目:

By default, the latest version of WCF does in fact setup defaults and json is the default. What wasn't clear was what kind of default binding WCF was using. It turns out to be webHttpBinding. You will also see a ton of examples on the web showing attributes being applied to the service method, such as [WebGet]. The method requires no attributes at all. For maxStringContentLength to take affect, you need to correctly setup the binding and behavior. Here is the correct entries in the web.config file:

<system.serviceModel>
  <behaviors>
    <endpointBehaviors>
      <behavior name="jsonBehavior">
        <enableWebScript />
      </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
      <behavior name="DevServiceBehavior" >
        <serviceMetadata httpGetEnabled="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <services>
    <service name="DeveloperService" behaviorConfiguration="DevServiceBehavior" >
      <endpoint address="" binding="webHttpBinding" contract="DeveloperService" bindingConfiguration="webHttpBindingDev" behaviorConfiguration="jsonBehavior">
        <identity>
          <dns value="localhost"/>
        </identity>
      </endpoint>
      <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
    </service>
  </services>
  <bindings>
    <webHttpBinding>
      <binding name="webHttpBindingDev">
        <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"  maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      </binding>
    </webHttpBinding>
  </bindings> 
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>

这篇关于WCF:maxStringContentLength 始终设置为 8192的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆