Access the WCF Service in Wince

Source: Internet
Author: User

As this article is not a popular article on WinCE development, we recommend that you go to Baidu and Google to find the answer for the basics of WinCE development and WCF development, and then work out how to access WCF in this article. Thank you.

 

Development Environment

IDE: Visual Studio 2008 (not supported currently in 2010, 2012, and 2013)

OS: Win 7 (64-bit)

Tools: ActiveSync win7 v6.1 (the device center provides a network for the Pocket PC 2003 simulator)

Simulator network connection strategy a copy: http://www.jb51.net/softjc/42088.html

Create an WinCE Project

This article is not widely used in WinCE development, so please use Baidu.

 

WCF Server

Key code in app. config

<Service behaviorConfiguration = "SystemDispatchServiceForPDABehavior" name = "SystemManageServiceLibrary. SystemDispatchServiceForPDA"> <! -- PDA system allocation --> <endpoint address = "http: // localhost: 20003/SystemDispatchForPDA/SystemDispatchServiceForPDA" binding = "webHttpBinding" contract = "SystemManageServiceLibrary. systemDispatch. ISystemDispatchServiceForPDA "> </endpoint> <! -- Metadata allocated by the PDA system --> <endpoint address = "http: // localhost: 20003/SystemDispatchForPDA/SystemDispatchServiceForPDA/mex "binding =" mexHttpBinding "contract =" IMetadataExchange "/> Service Contract-publish wcf rest (For details, refer to Baidu to search for wcf rest)

[ServiceContract] public interface ISystemDispatchServiceForPDA {// <summary> // PDA obtains cluster information /// </summary> /// <param name = "strPDA_IMEI"> inside the PDA factory no. </param> /// <returns> </returns> [OperationContract] // UriTemplate is actually a url rule for sending requests over http, replace {strPDA_IMEI} with the actual PDA string number. Then you can [WebGet (UriTemplate = "GetClusterInfo/{strPDA_IMEI}")] CLUSTER GetClusterInfo (string strPDA_IMEI );}View Code

 

WinCE

HttpWrapper. cs-encapsulation of Http requests, access the REST service provided by WCF

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"; // receive the returned page 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 adapter, used to convert Xml into a class

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

Call Method

        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;        }

 

 

Note the following points:

1. Install the Device Center

2. Set the network connection of the simulator.

3. WCF REST

4. WinCE parses the XML returned by WCF and how to splice the access URL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.