Wp7 调用WebService

来源:互联网 发布:学电脑编程要多少钱 编辑:程序博客网 时间:2024/06/05 20:26

Wp7可以方便得调用.net的WebService

 

先用C#做一个WebService,实现两个接口

GetMessage :发一条信息

SendMessage :接受最新一条信息

[c-sharp] view plaincopy
  1. using System;  
  2. using System.Data;  
  3. using System.Web;  
  4. using System.Collections;  
  5. using System.Web.Services;  
  6. using System.Web.Services.Protocols;  
  7. using System.ComponentModel;  
  8. using System.Data.SqlClient;  
  9. using COM;  
  10.   
  11. namespace WindowsService  
  12. {  
  13.     /// <summary>  
  14.     /// Service1 の概要の説明です  
  15.     /// </summary>  
  16.     [WebService(Namespace = "http://tempuri.org/")]  
  17.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  18.     [ToolboxItem(false)]  
  19.     public class Service1 : System.Web.Services.WebService  
  20.     {  
  21.   
  22.         [WebMethod]  
  23.         public Message GetMessage(String id)  
  24.         {  
  25.             SqlConnection SqlCon = new SqlConnection();  
  26.             DataSet ds = new DataSet();  
  27.             Message msg = null;  
  28.             try  
  29.             {  
  30.                 SqlCon = DBUtil.getSqlConnection();  
  31.                 DBUtil.SelectSqlCommand(ds, "select TOP 1 body,sms_time from SMSTbl where id = '" + id + "' order by sms_time DESC"ref SqlCon);  
  32.                 if (ds.Tables[0].Rows.Count > 0)  
  33.                 {  
  34.                     msg = new Message();  
  35.                     msg.body = ds.Tables[0].Rows[0][0].ToString();  
  36.                     msg.time = ds.Tables[0].Rows[0][1].ToString();  
  37.                 }  
  38.                 return msg;  
  39.             }  
  40.             finally  
  41.             {  
  42.                 DBUtil.closeConnection(SqlCon);  
  43.             }  
  44.         }  
  45.   
  46.         [WebMethod]  
  47.         public int SendMessage(String id, String body)  
  48.         {  
  49.             SqlConnection SqlCon = new SqlConnection();  
  50.             try  
  51.             {  
  52.                 SqlCon = DBUtil.getSqlConnection();  
  53.                 return DBUtil.InsertSqlCommand("Insert into SMSTbl( id,sms_time,body) values('" + id + "',getdate(),'" + body + "')"ref SqlCon);  
  54.             }  
  55.             finally  
  56.             {  
  57.                 DBUtil.closeConnection(SqlCon);  
  58.             }  
  59.         }  
  60.     }  
  61. }  

 

再在wp7 程序引用WebService

 

 

在wp7里调用它

 

[c-sharp] view plaincopy
  1. //添加引用  
  2. using DecoMailer.ServiceReference1;  
  3.   
  4. //在[按钮]押下事件中         
  5.  private void KanjiModeButton_Click(object sender, RoutedEventArgs e)  
  6.         {  
  7. //实例化SoapClient  
  8.             ServiceReference1.Service1SoapClient soap = new Service1SoapClient();  
  9. //调用GetMessage方法,并把ID:1传给方法  
  10.             soap.GetMessageAsync("1");  
  11. //Respones将触发GetMessageCompletedEvent事件,并调用soap_GetMessage处理  
  12.             soap.GetMessageCompleted += new EventHandler<GetMessageCompletedEventArgs>(soap_GetMessage);  
  13.         }  
  14.   
  15.         private void soap_GetMessage(object sender, GetMessageCompletedEventArgs e)  
  16.         {  
  17.             if (e.Error == null)  
  18.             {  
  19. //取得最新的Message类对象  
  20.                 ServiceReference1.Message lastMessage = e.Result;  
  21.                 //MessageEditor.Message = lastMessage;  
  22.             }  
  23.         }  

原创粉丝点击