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 >

  

编译并运行后得到数据:

相关文章

如何在Silverlight4(XAML)中绑定IsEnabled属性?我试过简单的...
我正在编写我的第一个vb.net应用程序(但我也会在这里标记c#,...
ProcessFile()是在UIThread上运行还是在单独的线程上运行.如...
我从同行那里听说,对sharepoint的了解对职业生涯有益.我们不...
我正在尝试保存一个类我的类对象的集合.我收到一个错误说明:...
我需要根据Silverlight中的某些配置值设置给定控件的Style.我...