C# WCF 错误 - 已超出传入消息的最大消息大小配额 (65536)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16265725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:35:39  来源:igfitidea点击:

WCF Error - The maximum message size quota for incoming messages (65536) has been exceeded

c#wcfconfiguration-files

提问by Yuvi Dagar

My Setup:

我的设置:

  • ASP.NET client hosted in IIS Express
  • WCF Service hosted in Console Application
  • Running Visual Studio.NET 2012 in Admin mode
  • IIS Express 中托管的 ASP.NET 客户端
  • 托管在控制台应用程序中的 WCF 服务
  • 在管理员模式下运行 Visual Studio.NET 2012

I am trying to return 2 List objects from a WCF service. My setup WORKS FINE when I return just 1 List objects. But when I return 2 List objects I get the error:

我正在尝试从 WCF 服务返回 2 个 List 对象。当我只返回 1 个 List 对象时,我的设置工作正常。但是当我返回 2 个 List 对象时,我收到错误消息:

The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.

已超出传入邮件的最大邮件大小配额 (65536)。要增加配额,请在适当的绑定元素上使用 MaxReceivedMessageSize 属性。

I know that this question has been asked many times on this site and other sites as well. I have tried almost everything posted on the internet with various combinations of the CONFIG FILE but still this has not worked out for me.

我知道这个问题已经在这个网站和其他网站上被问过很多次了。我已经尝试了几乎所有发布在互联网上的配置文件的各种组合,但这对我来说仍然没有奏效。

Client Config:

客户端配置:

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <httpRuntime maxRequestLength="1572864"/>
    </system.web>

    <system.serviceModel>
        <client>
            <endpoint name="basicHttpBinding"
                address="http://localhost:9502/HCDataService"
                binding="basicHttpBinding"
                bindingConfiguration="basicHttpBinding"                
                contract="HC.APP.Common.ServiceContract.IHCServiceContract"
                behaviorConfiguration="ServiceBehaviour">
            </endpoint>
        </client>

        <bindings>
            <basicHttpBinding>
                <binding name="basicHttpBinding" maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647">
                    <readerQuotas maxDepth="128" maxStringContentLength="2147483647" maxArrayLength="2147483646" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                </binding>
            </basicHttpBinding>
        </bindings>

        <behaviors>
            <endpointBehaviors>
                <behavior name="ServiceBehaviour">
                    <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
                </behavior>
            </endpointBehaviors>
        </behaviors>
    </system.serviceModel>
</configuration>

Server Config:

服务器配置:

<configuration>
    <system.serviceModel>
        <services>
            <service name="HC.APP.Server.Service.HCDataService" behaviorConfiguration="ServiceBehaviour">
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:9502/HCDataService"/>
                    </baseAddresses>
                </host>

                <endpoint name="basicHttpBinding"
                    address="http://localhost:9502/HCDataService"
                    binding="basicHttpBinding"
                    bindingConfiguration="basicHttpBinding"
                    contract="HC.APP.Common.ServiceContract.IHCServiceContract">
                </endpoint>
            </service>
        </services>

        <bindings>
            <basicHttpBinding>
                <binding name="basicHttpBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" closeTimeout="01:50:00" openTimeout="01:50:00" sendTimeout="01:50:00" receiveTimeout="01:50:00" >
                    <readerQuotas maxDepth="128" maxStringContentLength="8388608" maxArrayLength="2147483646" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                </binding>
            </basicHttpBinding>
        </bindings>

        <behaviors>
            <serviceBehaviors>
                <behavior name="ServiceBehaviour">
                    <serviceDebug includeExceptionDetailInFaults="true" />
                    <serviceMetadata httpGetEnabled="true" />
                    <dataContractSerializer ignoreExtensionDataObject="false" maxItemsInObjectGraph="2147483646" />
                </behavior>
            </serviceBehaviors>
        </behaviors>    
    </system.serviceModel>
</configuration>

采纳答案by Artless

That would be because you didn't specify on the server which binding to use. Let's take a look at your server config:

那是因为您没有在服务器上指定要使用的绑定。让我们看一下您的服务器配置:

Under <bindings>you are creating a binding configuration for <basicHttpBinding>, and you are naming it name="basicHttpBinding". Also, your endpoint name is <endpoint name="basicHttpBinding" ...>and its binding is binding="basicHttpBinding". However, it's not referring to your binding configuration, but to the binding type. So, it's actually using the default settings for basicHttpBinding.

<bindings>您创建一个绑定配置之下<basicHttpBinding>,您正在为它命名name="basicHttpBinding"。此外,您的端点名称是<endpoint name="basicHttpBinding" ...>,其绑定是binding="basicHttpBinding". 但是,它不是指您的绑定配置,而是指绑定类型。因此,它实际上是使用basicHttpBinding.

To fix this, first of all name your endpoint and binding configuration differently. For example, <endpoint name="basicEndpoint" ... >and <binding name="myBasicBinding" ... >. Then you need to assign your binding configuration to your endpoint with this attribute: bindingConfiguration="myBasicBinding".

要解决此问题,首先要以不同的方式命名您的端点和绑定配置。例如,<endpoint name="basicEndpoint" ... ><binding name="myBasicBinding" ... >。然后,你需要你的绑定配置与此属性分配给您的端点:bindingConfiguration="myBasicBinding"

Here's the client config:

这是客户端配置:

<system.web>
    <httpRuntime maxRequestLength="32768"/>
</system.web>

<system.serviceModel>
    <client>
        <endpoint name="basicEndpoint"
            address="http://localhost:9502/HCDataService"
            binding="basicHttpBinding"
            bindingConfiguration="myBasicBinding"
            contract="HC.APP.Common.ServiceContract.IHCServiceContract"
            behaviorConfiguration="ServiceBehaviour">
        </endpoint>
    </client>

    <bindings>
        <basicHttpBinding>
            <binding name="myBasicBinding" maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647">
                <readerQuotas maxDepth="128" maxStringContentLength="2147483647" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
            </binding>
        </basicHttpBinding>
    </bindings>

    <behaviors>
        <endpointBehaviors>
            <behavior name="ServiceBehaviour">
                <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
            </behavior>
        </endpointBehaviors>
    </behaviors>    
</system.serviceModel>

Here's the server config:

这是服务器配置:

<system.serviceModel>
    <services>
        <service name="HC.APP.Server.Service.HCDataService" behaviorConfiguration="ServiceBehaviour">
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:9502/HCDataService"/>
                </baseAddresses>
            </host>

            <endpoint name="basicEndpoint"
                address="http://localhost:9502/HCDataService"
                binding="basicHttpBinding"
                bindingConfiguration="myBasicBinding"
                contract="HC.APP.Common.ServiceContract.IHCServiceContract">
            </endpoint>
        </service>
    </services>

    <bindings>
        <basicHttpBinding>
            <binding name="myBasicBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" closeTimeout="01:50:00" openTimeout="01:50:00" sendTimeout="01:50:00" receiveTimeout="01:50:00" >
                <readerQuotas maxDepth="128" maxStringContentLength="8388608" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
            </binding>
        </basicHttpBinding>
    </bindings>

    <behaviors>
        <serviceBehaviors>
            <behavior name="ServiceBehaviour">
                <serviceDebug includeExceptionDetailInFaults="true" />
                <serviceMetadata httpGetEnabled="true" />
                <dataContractSerializer ignoreExtensionDataObject="false" maxItemsInObjectGraph="2147483646" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

Don't forget to update service referenceon your client to get the correct config.

不要忘记update service reference在您的客户端上获得正确的配置。