惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
V
V2EX
博客园_首页
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
H
Help Net Security
A
About on SuperTechFans
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
W
WeLiveSecurity
云风的 BLOG
云风的 BLOG
D
Docker
Security Archives - TechRepublic
Security Archives - TechRepublic
Help Net Security
Help Net Security
N
News and Events Feed by Topic
Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
A
Arctic Wolf
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
博客园 - 三生石上(FineUI控件)
aimingoo的专栏
aimingoo的专栏
Hacker News: Ask HN
Hacker News: Ask HN
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy International News Feed
T
Troy Hunt's Blog
T
Tenable Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Recorded Future
Recorded Future
F
Fortinet All Blogs
D
DataBreaches.Net
B
Blog
T
Threat Research - Cisco Blogs
MyScale Blog
MyScale Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
The GitHub Blog
The GitHub Blog
Security Latest
Security Latest
M
MIT News - Artificial intelligence

博客园 - 夏狼哉

.Net framework 的浏览器定义文件 Windows按名称排序问题 IE的文档模式, 及Textarea呈现bug一例 SilverLight与WCF服务双工通讯第一篇:PollingDuplexHttpBinding SilverLight通过Net.TCP(NetTCPBinding)方式调用WCF服务 WCF与Asp.Net Web Application的深度整合方法 C#与Outlook交互收发邮件 分布式版本控制系统Mercurial(二):web server的架设 分布式版本控制系统Mercurial(一):Mercurial基本功能介绍 C#将汉字转换为拼音首字母 nbsp空格 VS2010 targetFramework 错误 在网络上共享条码打印机 WCF身份验证之二:使用MessageHeader进行验证 WCF身份验证之一:使用证书进行验证 Windows CE 5.0 Emulator [条码打印]使用斑马语言(ZPL)打印汉字 C#打印条码与ZPL 应用程序在网络上通信的实现(C#)
SilverLight与WCF服务双工通讯第二篇:Net.Tcp binding
夏狼哉 · 2012-12-28 · via 博客园 - 夏狼哉

如果还不知道如何让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的双工应该是实践中的首选.