zoukankan      html  css  js  c++  java
  • SilverLight与WCF服务双工通讯第二篇:Net.Tcp binding

    如果还不知道如何让silverLight通过net.tcp管理与WCF服务通讯, 请先阅读 SilverLight通过Net.TCP(NetTCPBinding)方式调用WCF服务.

    一. 建立项目

    这里我建立了一个新的演示项目叫做SLTCPDuplexSample和SLTCPDuplexSample.Web, 前面的流程和《SilverLight通过Net.TCP(NetTCPBinding)方式调用WCF服务》 一文是完全相同的.

    二. 服务端配置

    在SLTCPDuplexSample.Web中新建一个名为service1.svc的WCF服务, 其实它的代码和上一篇SilverLight与WCF服务双工通讯第一篇:PollingDuplexHttpBinding 完全相同, 这里不再重复列出, 关于IService1.cs和Service1.svc.cs, 请参阅上一篇中的代码.

    将web.config中的<system.serviceModel>节按如下配置:

      <system.serviceModel>
    
        <bindings>
          <netTcpBinding>
            <binding name="tcpBindingConfig1">
              <security mode="None"></security>
            </binding>
          </netTcpBinding>
        </bindings>
    
        <services>
          <service name="SLTCPDuplexSample.Web.Service1">
            <endpoint address=""
                      binding="netTcpBinding"
                      bindingConfiguration="tcpBindingConfig1"
                      contract="SLTCPDuplexSample.Web.IService1">
            </endpoint>
            <endpoint address="mex"
                      binding="mexTcpBinding"
                      contract="IMetadataExchange">
            </endpoint>
            <host>
              <baseAddresses>
                <add baseAddress="net.tcp://localhost:4502/SLTCPDuplexSample.Web/Service1.svc"/>
              </baseAddresses>
            </host>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="">
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
            multipleSiteBindingsEnabled="true" />
      </system.serviceModel>

    发布, 并将其添加到IIS中. 关于IIS中的配置, SilverLight通过Net.TCP(NetTCPBinding)方式调用WCF服务 一文已经非常详细的列举, 请参阅上文配置IIS.

    三. SL端的配置

    在SL项目中添加服务引用, 将自动生成相应的ServiceReferences.ClientConfig, 由于net.tcp是由.net framework内置支持的, 所以不需要额外做任何的引用.

    在SL的MainPage中放一个名为txt1的文本框和一个名为btn1的按钮, 在btn的点击事件中:

            private void btn1_Click_1(object sender, RoutedEventArgs e)
            {
                var proxy = new ServiceReference1.Service1Client();
                proxy.PushMessageReceived += proxy_PushMessageReceived;
                proxy.RegisterClientAsync();
            }
    
            void proxy_PushMessageReceived(object sender, ServiceReference1.PushMessageReceivedEventArgs e)
            {
                txt1.Text = e.message;
            }

    运行, 点击btn1, 即可看到服务端推送的时间数据.

    结语: 本文写得比较简单, 因为实际上跟pollingDuplex模式相比, 代码部分基本上是完全相同的, 所不同的只有web.config配置. tcp双工跟pollingDuplex相比, 既不需要额外引用 dll, 也拥有更高得多的性能, 所以基于tcp的双工应该是实践中的首选.

    ---------------------------------------------

    作者:夏狼哉
    博客:http://www.cnblogs.com/Moosdau

    如需引用,敬请保留作者信息,谢谢

  • 相关阅读:
    浅谈Huffman树
    CF884D:Boxes And Balls
    MySQL单表查询(重要)
    MySQL字段完整性约束(重要)
    MySQL数据类型(重要)
    数据库基本操作
    MySQL权限管理
    MySQL存储引擎概述
    数据库基础
    并发编程小结
  • 原文地址:https://www.cnblogs.com/Moosdau/p/2837999.html
Copyright © 2011-2022 走看看