我正在使用pollingDuplex绑定来实现Silverlight Web应用程序和我的WCF Web服务之间的通信。直到我尝试将大量数据从Web应用程序以xmlString的形式发送到Web服务,它才能正常工作。然后我收到了错误消息:
“格式化程序在尝试反序列化消息时抛出异常:对操作'SendUserSelection'反序列化请求消息体时出错。读取XML数据时已超出最大字符串内容长度配额(8192)。通过更改创建XML阅读器时使用的XmlDictionaryReaderQuotas对象的MaxStringContentLength属性,可以增加配额。“
我发现为了增加MaxStringContentLength属性,我必须将pollingDuplex绑定转换为自定义绑定(http://blogs.msdn.com/b/silverlightws/archive/2010/04/04/some-known-wcf-issues-in-silverlight-4.aspx)。我的问题是我该怎么做?
我在web服务的web.config文件中定义的pollingDuplex绑定看起来如下:
<pollingDuplex>
<binding name="myPollingDuplex" closeTimeout="00:10:00" openTimeout="00:10:00"
receiveTimeout="00:10:00" sendTimeout="00:10:00" duplexMode="MultipleMessagesPerPoll" />
终点:
<endpoint address="" binding="pollingDuplex" bindingConfiguration="myPollingDuplex" contract="WebApplication.Web.MainWS"/>
Web应用程序端用于实例化Web服务客户端的代码:
this.client = new MainWSRef.MainWSClient(new PollingDuplexHttpBinding { DuplexMode = PollingDuplexMode.MultipleMessagesPerPoll },
new EndpointAddress("http://localhost:1981/MainWS.svc"));
我尝试了以下内容:
<customBinding>
<binding name="myPollingDuplex" closeTimeout="00:10:00" openTimeout="00:10:00"
receiveTimeout="00:10:00" sendTimeout="00:10:00">
<pollingDuplex duplexMode="MultipleMessagesPerPoll">
</pollingDuplex>
<textMessageEncoding>
<readerQuotas maxDepth="32" maxStringContentLength="5242880"
maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</textMessageEncoding>
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</customBinding>
端点:
<endpoint address="" binding="customBinding" bindingConfiguration="myPollingDuplex" contract="WebApplication.Web.MainWS"/>
Web应用程序端的代码:
CustomBinding binding = new CustomBinding(new PollingDuplexBindingElement(), new BinaryMessageEncodingBindingElement(), new HttpTransportBindingElement());
this.client = new MainWSRef.MainWSClient(binding, new EndpointAddress("http://localhost:1981/MainWS.svc"));
当我尝试运行代码时,我收到以下错误消息:
“远程服务器返回错误:NotFound。”
我做错了吗?我很感激任何建议。
答案 0 :(得分:0)
当我指定ReceiveTimeout="02:00:00"
时,我收到同样的错误,没有它,它正在工作。
我试着找出如何在没有错误的情况下设置ReveiveTimeout。
更新: 我认为它的工作,那就是我的服务器web.config:
<customBinding>
<binding name="SLDuplexService" receiveTimeout="02:00:00">
<pollingDuplex duplexMode="MultipleMessagesPerPoll"
maxPendingSessions="2147483647" maxPendingMessagesPerSession="2147483647" maxOutputDelay="00:00:05"
inactivityTimeout="02:00:00" />
<binaryMessageEncoding/>
<httpTransport transferMode="StreamedResponse"/>
</binding>
</customBinding>
请注意,receiveTimeout是绑定的属性,而inactivityTimeout是pollingDuplex的属性。如果您在10分钟后不想要出现故障通道,则必须设置两个超时。
您还必须在客户端上指定超时,这就是我的代码:
PollingDuplexHttpBinding binding = new PollingDuplexHttpBinding(PollingDuplexHttpSecurityMode.None, PollingDuplexMode.MultipleMessagesPerPoll);
binding.InactivityTimeout = new TimeSpan(2,0,0);
binding.ReceiveTimeout = new TimeSpan(2, 0, 0);
_client = new SLDuplexServiceClient(binding, new EndpointAddress("http://localhost/LpSystem.ServiceInterface.Web/SLDuplexService/SLDuplexService.svc"));