如何使用ClearUserNameBinding更改默认消息大小?
我们在我们的WCF服务中使用ClearUserNameBindig。如何使用ClearUserNameBinding更改默认消息大小?
当我们试图用3K多条记录返回的消息,我们收到此错误:
The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.
我们试图修改web.config中这样的:
<bindings>
<clearUsernameBinding>
<binding name="myClearUsernameBinding"
maxReceivedMessageSize="20000000"
maxBufferSize="20000000"
maxBufferPoolSize="20000000" />
<readerQuotas maxDepth="32"
maxArrayLength="200000000"
maxStringContentLength="200000000"/>
</clearUsernameBinding>
</bindings>
但是我们收到的这款错误:
Unrecognized attribute 'maxReceivedMessageSize'.
如何使用Clea更改默认消息大小rUserNameBinding?
我们发现以下这个步骤的解决方案:
http://sureshjakka.blogspot.com.ar/2010/03/changing-message-sizes-in.html
我们修改ClearUserNameBinding
这样的代码:
-
在
AutoSecuredHttpTransportElement()
构造函数中,初始化值,以最大可能public AutoSecuredHttpTransportElement() { MaxReceivedMessageSize = int.MaxValue; MaxBufferSize = int.MaxValue; MaxBufferPoolSize = long.MaxValue; }
-
在
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>
最后我们更新服务器和客户端的引用。
Downvoter,¿你能解释为什么吗? – 2016-02-05 20:46:58