22

我的设置:

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

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

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

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

客户端配置:

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

1 回答 1

38

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

<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在您的客户端上获取正确的配置。

于 2013-04-28T18:36:00.330 回答