介绍
Silverlight 2.0 调用 WCF 的双向通信服务(Duplex Service) 。开发一个服务端主动向客服端发送股票信息的程序,首先客户端先向服务端发送需要监控的股票的股票代码,然后服务端在该股信息发生变化的时候将信息推送到客户端。
    服务端:
        定义服务契约及回调接口
        从当前上下文获取回调的客户端信道
        需要的话则向客户端信道“推”消息
    客户端:
        构造 PollingDuplexHttpBinding 并在其上创建 IDuplexSessionChannel 的信道工厂
        异步方式打开信道工厂
        异步方式打开信道
        构造需要发送到服务端的消息 System.ServiceModel.Channels.Message
        异步向服务端发送消息
        监听指定信道,用于异步方式接收服务端返回的消息
        不需要再接收服务端的消息则关闭信道


在线DEMO
http://www.cnblogs.com/webabcd/archive/2008/10/09/1307486.html


示例
服务端:
IDuplexService.cs
2.0通信之调用WCF的双向通信(Duplex Service)using System;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Collections.Generic;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Linq;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Runtime.Serialization;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.ServiceModel;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Text;
2.0通信之调用WCF的双向通信(Duplex Service)
2.0通信之调用WCF的双向通信(Duplex Service)
using System.ServiceModel.Channels;
2.0通信之调用WCF的双向通信(Duplex Service)

DuplexService.cs
2.0通信之调用WCF的双向通信(Duplex Service)using System;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Collections.Generic;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Linq;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Runtime.Serialization;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.ServiceModel;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Text;
2.0通信之调用WCF的双向通信(Duplex Service)
2.0通信之调用WCF的双向通信(Duplex Service)
using System.ServiceModel.Channels;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Threading;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.ServiceModel.Activation;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.IO;
2.0通信之调用WCF的双向通信(Duplex Service)
}

PollingDuplexServiceHostFactory.cs
2.0通信之调用WCF的双向通信(Duplex Service)using System;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Collections.Generic;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Linq;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Web;
2.0通信之调用WCF的双向通信(Duplex Service)
2.0通信之调用WCF的双向通信(Duplex Service)
using System.ServiceModel;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.ServiceModel.Channels;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.ServiceModel.Activation;
2.0通信之调用WCF的双向通信(Duplex Service)

DuplexService.svc
%>


客户端:
DuplexService.xaml
2.0通信之调用WCF的双向通信(Duplex Service)<UserControl x:Class="Silverlight20.Communication.DuplexService"
2.0通信之调用WCF的双向通信(Duplex Service)    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
2.0通信之调用WCF的双向通信(Duplex Service)    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml">
2.0通信之调用WCF的双向通信(Duplex Service)    
<StackPanel HorizontalAlignment="Left" Margin="5">
2.0通信之调用WCF的双向通信(Duplex Service)    
2.0通信之调用WCF的双向通信(Duplex Service)        
<TextBox x:Name="txtStockCode" Text="请输入股票代码" Margin="5" />
2.0通信之调用WCF的双向通信(Duplex Service)        
<Button x:Name="btnSubmit" Content="获取股票信息" Click="btnSubmit_Click" Margin="5" />
2.0通信之调用WCF的双向通信(Duplex Service)        
<Button x:Name="btnStop" Content="停止获取" Click="btnStop_Click"  Margin="5" />
2.0通信之调用WCF的双向通信(Duplex Service)        
<TextBlock x:Name="lblStockMessage" Margin="5" />
2.0通信之调用WCF的双向通信(Duplex Service)    
2.0通信之调用WCF的双向通信(Duplex Service)    
</StackPanel>
2.0通信之调用WCF的双向通信(Duplex Service)
</UserControl>
2.0通信之调用WCF的双向通信(Duplex Service)

DuplexService.xaml.cs
2.0通信之调用WCF的双向通信(Duplex Service)using System;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Collections.Generic;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Linq;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Net;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Windows;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Windows.Controls;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Windows.Documents;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Windows.Input;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Windows.Media;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Windows.Media.Animation;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Windows.Shapes;
2.0通信之调用WCF的双向通信(Duplex Service)
2.0通信之调用WCF的双向通信(Duplex Service)
using System.ServiceModel;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.ServiceModel.Channels;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.Threading;
2.0通信之调用WCF的双向通信(Duplex Service)
using System.IO;
2.0通信之调用WCF的双向通信(Duplex Service)
2.0通信之调用WCF的双向通信(Duplex Service)
namespace Silverlight20.Communication
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-22
  • 2022-12-23
  • 2022-02-02
  • 2022-12-23
猜你喜欢
  • 2022-03-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-06
  • 2021-12-18
相关资源
相似解决方案