Wp7可以方便得调用.net的WebService
?
先用C#做一个WebService,实现两个接口
GetMessage?:发一条信息
SendMessage?:接受最新一条信息
- using?System;??
- using?System.Data;??
- using?System.Web;??
- using?System.Collections;??
- using?System.Web.Services;??
- using?System.Web.Services.Protocols;??
- using?System.ComponentModel;??
- using?System.Data.SqlClient;??
- using?COM;??
- ??
- namespace?WindowsService??
- {??
- ??????
- ??????
- ///?</summary>??
- ????[WebService(Namespace?=?"http://tempuri.org/")]??
- ????[WebServiceBinding(ConformsTo?=?WsiProfiles.BasicProfile1_1)]??
- ????[ToolboxItem(false)]??
- ????public?class?Service1?:?System.Web.Services.WebService??
- ????{??
- ??
- ????????[WebMethod]??
- ????????public?Message?GetMessage(String?id)??
- ????????{??
- ????????????SqlConnection?SqlCon?=?new?SqlConnection();??
- ????????????DataSet?ds?=?new?DataSet();??
- ????????????Message?msg?=?null;??
- ????????????try??
- ????????????{??
- ????????????????SqlCon?=?DBUtil.getSqlConnection();??
- ????????????????DBUtil.SelectSqlCommand(ds,?"select?TOP?1?body,sms_time?from?SMSTbl?where?id?=?'"?+?id?+?"'?order?by?sms_time?DESC",?ref?SqlCon);??
- ????????????????if?(ds.Tables[0].Rows.Count?>?0)??
- ????????????????{??
- ????????????????????msg?=?new?Message();??
- ????????????????????msg.body?=?ds.Tables[0].Rows[0][0].ToString();??
- ????????????????????msg.time?=?ds.Tables[0].Rows[0][1].ToString();??
- ????????????????}??
- return?msg;??
- ????????????}??
- finally??
- ????????????????DBUtil.closeConnection(SqlCon);??
- ????????}??
- int?SendMessage(String?id,?String?body)??
- ????????????????return?DBUtil.InsertSqlCommand("Insert?into?SMSTbl(?id,sms_time,body)?values('"?+?id?+?"',getdate(),'"?+?body?+?"')",248); line-height:18px"> ????????????}??
- ????????????finally??
- ????????????{??
- ????????????????DBUtil.closeConnection(SqlCon);??
- ????????}??
- ????}??
- }??
再在wp7 程序引用WebService

在wp7里调用它
copy
??
- using?DecoMailer.ServiceReference1;??
- ??
- ?private?void?KanjiModeButton_Click(object?sender,?RoutedEventArgs?e)??
- //实例化SoapClient??
- ????????????ServiceReference1.Service1SoapClient?soap?=?new?Service1SoapClient();??
- //调用GetMessage方法,并把ID:1传给方法??
- ????????????soap.GetMessageAsync("1");??
- //Respones将触发GetMessageCompletedEvent事件,并调用soap_GetMessage处理??
- ????????????soap.GetMessageCompleted?+=?new?EventHandler<GetMessageCompletedEventArgs>(soap_GetMessage);??
- void?soap_GetMessage(if?(e.Error?==?null)??
- //取得最新的Message类对象??
- ????????????????ServiceReference1.Message?lastMessage?=?e.Result;??
- ??????????????????
- ????????}??
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|