客户端:
DuplexService.xaml
<UserControl x:Class="Silverlight20.Communication.DuplexService"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <StackPanel HorizontalAlignment="Left" Margin="5">
        
                <TextBox x:Name="txtStockCode" Text="请输入股票代码" Margin="5" />
                <Button x:Name="btnSubmit" Content="获取股票信息" Click="btnSubmit_Click" Margin="5" />
                <Button x:Name="btnStop" Content="停止获取" Click="btnStop_Click"    Margin="5" />
                <TextBlock x:Name="lblStockMessage" Margin="5" />
        
        </StackPanel>
</UserControl>
 
DuplexService.xaml.cs
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlightusing System;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlightusing System.Collections.Generic;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlightusing System.Linq;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlightusing System.Net;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlightusing System.Windows;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlightusing System.Windows.Controls;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlightusing System.Windows.Documents;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlightusing System.Windows.Input;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlightusing System.Windows.Media;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlightusing System.Windows.Media.Animation;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlightusing System.Windows.Shapes;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlightusing System.ServiceModel;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlightusing System.ServiceModel.Channels;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlightusing System.Threading;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlightusing System.IO;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlightnamespace Silverlight20.Communication
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight{
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight        public partial class DuplexService : UserControl
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight        {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                SynchronizationContext _syncContext;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                // 是否接收服务端发送过来的消息
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                bool _status = true;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                public DuplexService()
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        InitializeComponent();
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                private void btnSubmit_Click(object sender, RoutedEventArgs e)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        _status = true;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        // UI 线程
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        _syncContext = SynchronizationContext.Current;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        PollingDuplexHttpBinding binding = new PollingDuplexHttpBinding()
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                // InactivityTimeout - 服务端与客户端在此超时时间内无任何消息交换的情况下,服务会关闭其会话
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                InactivityTimeout = TimeSpan.FromMinutes(1)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        };
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        // 构造 IDuplexSessionChannel 的信道工厂
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        IChannelFactory<IDuplexSessionChannel> factory =
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                binding.BuildChannelFactory<IDuplexSessionChannel>(new BindingParameterCollection());
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        // 打开信道工厂
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        IAsyncResult factoryOpenResult =
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                factory.BeginOpen(new AsyncCallback(OnOpenCompleteFactory), factory);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        if (factoryOpenResult.CompletedSynchronously)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                // 如果信道工厂被打开的这个 异步操作 已经被 同步完成 则执行下一步
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                CompleteOpenFactory(factoryOpenResult);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                private void btnStop_Click(object sender, RoutedEventArgs e)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        _status = false;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                void OnOpenCompleteFactory(IAsyncResult result)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        // 该异步操作已被同步完成的话则不做任何操作,反之则执行下一步
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        if (result.CompletedSynchronously)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                return;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        else
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                CompleteOpenFactory(result);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                void CompleteOpenFactory(IAsyncResult result)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        IChannelFactory<IDuplexSessionChannel> factory = result.AsyncState as IChannelFactory<IDuplexSessionChannel>;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        // 完成异步操作,以打开信道工厂
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        factory.EndOpen(result);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        // 在信道工厂上根据指定的地址创建信道
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        IDuplexSessionChannel channel =
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                factory.CreateChannel(new EndpointAddress("http://localhost:3036/DuplexService.svc"));
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        // 打开信道
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        IAsyncResult channelOpenResult =
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                channel.BeginOpen(new AsyncCallback(OnOpenCompleteChannel), channel);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        if (channelOpenResult.CompletedSynchronously)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                // 如果信道被打开的这个 异步操作 已经被 同步完成 则执行下一步
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                CompleteOpenChannel(channelOpenResult);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                void OnOpenCompleteChannel(IAsyncResult result)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        // 该异步操作已被同步完成的话则不做任何操作,反之则执行下一步
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        if (result.CompletedSynchronously)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                return;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        else
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                CompleteOpenChannel(result);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                void CompleteOpenChannel(IAsyncResult result)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        IDuplexSessionChannel channel = result.AsyncState as IDuplexSessionChannel;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        // 完成异步操作,以打开信道
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        channel.EndOpen(result);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        // 构造需要发送到服务端的 System.ServiceModel.Channels.Message (客户端终结点与服务端终结点之间的通信单元)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        Message message = Message.CreateMessage(
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                channel.GetProperty<MessageVersion>(), // MessageVersion.Soap11 (Duplex 服务仅支持 Soap11)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                "Silverlight20/IDuplexService/SendStockCode", // Action 为请求的目的地(需要执行的某行为的路径)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                txtStockCode.Text);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        // 向目的地发送消息
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        IAsyncResult resultChannel =
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                channel.BeginSend(message, new AsyncCallback(OnSend), channel);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        if (resultChannel.CompletedSynchronously)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                // 如果向目的地发送消息的这个 异步操作 已经被 同步完成 则执行下一步
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                CompleteOnSend(resultChannel);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        // 监听指定的信道,用于接收返回的消息
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        ReceiveLoop(channel);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                void OnSend(IAsyncResult result)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        // 该异步操作已被同步完成的话则不做任何操作,反之则执行下一步
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        if (result.CompletedSynchronously)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                return;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        else
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                CompleteOnSend(result);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                void CompleteOnSend(IAsyncResult result)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        try
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                IDuplexSessionChannel channel = (IDuplexSessionChannel)result.AsyncState;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                // 完成异步操作,以完成向目的地发送消息的操作
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                channel.EndSend(result);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        catch (Exception ex)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                _syncContext.Post(WriteText, ex.ToString() + Environment.NewLine);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                void ReceiveLoop(IDuplexSessionChannel channel)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        // 监听指定的信道,用于接收返回的消息
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        IAsyncResult result =    
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                channel.BeginReceive(new AsyncCallback(OnReceiveComplete), channel);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        if (result.CompletedSynchronously)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                CompleteReceive(result);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                void OnReceiveComplete(IAsyncResult result)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        if (result.CompletedSynchronously)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                return;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        else
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                CompleteReceive(result);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                void CompleteReceive(IAsyncResult result)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        IDuplexSessionChannel channel = (IDuplexSessionChannel)result.AsyncState;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        try
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                // 完成异步操作,以接收到服务端发过来的消息
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                Message receivedMessage = channel.EndReceive(result);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                if (receivedMessage == null)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                        // 服务端会话已被关闭
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                        // 此时应该关闭客户端会话,或向服务端发送消息以启动一个新的会话
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                else
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                        // 将接收到的信息输出到界面上
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                        string text = receivedMessage.GetBody<string>();
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                        _syncContext.Post(WriteText, text + Environment.NewLine);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                        if (!_status)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                        {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                                // 关闭信道
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                                IAsyncResult resultFactory =
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                                        channel.BeginClose(new AsyncCallback(OnCloseChannel), channel);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                                if (resultFactory.CompletedSynchronously)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                                        CompleteCloseChannel(result);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                        }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                        else
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                        {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                                // 继续监听指定的信道,用于接收返回的消息
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                                ReceiveLoop(channel);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                        }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        catch (Exception ex)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                // 出错则记日志
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                using (StreamWriter sw = new StreamWriter(@"C:\Silverlight_Duplex_Log.txt", true))
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                        sw.Write(ex.ToString());
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                        sw.WriteLine();
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                void OnCloseChannel(IAsyncResult result)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        if (result.CompletedSynchronously)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                return;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        else
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                                CompleteCloseChannel(result);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                void CompleteCloseChannel(IAsyncResult result)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        IDuplexSessionChannel channel = (IDuplexSessionChannel)result.AsyncState;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        // 完成异步操作,以关闭信道
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        channel.EndClose(result);
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                void WriteText(object text)
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                {
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        // 将信息打到界面上
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                        lblStockMessage.Text += (string)text;
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight                }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight        }
上接稳扎稳打Silverlight(23) - 2.0通信之调用WCF的双向通信(Duplex Service)_Silverlight}