A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© aisini 金牌黑马   /  2014-7-25 15:44  /  1034 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 aisini 于 2014-7-25 15:45 编辑

           由于本文并非WinCE开发普及篇,所以一些WinCE开发和WCF开发的基础还请移步百度和谷歌寻找答案,然后结合本文开发出WinCE中如何访问WCF,谢谢
开发环境
     IDE:Visual Studio 2008 (2010、2012、2013目前都不支持)
    OS:Win 7 (64位)
   Tools:ActiveSync win7 v6.1(设备中心,给Pocket PC 2003模拟器提供网络)
   WCF服务端    app.config中关键代码
  1. <service behaviorConfiguration="SystemDispatchServiceForPDABehavior" name="SystemManageServiceLibrary.SystemDispatchServiceForPDA">
  2.         <!--PDA系统分配-->
  3.         <endpoint address="http://localhost:20003/SystemDispatchForPDA/SystemDispatchServiceForPDA"
  4.           binding="webHttpBinding"
  5.           contract="SystemManageServiceLibrary.SystemDispatch.ISystemDispatchServiceForPDA" >
  6.         </endpoint>
  7.         <!--PDA系统分配元数据-->
  8.         <endpoint address="http://localhost:20003/SystemDispatchForPDA/SystemDispatchServiceForPDA/mex"
  9.           binding="mexHttpBinding" contract="IMetadataExchange" />
  10.         <host>
  11.           <baseAddresses>
  12.             <add baseAddress="http://localhost:20003/SystemDispatchForPDA"/>
  13.           </baseAddresses>
  14.           <timeouts openTimeout="00:00:30" />
  15.         </host>
  16.       </service>
复制代码
服务契约 - 公布WCF REST(详细的可以百度搜索 WCF REST)
  1. [ServiceContract]
  2.     public interface ISystemDispatchServiceForPDA
  3.     {
  4.         /// <summary>
  5.         /// PDA获取集群信息
  6.         /// </summary>
  7.         /// <param name="strPDA_IMEI">PDA内部出厂序号</param>
  8.         /// <returns></returns>
  9.         [OperationContract]
  10.         //UriTemplate 实际就是通过http协议发送请求的url规则,把{strPDA_IMEI}替换成真实的PDA串号即可
  11.         [WebGet(UriTemplate = "GetClusterInfo/{strPDA_IMEI}")]
  12.         CLUSTER GetClusterInfo(string strPDA_IMEI);
  13.     }
复制代码
  WinCE      HttpWrapper.cs - Http请求的封装,访问WCF提供的REST服务
  1. public class HttpWrapper
  2.     {
  3.         public static string SendRequest(string url)
  4.         {
  5.             HttpWebResponse response = null;
  6.             HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
  7.             request.Method = "GET";
  8.             request.AllowWriteStreamBuffering = false;
  9.             request.KeepAlive = true;
  10.             request.ContentType = "application/x-www-form-urlencoded";

  11.             // 接收返回的页面
  12.             response = request.GetResponse() as HttpWebResponse;
  13.             Stream responseStream = response.GetResponseStream();
  14.             StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
  15.             string strResult = reader.ReadToEnd();
  16.             reader.Close();
  17.             response.Close();
  18.             return strResult;
  19.         }
  20.     }
复制代码
       XmlAdapter.cs - Xml适配器,用于将Xml转换成类
  1. public class XmlAdapter
  2.     {
  3.         public static T ConvertToClass<T>(string strXML) where T : class
  4.         {
  5.             XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
  6.             MemoryStream reader = new MemoryStream(Encoding.UTF8.GetBytes(strXML));
  7.             T obj = xmlSerializer.Deserialize(reader) as T;
  8.             reader.Dispose();
  9.             return obj;
  10.         }
  11.     }
复制代码
       调用方法        
  1. private static string URL = "http://ip:20003/SystemDispatchForPDA/SystemDispatchServiceForPDA/";

  2.         public static CLUSTER GetClusterInfo(string strPDA_IMEI)
  3.         {
  4.             string strResponse = HttpWrapper.SendRequest(URL + "GetClusterInfo/" + strPDA_IMEI);

  5.             CLUSTER cluster = XmlAdapter.ConvertToClass<CLUSTER>(strResponse);

  6.             return cluster;
  7.         }<span class="Apple-tab-span">                </span>
复制代码
真正需要注意的其实就是几点:     

        1.安装设备中心
        2.设置模拟器网络连接   
        3.WCF REST   
        4.WinCE解析WCF返回的XML,以及如何拼接访问的URL



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马