.net 最大数组长度配额

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/464707/
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-09-03 11:03:10  来源:igfitidea点击:

Maximum array length quota

.netwcfwcf-binding

提问by Dav Evans

I'm writing a small WCF/WPF app to resize images but WCF is giving me grief when I try to send an image of size 28K to my service from the client. The service works fine when I send it smaller images. I immediately assumed that this was a configuration issue and I've trawled the web looking at posts regarding the MaxArrayLength property in my binding configuration. Ive upped the limits on these settings on both the client and server to the maximum 2147483647 but still I get the following error:

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

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://mywebsite.com/services/servicecontracts/2009/01:OriginalImage. The InnerException message was 'There was an error deserializing the object of type System.Drawing.Image. The maximum array length quota (16384) has been exceeded while reading XML data. This quota may be increased by changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader.'. Please see InnerException for more details.

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

I've made my client and server configs the same and they look like the following: Server:

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

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

and my client config looks like:

我的客户端配置如下所示:

 <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="[email protected]" />
            </identity>
        </endpoint>
    </client>
</system.serviceModel>

It seems no matter what I set these values to I still get an error saying wcf cannot serialize my file because its greater than 16384. Any ideas?

似乎无论我将这些值设置为什么,我仍然收到一条错误消息,指出 wcf 无法序列化我的文件,因为它大于 16384。有什么想法吗?

Update:the email address in the userPrincipalName tag has been altered for my privacy

更新:为了我的隐私,userPrincipalName 标签中的电子邮件地址已被更改

回答by Dav Evans

My Bad - I forgot to apply my binding configuration to my endpoint in my server-side config. The server config should read:

我的不好 - 我忘记将绑定配置应用到服务器端配置中的端点。服务器配置应为:

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

Note the bindingConfiguration="NetTcpBinding_ImageResizerServiceContract" has been added to the netTcp endpoint. My app now works with larger images. Sweet.

请注意 bindingConfiguration="NetTcpBinding_ImageResizerServiceContract" 已添加到 netTcp 端点。我的应用程序现在可以处理更大的图像。甜的。

回答by Vijay

Please add the <readerQuotas>in binding.

请添加<readerQuotas>in 绑定。

That is the main issue while uploading and downloading the byte[], it solved my problem.

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

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

回答by Unequip

I utilized the Microsoft SDK SvcConfigEditor. You have this if you use Visual Studio (which has its own version). It is also a free download.

我使用了 Microsoft SDK SvcConfigEditor。如果你使用 Visual Studio(它有自己的版本),你就有这个。它也是一个免费下载。

On your hard drive (check both Program Files and Program Files (x86)):

在您的硬盘上(检查 Program Files 和 Program Files (x86)):

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

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

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

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

If you have multiple installs of the Microsoft SDK, what version you use will depend on what version of .NET you are developing under. The tool will throw an error to let you know that you tried to open the .dll file under the wrong version.

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

To use the tool, point to your service's .dll file and let the tool do the heavy lifting. This is particularly helpful if you have services talking to services (proxies services). Also, keep in mind that you'll probably need a config setting for both your client (at the application) and server configs (at the webservice).

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

I was missing a configuration on the server-side endpoint. I had to do two things:

我缺少服务器端端点上的配置。我必须做两件事:

  1. Put an endpoint on my server-side config, configured using SvcConfigEditor
  2. Remember to set the MaxArrayLength in the SvcConfigEditor tool
  1. 将端点放在我的服务器端配置上,使用 SvcConfigEditor 进行配置
  2. 记得在 SvcConfigEditor 工具中设置 MaxArrayLength

Also, ASP.NET was being picky about this value, so you might try simplifying your configuration binding and turning it off:

此外,ASP.NET 对这个值很挑剔,因此您可以尝试简化配置绑定并将其关闭:

  <serviceHostingEnvironment multipleSiteBindingsEnabled="false" />

回答by Goldy Sonata

it,s working, previously showing reference of net.tcp://myservice.com/Ac_Service.svc/mex when browsing wcf service but now it's http://myservice.com/Ac_Service.svc?wsdl

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

will it work in future also, without any problem.

将来是否也能正常工作,没有任何问题。