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 {  set get ; }        
        [DataMember] 
        
public   string  Title {  set get ; } 
        [DataMember] 
        
public   string  Message {  set get ; } 
        [DataMember] 
        
public  DateTime SendTime {  set get ; } 
        [DataMember] 
        
public  DateTime ?  ReadTime {  set get ; } 
        [DataMember] 
        
public   string  Sender {  set get ; }           
        [DataMember] 
        
public   string  ReceiveUser {  set get ; } 
        [DataMember] 
        
public   string  ReceiveOrgan {  set get ; } 
        [DataMember] 
        
public   string  ReceiveMode {  set get ; } 
        [DataMember] 
        
public   int  State {  set get ; } 
        [DataMember] 
        
public   int  Receipt {  set get ; } 
        [DataMember] 
        
public   string  Source {  set get ; } 

    }

// 最后在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上了,可以通过浏览服务或服务引用进行验证服务是否正常运行。

   

二,在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 >

  

编译并运行后得到数据:

 


     本文转自wengyuli 51CTO博客,原文链接:http://blog.51cto.com/wengyuli/587838,如需转载请自行联系原作者





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值