WCF错误 - 已超出传入邮件的最大邮件大小配额(65536)

时间:2013-04-28 17:39:13

标签: c# wcf configuration-files

我的设置:

  • IIS Express中托管的ASP.NET客户端
  • 在控制台应用程序中托管的WCF服务
  • 以管理员模式运行Visual Studio.NET 2012

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

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

我知道在本网站和其他网站上已经多次询问过这个问题。我已尝试使用CONFIG FILE的各种组合在互联网上发布的几乎所有内容,但这对我来说仍然没有用。

客户端配置:

<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>

服务器配置:

<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>

1 个答案:

答案 0 :(得分:36)

那是因为你没有在服务器上指定使用哪种绑定。我们来看看你的服务器配置:

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

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

这是客户端配置:

<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>

这是服务器配置:

<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>

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

相关问题