【问题标题】:Custom Message Encoder in WCF with support for ReaderQuotasWCF 中的自定义消息编码器,支持 ReaderQuotas
【发布时间】:2012-04-20 11:54:04
【问题描述】:

在这里找到答案(最后一篇):http://social.msdn.microsoft.com/Forums/eu/wcf/thread/f5c0ea22-1d45-484e-b2c0-e3bc9de20915

我的自定义 (TextOrMtomEncoder) 的实现遇到了最后一个问题,即 ReaderQuotas 的实现。

我在网上搜索了很多,但我无法找出最后的拼图。

I've got a class,其中包含我对“BindingElementExtensionElement”和“MessageEncodingBindingElement”的实现。

MessageEncodingBindingElement 实现包含以下内容的覆盖:

T GetProperty<T>(BindingContext context)

我从默认的 .NET MessageEncoding 实现中“借用”like the TextMessageEncoding

这必须是正确的实现,because MSDN says so

配置从 web.config 加载正常,我可以看到我的两个类中的 ReaderQuotas 属性设置正确,但看起来 .NET 没有从我的 MessageEncodingBindingElement 实现中读取 ReaderQuotas 配置。

我的猜测是 .NET 使用 GetProperty 方法来加载配置,因为 MessageVersion 是通过此方法请求的。但问题是,T 永远不会等于 XmlDictionaryReaderQuotas,所以 ReaderQuotas 永远不会开始请求。

我的问题的根源是奇怪的顺便说一句,我正在使用 IIS7.5 的 Windows 7 x64 机器上开发。在我的机器上发布“大”文件(如 100 KB)。但是当我将服务部署到 Windows Server 2008 R2(尝试了 2 台不同的服务器)时,出现以下错误:

格式化程序在尝试反序列化 消息:尝试反序列化参数时出错 http://socialproxy.infocaster.net:argument。内部异常消息 is '反序列化类型对象时出错 系统对象。最大数组长度配额 (16384) 已 读取 XML 数据时超出。此配额可能会增加 更改 XmlDictionaryReaderQuotas 上的 MaxArrayLength 属性 创建 XML 阅读器时使用的对象。第 1 行,位置 1584.'。 有关详细信息,请参阅 InnerException。

就像我说的,它可以在我的机器上运行:-/

谁能告诉我如何解决这个问题?

非常感谢!

WCF 服务配置:

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

【问题讨论】:

    标签: wcf mtom custom-binding readerquotas


    【解决方案1】:

    在这里找到答案(最后一篇):http://social.msdn.microsoft.com/Forums/eu/wcf/thread/f5c0ea22-1d45-484e-b2c0-e3bc9de20915

    好的,我找到了。您必须将 readerquotes 传递给标准编码器。不幸的是,这里没有构造函数,所以你必须设置属性。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-03
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      相关资源
      最近更新 更多