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

    

转载于:https://www.cnblogs.com/doddgu/p/3718778.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值