微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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 >

  

编译并运行后得到数据:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐