【问题标题】:How to change default message size using ClearUserNameBinding?如何使用 ClearUserNameBinding 更改默认消息大小?
【发布时间】:2014-09-26 14:52:23
【问题描述】:

我们在 WCF 服务中使用 ClearUserNameBindig

当我们尝试返回超过 3k 条记录的消息时,我们收到了以下错误:

已超出传入邮件的最大邮件大小配额 (65536)。要增加配额,请在适当的绑定元素上使用 MaxReceivedMessageSize 属性。

我们尝试像这样修改 web.config:

<bindings>
  <clearUsernameBinding>
    <binding name="myClearUsernameBinding"
              maxReceivedMessageSize="20000000"
              maxBufferSize="20000000"
              maxBufferPoolSize="20000000" />
    <readerQuotas maxDepth="32"
              maxArrayLength="200000000"
            maxStringContentLength="200000000"/>
  </clearUsernameBinding>
</bindings>

但是我们收到了这个错误:

无法识别的属性“maxReceivedMessageSize”。

如何使用 ClearUserNameBinding 更改默认消息大小?

【问题讨论】:

  • 反对者,¿你能解释一下原因吗?

标签: wcf wcf-binding


【解决方案1】:

我们按照以下步骤找到了解决方案:

http://sureshjakka.blogspot.com.ar/2010/03/changing-message-sizes-in.html

我们修改ClearUserNameBinding的代码如下:

  1. AutoSecuredHttpTransportElement() 构造函数中,将值初始化为可能的最大值

    public AutoSecuredHttpTransportElement()
    {
        MaxReceivedMessageSize = int.MaxValue;
        MaxBufferSize = int.MaxValue;
        MaxBufferPoolSize = long.MaxValue;
    }
    
  2. CreateBindingElements() 方法中创建 XMLDictionaryReaderQutotas 对象并在TextMessageEncodingBindingElement 上设置相同的对象。这是此方法的修改版本。

    public override BindingElementCollection CreateBindingElements()
    {
        XmlDictionaryReaderQuotas rqMax = XmlDictionaryReaderQuotas.Max;
        TextMessageEncodingBindingElement textBE = new TextMessageEncodingBindingElement();
        textBE.MessageVersion = this.messageVersion;
    
        rqMax.CopyTo(textBE.ReaderQuotas);
    
        var res = new BindingElementCollection();
        res.Add(textBE); 
        res.Add(SecurityBindingElement.CreateUserNameOverTransportBindingElement());
        res.Add(new AutoSecuredHttpTransportElement());
        return res;
    }
    

注意:请确保您的参考资料中有“System.Runtime.Serialization”版本 3.0.0.0 及更高版本。因为如果你有 2.0.0.0 版本,你会得到编译错误,因为这个版本不允许在 ReaderQuotas 上设置属性。

Web.config:

<bindings>
  <clearUsernameBinding>
    <binding name="myClearUsernameBinding" />
  </clearUsernameBinding>
</bindings>

最后我们更新服务端和客户端的引用。

【讨论】:

    猜你喜欢
    • 2013-01-29
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    相关资源
    最近更新 更多