Custom Message Encoder in WCF with support for ReaderQuotas

4k views Asked by At

Found the answer here (last post): http://social.msdn.microsoft.com/Forums/eu/wcf/thread/f5c0ea22-1d45-484e-b2c0-e3bc9de20915

I'm having one last issue with the implementation of my custom (TextOrMtomEncoder), which is the implementation of ReaderQuotas.

I've searched the web a lot, but I can't figure out the final piece of the puzzle.

I've got a class, which contains my implementations of the 'BindingElementExtensionElement' and 'MessageEncodingBindingElement'.

The MessageEncodingBindingElement implementation contains an override for:

T GetProperty<T>(BindingContext context)

which I 'borrowed' from the default .NET MessageEncoding implementations, like the TextMessageEncoding.

This has to be the right implementation, because MSDN says so.

The configuration is loaded fine from the web.config, I can see the ReaderQuotas properties in both my classes are set correctly, but it looks like .NET isn't reading the ReaderQuotas config from my MessageEncodingBindingElement implementation.

My guess is .NET uses the GetProperty method to load the config, because MessageVersion is requested via this method. But the problem is, T is never equal to XmlDictionaryReaderQuotas, so the ReaderQuotas is never begin requested.

The root of my question is weird btw, I'm developing on a Windows 7 x64 machine with IIS7.5. Posting 'large' files (like 100 KB) works on my machine. But when I deploy the service to Windows Server 2008 R2 (tried 2 different servers), 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://socialproxy.infocaster.net:argument. The InnerException message was 'There was an error deserializing the object of type System.Object. 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. Line 1, position 1584.'. Please see InnerException for more details.

And like I said, it works on my machine :-/

Could anybody tell me how I can resolve this?

Many thanks in advance!

The WCF service config:

<system.serviceModel>
    <extensions>
        <behaviorExtensions>
            <add name="wsdlExtensions" type="WCFExtras.Wsdl.WsdlExtensionsConfig, WCFExtras, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
            <add name="textOrMtomMessageBehavior" type="InfoCaster.SocialProxy.lib.TextOrMtom.TextOrMtomMessageBehavior, InfoCaster.SocialProxy" />
        </behaviorExtensions>
        <bindingElementExtensions>
            <add name="textOrMtomEncoding" type="InfoCaster.SocialProxy.lib.TextOrMtom.TextOrMtomEncodingElement, InfoCaster.SocialProxy" />
        </bindingElementExtensions>
    </extensions>
    <bindings>
        <customBinding>
            <binding name="TextOrMtomBinding">
                <textOrMtomEncoding messageVersion="Soap11">
                    <readerQuotas maxDepth="32" maxStringContentLength="5242880" maxArrayLength="204800000" maxBytesPerRead="5242880" maxNameTableCharCount="5242880" />
                </textOrMtomEncoding>
                <httpTransport maxBufferSize="5242880" maxReceivedMessageSize="5242880" transferMode="Buffered" authenticationScheme="Anonymous" />
            </binding>
        </customBinding>
    </bindings>
    <services>
        <clear />
        <service name="InfoCaster.SocialProxy.SocialProxy" behaviorConfiguration="InfoCaster.SocialProxy.ISocialProxyServiceBehavior">
            <endpoint name="SocialProxyServiceEndpoint" address="" binding="customBinding" bindingConfiguration="TextOrMtomBinding" contract="InfoCaster.SocialProxy.ISocialProxy" behaviorConfiguration="InfoCaster.SocialProxy.ISocialProxyEndpointBehavior" />
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <clear />
            <behavior name="InfoCaster.SocialProxy.ISocialProxyServiceBehavior">
                <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                <serviceMetadata httpGetEnabled="true" />
                <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <clear />
            <behavior name="InfoCaster.SocialProxy.ISocialProxyEndpointBehavior">
                <textOrMtomMessageBehavior />
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
1

There are 1 answers

0
kipusoep On BEST ANSWER

Found the answer here (last post): http://social.msdn.microsoft.com/Forums/eu/wcf/thread/f5c0ea22-1d45-484e-b2c0-e3bc9de20915

Ok, I found it. You have to pass the readerquotes to the standard encoders. Unfortunantly there's no constructor for this so you have to set the property.

class TextOrMtomEncoder : MessageEncoder   {
    MessageEncoder _textEncoder;
    MessageEncoder _mtomEncoder;
    public TextOrMtomEncoder(MessageVersion messageVersion, XmlDictionaryReaderQuotas readerQuotas)
    {
      TextMessageEncodingBindingElement textEncoderBindingElement = new TextMessageEncodingBindingElement(messageVersion, Encoding.UTF8);
      MtomMessageEncodingBindingElement mtomEncoderBindingElement = new MtomMessageEncodingBindingElement(messageVersion, Encoding.UTF8);

      readerQuotas.CopyTo(mtomEncoderBindingElement.ReaderQuotas);
      readerQuotas.CopyTo(textEncoderBindingElement.ReaderQuotas);

      _mtomEncoder = mtomEncoderBindingElement.CreateMessageEncoderFactory().Encoder;
      _textEncoder = textEncoderBindingElement.CreateMessageEncoderFactory().Encoder;
    }