一直觉得SL中的wcf双工通讯方式有点鸡肋,如果是以http方式实现则效率太低,如果用SL4中的tcp方式实现,又跟socket太雷同,所以一直没去研究,不过这东西在对性能要求不高时(比如在网页上每5分钟更新一次天气预报/股票信息),实现起来还是蛮方便的.


wcf双工通讯与传统的wcf相比,最大的区别就是:传统的wcf通常都是客户端去调服务,即客户端从服务端上“拉”信息,而双工通讯除了允许客户端从服务端"拉"信息外,服务端还能主动向客户端“推”送信息。


当然这种实现是有性能消耗的,服务端将保存一条"回调通道"以便能利用该通道把信息推到客户端,而客户端也要有相应的回调函数来处理--有点类似"ajax中的长链接"以及"服务器推"技术



sl3的官方文档http://msdn.microsoft.com/zh-cn/library/dd470106(VS.95).aspx中有一个客户端把订单发送到服务端,然后由服务端处理后,再把结果回推到客户端的例子,不过sdk中有二个地方没有注明细节,可能会误导大家调试失败。


1.服务端的web.config中配置bindingExtensions时,如果按照官方的配置写法:


<extensions>

      <bindingExtensions>

        <add name="pollingDuplexHttpBinding"

   type="System.ServiceModel.Configuration.PollingDuplexHttpBindingCollectionElement, System.ServiceModel.PollingDuplex, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

      </bindingExtensions>

    </extensions>


浏览svc将提示程序集加载失败,如果出现这种情况,请将上面的“, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”去掉,即改成


<extensions>

            <bindingExtensions>

                <add name="pollingDuplexHttpBinding" type="System.ServiceModel.Configuration.PollingDuplexHttpBindingCollectionElement, System.ServiceModel.PollingDuplex"/>

            </bindingExtensions>

        </extensions>


另外,如果生成后,发现bin目录下没有System.ServiceModel.PollingDuplex.dll,可以手动复制一份到bin目录中



2.客户端调试时,如果出现无法访问客户端,请检查服务端下有无策略文件clientaccesspolicy.xml,参考内容如下:


<?xml version="1.0" encoding="utf-8"?>

<access-policy>

  <cross-domain-access>

    <policy>

      <allow-from http-request-headers="SOAPAction">

        <domain uri="*"/>

      </allow-from>

      <grant-to>

        <resource path="/" include-subpaths="true"/>

      </grant-to>

    </policy>

  </cross-domain-access>

</access-policy>


源代码下载:DuplexWcf_SL.rar (vs2010编辑的,用vs2008打开可能需要手动修改一些地方)


作者:菩提树下的杨过