none
Silverlight调用WCF RRS feed

  • 问题

  • 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不久的菜鸟,恳请各位大哥能指点一下小弟,我将不胜感激!

    2013年5月8日 14:17

答案

  • 你好,

    >>我把客户端配置文件最大的可接收的数据改成很小很小,当我传输几十k的数据却又是可以的,感觉silverlight客户端的配置文件没有起什么作用啊!

    从你的配置看似乎是没有问题的。但是配置文件里已经配置了终结点调用服务的时候就不用再new CustomBinding了, 这里有可能客户端用的是你后来指定的那个绑定,而不是配置文件里的那个。

    另外,为服务终结点使用netTcpBinding(DemoTcpBinding)传输数据有问题么?

    如果问题仍然存在,建议你开启Tracing来获得更详细的异常信息, 并借助SvcTraceViewer.exe分析日志。

    http://msdn.microsoft.com/en-us/library/ms733025.aspx


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年5月10日 9:15
    版主