【问题标题】:WCF Error because The maximum message size quota for incoming messages (65536) has been exceededWCF 错误,因为已超出传入邮件 (65536) 的最大邮件大小配额
【发布时间】:2014-10-06 00:33:57
【问题描述】:

我在服务器端创建 WCF 服务以返回文件的二进制(字节).. 我只能返回 txt 文件的二进制文件,否则会显示“已超出传入消息的最大消息大小配额 (65536)。要增加配额,请在适当的绑定元素上使用 MaxReceivedMessageSize 属性。”

<bindings>
      <basicHttpBinding>
        <binding name="basicHttp" allowCookies="true"
                 maxReceivedMessageSize="2147483647" 
                 maxBufferSize="2147483647"
                 maxBufferPoolSize="2147483647"
          transferMode="Buffered"
          messageEncoding="Text"
          textEncoding="utf-8"
          bypassProxyOnLocal="false"
               useDefaultWebProxy="true" >
        <security mode="None" />
            <readerQuotas maxDepth="2147483647" 
                 maxArrayLength="2147483647"
                 maxStringContentLength="2147483647"
        maxBytesPerRead="2147483647"
        maxNameTableCharCount="2147483647"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

你能给我解决办法吗?

我需要你的帮助..谢谢之前:)

这里是客户端代码端(app.config)

<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IService" />
    </basicHttpBinding>
  </bindings>
  <client>
    <endpoint address="http://localhost:3724/Service.svc" 
              binding="basicHttpBinding"
              bindingConfiguration="BasicHttpBinding_IService"
              contract="ServiceReference1.IService"
              name="BasicHttpBinding_IService" />
  </client>

此客户端是桌面应用程序而不是网站

【问题讨论】:

标签: asp.net wcf-data-services


【解决方案1】:

您需要将您定义的绑定分配给您创建的端点。这是一个例子:

<bindings>
  <basicHttpBinding>
    <binding name="basicHttp" allowCookies="true"
             maxReceivedMessageSize="2147483647" 
             maxBufferSize="2147483647"
             maxBufferPoolSize="2147483647"
             transferMode="Buffered"
             messageEncoding="Text"
             textEncoding="utf-8"
             bypassProxyOnLocal="false"
             useDefaultWebProxy="true" >
      <security mode="None" />
      <readerQuotas maxDepth="2147483647" 
                    maxArrayLength="2147483647"
                    maxStringContentLength="2147483647"
                    maxBytesPerRead="2147483647"
                    maxNameTableCharCount="2147483647"/>
    </binding>
  </basicHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<services> 
  <service> 
    <endpoint address="/Service.svc" binding="basicHttpBinding" 
              bindingCongifuration="basicHttp" 
              contract="service.IService"/> 
  </service> 
</services>

有几点需要注意 - 在您的评论中,您的服务端点使用的是 wsHttpBinding,但您在 &lt;bindings&gt; 部分中定义了 basicHttpBinding。此外,bindingConfiguration 属性必须是分配给已定义绑定的名称。我上面的例子是基于原始发布的配置文件。

或者,如果您使用的是 .NET 4.0 或更高版本,您可以在 &lt;bindings&gt; 设置中定义绑定,并通过省略 &lt;binding&gt; 元素上的 name 属性将其设置为该绑定的默认定义。

默认情况下,.NET 4.0+ 将basicHttpBinding 用于http。您可以在&lt;protocolMapping&gt; 部分(包含在&lt;system.serviceModel&gt; 部分中)的配置文件中更改此设置。例如,如果您想对所有 http 请求使用 wsHttpBinding,您可以这样做:

<protocolMapping>
  <add binding="wsHttpBinding" scheme="http" />
</protocolMapping>

添加客户端示例

客户端看起来非常相似,只是您有一个&lt;clients&gt; 部分而不是&lt;services&gt; 部分。请注意,绑定上的某些设置适用于应用程序所在的机器(客户端或服务器),并且在客户端-服务器关系的一侧设置它们对另一侧没有影响(如maxReceivedMessageSize,对于例子)。通常绑定类型和安全性必须一致,并且在实践中通常更容易在两端使用相同的绑定,但可能会有一侧具有不同缓冲区或其他项的情况。

<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IService" allowCookies="true"
             maxReceivedMessageSize="2147483647" 
             maxBufferSize="2147483647"
             maxBufferPoolSize="2147483647"
             transferMode="Buffered"
             messageEncoding="Text"
             textEncoding="utf-8"
             bypassProxyOnLocal="false"
             useDefaultWebProxy="true" >
      <security mode="None" />
      <readerQuotas maxDepth="2147483647" 
                    maxArrayLength="2147483647"
                    maxStringContentLength="2147483647"
                    maxBytesPerRead="2147483647"
                    maxNameTableCharCount="2147483647"/>
    </binding>
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="localhost:3724/Service.svc" 
            binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService" 
            contract="ServiceReference1.IService" 
            name="BasicHttpBinding_IService" /> 
</client>

【讨论】:

  • 我使用 .NET 4.0+ 并编写我的代码作为您的示例.. 我不知道为什么,但它不起作用.. 谢谢蒂姆,很抱歉给您带来困扰:)
  • @PeterYohanes - 我刚刚重读了你的问题。错误是否出现在客户端(即,当您从服务器获取数据时)?如果是这样,您需要在客户端的配置中进行更改,而不是在服务器中进行更改。如果是这种情况,请在您的原始问题中发布您的客户端配置的&lt;system.serviceModel&gt; 部分?我去看看。
  • 很抱歉回复晚了,不仅在客户端出现错误,而且如果在服务器上调用 WCF 也是错误的......你能给我任何想法吗?
  • @PeterYohanes - 您在客户端中定义的绑定没有设置任何值,因此您将获得默认值。您需要让绑定值与服务器上的值匹配,然后在客户端中也使用该绑定配置。与我上面的示例非常相似,但端点位于 &lt;client&gt; 部分。
  • 在我的客户端,我设置了 localhost:3724/Service.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference1.IService" name="BasicHttpBinding_IService" /> 然后我将服务器上的所有绑定复制粘贴到客户端?
猜你喜欢
  • 2018-01-15
  • 2015-09-15
  • 2015-06-13
  • 2013-04-22
  • 2014-09-13
  • 2012-03-13
  • 2014-02-13
  • 2011-02-23
相关资源
最近更新 更多