Wince 中访问WCF服务

王朝学院·作者佚名  2016-05-16
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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 CodexmlAdapter.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

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航