Wince 中访问WCF服务

由于本文并非WinCE开发普及篇,所以一些WinCE开发和WCF开发的基础还请移步百度和谷歌寻找答案,然后结合本文开发出WinCE中如何访问WCF,谢谢。

 

开发环境

IDE:Visual Studio 2008 (2010、2012、2013目前都不支持)

OS:Win 7 (64位)

Tools:ActiveSync win7 v6.1(设备中心,给Pocket PC 2003模拟器提供网络)

模拟器网络连接攻略一份:http://www.jb51.net/softjc/42088.html

创建WinCE项目

请恕本文并非WinCE开发普及篇,所以这些请百度吧。

 

WCF服务端

app.config中关键代码

<service behaviorConfiguration="SystemDispatchServiceForPDABehavior" name="SystemManageServiceLibrary.SystemDispatchServiceForPDA">
        <!--PDA系统分配-->
        <endpoint address="http://localhost:20003/SystemDispatchForPDA/SystemDispatchServiceForPDA"
          binding="webHttpBinding"
          contract="SystemManageServiceLibrary.SystemDispatch.ISystemDispatchServiceForPDA" >
        </endpoint>
        <!--PDA系统分配元数据-->
        <endpoint address="http://localhost:20003/SystemDispatchForPDA/SystemDispatchServiceForPDA/mex"
          binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:20003/SystemDispatchForPDA"/>
          </baseAddresses>
          <timeouts openTimeout="00:00:30" />
        </host>
      </service>
View Code

服务契约 - 公布WCF REST(详细的可以百度搜索 WCF REST)

    [ServiceContract]
    public interface ISystemDispatchServiceForPDA
    {
        /// <summary>
        /// PDA获取集群信息
        /// </summary>
        /// <param name="strPDA_IMEI">PDA内部出厂序号</param>
        /// <returns></returns>
        [OperationContract]
        //UriTemplate 实际就是通过http协议发送请求的url规则,把{strPDA_IMEI}替换成真实的PDA串号即可
        [WebGet(UriTemplate = "GetClusterInfo/{strPDA_IMEI}")]
        CLUSTER GetClusterInfo(string strPDA_IMEI);
    }
View Code

 

WinCE

HttpWrapper.cs - Http请求的封装,访问WCF提供的REST服务

    public class HttpWrapper
    {
        public static string SendRequest(string url)
        {
            HttpWebResponse response = null;
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            request.AllowWriteStreamBuffering = false;
            request.KeepAlive = true;
            request.ContentType = "application/x-www-form-urlencoded";

            // 接收返回的页面
            response = request.GetResponse() as HttpWebResponse;
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
            string strResult = reader.ReadToEnd();
            reader.Close();
            response.Close();
            return strResult;
        }
    }
View Code

XmlAdapter.cs - Xml适配器,用于将Xml转换成类

    public class XmlAdapter
    {
        public static T ConvertToClass<T>(string strXML) where T : class
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

            MemoryStream reader = new MemoryStream(Encoding.UTF8.GetBytes(strXML));

            T obj = xmlSerializer.Deserialize(reader) as T;

            reader.Dispose();

            return obj;
        }
    }
View Code

调用方法

        private static string URL = "http://ip:20003/SystemDispatchForPDA/SystemDispatchServiceForPDA/";

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

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

            return cluster;
        }

 

 

真正需要注意的其实就是几点:

1.安装设备中心

2.设置模拟器网络连接

3.WCF REST

4.WinCE解析WCF返回的XML,以及如何拼接访问的URL

    

posted @ 2014-05-09 14:13  寻找和谐  阅读(1331)  评论(1编辑  收藏  举报