Silverlight与WCF之间的通信(1)SL客户端定时请求WCF服务

这几天在做一个Silverlight的页面聊天工具,基本上从头看了一遍Silverlight和WCF相关的东西,也遇到了不少问题,刚开始是用Silverlight做了定时请求服务器的数据信息,后期准备利用WCF的双工通信来做,通信过程分为两个部分记录。

一 ,需要建立一个WCF的服务端

这里直接在Silverlight的站点中新建了一个WCF服务,代码如下: 

代码
//先定义服务契约

[ServiceContract] 
    
public interface IChatService 
    { 
        [OperationContract] 
        
bool SendMessage(MessageInfo msg); 

        [OperationContract] 
        List
<MessageInfo> GetMessages(string UserAccount); 
    }

//再定义数据契约

[DataContract] 
    
public class MessageInfo 
    { 
        [DataMember]        
        
public string ID { setget; }        
        [DataMember] 
        
public string Title { setget; } 
        [DataMember] 
        
public string Message { setget; } 
        [DataMember] 
        
public DateTime SendTime { setget; } 
        [DataMember] 
        
public DateTime? ReadTime { setget; } 
        [DataMember] 
        
public string Sender { setget; }           
        [DataMember] 
        
public string ReceiveUser { setget; } 
        [DataMember] 
        
public string ReceiveOrgan { setget; } 
        [DataMember] 
        
public string ReceiveMode { setget; } 
        [DataMember] 
        
public int State { setget; } 
        [DataMember] 
        
public int Receipt { setget; } 
        [DataMember] 
        
public string Source { setget; } 

    }

//最后在webconfig中配置好服务

<system.serviceModel> 
        
<behaviors> 
            
<serviceBehaviors> 
                
<behavior name="ChatBehavior"> 
                    
<serviceMetadata httpGetEnabled="true" /> 
                    
<serviceDebug includeExceptionDetailInFaults="false" /> 
                
</behavior> 
            
</serviceBehaviors> 
        
</behaviors> 
      
<services> 
        
<service behaviorConfiguration="ChatBehavior" name="WebChat.Web.ChatService"> 
          
<endpoint address="" binding="basicHttpBinding" contract="WebChat.Web.IChatService"></endpoint> 
        
</service> 
      
</services> 
        
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    
</system.serviceModel>

 到这里呢,一个服务就Host在IIS上了,可以通过浏览服务或服务引用进行验证服务是否正常运行。

image

二,在Silverlight中向服务器请求数据 

代码
public partial class MainPage : UserControl 
    { 
        Timer timer; 
        ChatService.ChatServiceClient proxy; 

        BasicHttpBinding binding; 
        EndpointAddress endPoint; 

        
public MainPage() 
        { 
            InitializeComponent(); 

            binding 
= new BasicHttpBinding(); 
            endPoint 
= new EndpointAddress("http://localhost:1939/ChatService.svc%22);

            timer 
= new Timer(new TimerCallback(GetMessages),this,100,100); 
        } 

        
#region 定时检测信息 
        
public void GetMessages(object obj) 
        { 
            proxy 
= new ChatService.ChatServiceClient(binding, endPoint); 
            proxy.GetMessagesCompleted 
+= new EventHandler<ChatService.GetMessagesCompletedEventArgs>(GetMessagesComleted); 
            proxy.GetMessagesAsync(
"wengyuli"); 
        } 

        
void GetMessagesComleted(object sender, ChatService.GetMessagesCompletedEventArgs e) 
        { 
            
if(e.Error==null
            { 
                
if (this.listMsgs.Dispatcher.CheckAccess()) 
                { 
                  
this.listMsgs.ItemsSource= e.Result; 
                } 
                
else 
                { 
                  
this.listMsgs.Dispatcher.BeginInvoke(() => { this.listMsgs.ItemsSource = e.Result; }); 
                } 
            } 
        } 
        
#endregion 

        
#region  发送信息 
        
private void btnSend_Click(object sender, RoutedEventArgs e) 
        { 
            ChatService.MessageInfo message 
= new ChatService.MessageInfo(); 
            message.ID 
= Guid.NewGuid().ToString(); 
            message.Receipt 
= 0
            message.ReceiveMode 
= "user"
            message.ReceiveOrgan 
= ""
            message.ReceiveUser 
= "xiejianjun"
            message.Message 
= this.txtMessage.Text; 
            message.Sender 
= "wengyuli"
            message.SendTime 
= DateTime.Now; 
            message.Source 
= "web"
            message.State 
= 0
            message.Title 
= this.txtMessage.Text; 

            proxy 
= new ChatService.ChatServiceClient(binding, endPoint); 
            proxy.SendMessageCompleted 
+= new EventHandler<ChatService.SendMessageCompletedEventArgs>(SendMessageComleted); 

            proxy.SendMessageAsync(message); 
        } 
        
void SendMessageComleted(object sender,ChatService.SendMessageCompletedEventArgs e) 
        { 
            
if (e.Error == null
            { 
                
//MessageBox.Show("服务器返回结果:" + e.Result); 
            } 
        } 
        
#endregion 
    }

 

前台:

 

<ListBox Height="213" HorizontalAlignment="Left" DisplayMemberPath="Message" ItemsSource="{Binding MessageInfo}" Name="listMsgs" VerticalAlignment="Top" Width="283" ></ListBox>

  

编译并运行后得到数据:

image

posted @ 2010-06-18 22:53  翁玉礼  阅读(3810)  评论(21编辑  收藏  举报