Question

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:

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.

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="me@domain.com" />
            </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?

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

Was it helpful?

Solution

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.

OTHER TIPS

Please add the <readerQuotas> in binding.

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>

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.

On your hard drive (check both Program Files and 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

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.

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

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

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

  <serviceHostingEnvironment multipleSiteBindingsEnabled="false" />

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

will it work in future also, without any problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top