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
    评论
在 Visual Studio 创建 WCF 服务时,有两种项目类型可供选择:WCF 服务应用程序和 WCF 服务库。这两种项目类型有以下区别: 1. WCF 服务应用程序是一种可执行文件,它包含了一个独立的 WCF 服务宿主。这意味着您可以直接运行 WCF 服务应用程序,并使用其自带的服务宿主来运行和测试您的 WCF 服务。当您部署 WCF 服务时,您可以将 WCF 服务应用程序部署到 IIS 或自定义的 Windows 服务。 2. WCF 服务库是一种库项目,它包含了一个或多个 WCF 服务契约和实现类。这意味着您必须将 WCF 服务库引用到另一个托管应用程序,然后手动配置该应用程序的服务宿主来运行和测试您的 WCF 服务。当您部署 WCF 服务时,您可以将 WCF 服务库部署到 IIS ,但必须将其部署为 WCF 服务应用程序的一部分。 3. WCF 服务应用程序和 WCF 服务库的项目结构也不同。WCF 服务应用程序包含了一个 .svc 文件和一个 Web.config 配置文件,用于配置 WCF 服务宿主和服务终结点。而 WCF 服务库只包含 WCF 服务契约和实现类,这些类通常包含在一个或多个 .cs 文件。 4. 在使用 WCF 服务应用程序时,您可以使用 Visual Studio 自带的 WCF 测试客户端来测试您的 WCF 服务。而在使用 WCF 服务库时,您必须手动编写测试客户端或使用第三方测试工具来测试您的 WCF 服务。 总之,WCF 服务应用程序是一种独立的 WCF 服务宿主,适用于独立运行和测试 WCF 服务。而 WCF 服务库则是一种库项目,适用于将 WCF 服务集成到其他托管应用程序

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值