Silverlight跟WCF进行双工通信,终结点的绑定是采用自定义绑定,当服务发送的数据小于70K的时候,silverlight就能接收到,但是当服务发送的数据大于70k的时候,silverlight客户端就获取不到数据,silverlight异步获取到的错误信息是:服务器未提供有意义的回复;这可能是由协定不匹配、会话过早关闭或内部服务器错误引起的。
WCF的配置为:
<?xml version="1.0"?> <configuration> <!--后台服务配置--> <system.serviceModel> <protocolMapping> <add scheme="tcp" binding="netTcpBinding"/> </protocolMapping> <bindings> <customBinding> <binding name="DemoCustomBinding"> <binaryMessageEncoding maxReadPoolSize="2147483647" maxSessionSize="2147483647" maxWritePoolSize="2147483647"></binaryMessageEncoding> <tcpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"/> </binding> </customBinding> <netTcpBinding> <binding name="DemoTcpBinding" portSharingEnabled="true" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"></binding> </netTcpBinding> </bindings> <services> <service behaviorConfiguration="AirCoolIslandService.AirCoolIslandServiceBehavior" name="AirCoolIslandService.AirCoolIslandService"> <endpoint address="Service" binding="customBinding" contract="AirCoolIslandService.IAirCoolIslandService" bindingConfiguration="DemoCustomBinding"></endpoint> <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/> <host> <baseAddresses> <add baseAddress="http://192.168.0.123:4506"/> <add baseAddress="net.tcp://192.168.0.123:4507"/> </baseAddresses> </host> </service> </services> <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> <behaviors> <serviceBehaviors> <behavior name="AirCoolIslandService.AirCoolIslandServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> <dataContractSerializer maxItemsInObjectGraph="2147483647"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>
Silverlight客户端添加引用后生成的配置文件为:
<configuration> <system.serviceModel> <bindings> <customBinding> <binding name="CustomBinding_IAirCoolIslandService"> <binaryMessageEncoding /> <tcpTransport maxReceivedMessageSize="21147483647" maxBufferSize="2147483647" /> </binding> <binding name="CustomBinding_IAirCoolIslandService1"> <binaryMessageEncoding /> <tcpTransport maxReceivedMessageSize="21147483647" maxBufferSize="2147483647" /> </binding> </customBinding> </bindings> <client> <endpoint address="net.tcp://192.168.1.26:4507/Service" binding="customBinding" bindingConfiguration="CustomBinding_IAirCoolIslandService" contract="ACIServiceClient.IAirCoolIslandService" name="CustomBinding_IAirCoolIslandService" /> <endpoint address="net.tcp://192.168.1.26:4507/Service" binding="customBinding" bindingConfiguration="CustomBinding_IAirCoolIslandService1" contract="ACIService.IAirCoolIslandService" name="CustomBinding_IAirCoolIslandService1" /> </client> </system.serviceModel> </configuration>
silverlight调用WCF的代码:
var endPointAddress = new EndpointAddress(serviceUri); BindingElement bindingElement = new TcpTransportBindingElement(); CustomBinding customBind = new CustomBinding(bindingElement); serviceClient = new ACIService.AirCoolIslandServiceClient(new InstanceContext(this), customBind, endPointAddress); ((IContextChannel)serviceClient.InnerChannel).OperationTimeout = new TimeSpan(0, 0, 240); //调用服务的异步方法,获取历史数据 serviceClient.GetHistoryDataAsync(beginTime, endTime, sb.ToString().TrimEnd(','), RealOrImitate); serviceClient.GetHistoryDataCompleted += new EventHandler<ACIService.GetHistoryDataCompletedEventArgs>(serviceClient_GetHistoryDataCompleted);
我实在搞不明白,这到底是为什么,两端的配置文件我都设置了最大的可接收的数据量,可是为什么还是只能接收这么小的数据呢?
另外,我把客户端配置文件最大的可接收的数据改成很小很小,当我传输几十k的数据却又是可以的,感觉silverlight客户端的配置文件没有起什么作用啊!
小弟是个刚接触WCF不久的菜鸟,恳请各位大哥能指点一下小弟,我将不胜感激!
跟楼主一起等答案。。