本文讲一下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>
编译并运行后得到数据: