最大阵列长度配额

时间:2009-01-21 10:19:21

标签: .net wcf wcf-binding

我正在编写一个小型WCF / WPF应用程序来调整图像大小,但当我尝试从客户端向我的服务发送大小为28K的图像时,WCF让我感到悲伤。当我发送较小的图像时,该服务工作正常。我立即认为这是一个配置问题,我在网上搜索了关于绑定配置中MaxArrayLength属性的帖子。我已将客户端和服务器上这些设置的限制提高到最大值2147483647,但仍然出现以下错误:

  

格式化程序在尝试反序列化消息时抛出异常:尝试反序列化时出错   参数http://mywebsite.com/services/servicecontracts/2009/01:OriginalImage。   InnerException消息是'反序列化System.Drawing.Image类型的对象时出错。   读取XML数据时已超出最大数组长度配额(16384)。可以通过更改创建XML阅读器时使用的XmlDictionaryReaderQuotas对象上的MaxArrayLength属性来增加此配额。有关详细信息,请参阅InnerException。

我使我的客户端和服务器配置相同,它们如下所示: 服务器:

<system.serviceModel>
    <bindings>
        <netTcpBinding>
            <binding name="NetTcpBinding_ImageResizerServiceContract" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
                hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="10"
                maxReceivedMessageSize="2147483647">
                <readerQuotas maxDepth="32"
                              maxStringContentLength="2147483647"
                              maxArrayLength="2147483647"
                              maxBytesPerRead="2147483647"
                              maxNameTableCharCount="2147483647" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
                <security mode="Transport">
                    <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                    <message clientCredentialType="Windows" />
                </security>
            </binding>
        </netTcpBinding>
    </bindings>
    <behaviors>
        <serviceBehaviors>
            <behavior name="ServiceBehavior">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
        <service name="LogoResizer.WCF.ServiceTypes.ImageResizerService" behaviorConfiguration="ServiceBehavior">
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:900/mex/"/>
                    <add baseAddress="net.tcp://localhost:9000/" />
                </baseAddresses>
            </host>
            <endpoint binding="netTcpBinding" contract="LogoResizer.WCF.ServiceContracts.IImageResizerService" />
            <endpoint  address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
</system.serviceModel>

我的客户端配置如下:

 <system.serviceModel>
    <bindings>
        <netTcpBinding>
            <binding name="NetTcpBinding_ImageResizerServiceContract" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
                hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="10"
                maxReceivedMessageSize="2147483647">
                <readerQuotas maxDepth="32" 
                              maxStringContentLength="2147483647"
                              maxArrayLength="2147483647" 
                              maxBytesPerRead="2147483647" 
                              maxNameTableCharCount="2147483647" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
                <security mode="Transport">
                    <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                    <message clientCredentialType="Windows" />
                </security>
            </binding>
        </netTcpBinding>
    </bindings>
    <client>
        <endpoint address="net.tcp://localhost:9000/" binding="netTcpBinding"
            bindingConfiguration="NetTcpBinding_ImageResizerServiceContract"
            contract="ImageResizerService.ImageResizerServiceContract"
            name="NetTcpBinding_ImageResizerServiceContract">
            <identity>
                <userPrincipalName value="me@domain.com" />
            </identity>
        </endpoint>
    </client>
</system.serviceModel>

似乎无论我将这些值设置为什么,我仍然会收到错误,说wcf无法序列化我的文件,因为它大于16384.有什么想法吗?

更新:userPrincipalName标记中的电子邮件地址已根据我的隐私进行了更改

4 个答案:

答案 0 :(得分:38)

我的错 - 我忘了在我的服务器端配置中将绑定配置应用到我的端点。服务器配置应为:

<system.serviceModel>
    <bindings>
        <netTcpBinding>
            <binding name="NetTcpBinding_ImageResizerServiceContract" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
                hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="10"
                maxReceivedMessageSize="2147483647">
                <readerQuotas maxDepth="2147483647"
                              maxStringContentLength="2147483647"
                              maxArrayLength="2147483647"
                              maxBytesPerRead="2147483647"
                              maxNameTableCharCount="2147483647" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
                <security mode="Transport">
                    <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                    <message clientCredentialType="Windows" />
                </security>
            </binding>

        </netTcpBinding>
    </bindings>
    <behaviors>
        <serviceBehaviors>
            <behavior name="ServiceBehavior">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
        <service name="LogoResizer.WCF.ServiceTypes.ImageResizerService" behaviorConfiguration="ServiceBehavior">
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:900/mex/"/>
                    <add baseAddress="net.tcp://localhost:9000/" />
                </baseAddresses>
            </host>
            <endpoint bindingConfiguration="NetTcpBinding_ImageResizerServiceContract" binding="netTcpBinding" contract="LogoResizer.WCF.ServiceContracts.IImageResizerService" />
            <endpoint  address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
</system.serviceModel>

请注意,bindingConfiguration =“NetTcpBinding_ImageResizerServiceContract”已添加到netTcp端点。我的应用现在适用于更大的图像。甜。

答案 1 :(得分:6)

请在绑定中添加<readerQuotas>

这是上传和下载byte []时的主要问题,它解决了我的问题。

<basicHttpBinding>
    <binding name="ServicesBinding" transferMode="Streamed" maxBufferSize="200000000"
        maxReceivedMessageSize="200000000" messageEncoding="Text"  
        receiveTimeout="00:10:00">
        <readerQuotas maxDepth="2147483647"
            maxStringContentLength="2147483647"
            maxArrayLength="2147483647"
            maxBytesPerRead="2147483647"
            maxNameTableCharCount="2147483647" />
    </binding>
</basicHttpBinding>

答案 2 :(得分:2)

我使用了Microsoft SDK SvcConfigEditor。如果您使用Visual Studio(具有自己的版本),则可以使用此选项。它也是免费下载。

在硬盘上(检查程序文件和程序文件(x86)):

  

C:\ Program Files(x86)\ Microsoft SDKs \ Windows \ v7.0A \ Bin \ NETFX 4.0   工具\ SvcConfigEditor.exe

     

C:\ Program Files \ Microsoft SDKs \ Windows \ v7.1 \ Bin \ NETFX 4.0   工具\ SvcConfigEditor.exe

如果您安装了多个Microsoft SDK,那么您使用的版本取决于您正在开发的.NET版本。该工具将抛出一个错误,让您知道您尝试在错误的版本下打开.dll文件。

要使用该工具,请指向服务的.dll文件,让工具完成繁重的工作。如果您有与服务(代理服务)交谈的服务,这将特别有用。另外,请记住,您可能需要为客户端(在应用程序中)和服务器配置(在Web服务上)配置设置。

我在服务器端端点缺少配置。我不得不做两件事:

  1. 将端点放在我的服务器端配置上,使用SvcConfigEditor配置
  2. 请记住在SvcConfigEditor工具中设置MaxArrayLength
  3. 此外,ASP.NET对此值很挑剔,因此您可以尝试简化配置绑定并将其关闭:

      <serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
    

答案 3 :(得分:-2)

它工作,以前在浏览wcf服务时显示net.tcp://myservice.com/Ac_Service.svc/mex的参考但是现在它是 http://myservice.com/Ac_Service.svc?wsdl

将来也会有用,没有任何问题。