MVC-API with Windows CE Embedded 6

WinCEMVC

Previously I posted about using a Web-API to import data from another vendor’s products into our database. One of my goals for our team has been to move away from using direct calls into our database be it by either .NET ADO, Linq or SQL commands and instead expose a means for all products to get their data needs via a service. When we built a mobile application; which is a Single Page Application, I rolled out a MVC-API using Microsoft’s framework for this. Our first big pay off on adaptability and cross platform use proved out when we needed to support a RFID Device made by Motorola. This device uses a Windows CE Embedded operating system. Below I will quickly cover how I coded to connect to the API from this device.

First things first…

I had tried to store the API url’s on the device in a XML file, but watch out because xml does not like the & symbol in any value. You can use things like & or %26 inline, but Windows CE in-application-browser does not seem to like this very much… My work around was to store the values on the device’s SQL Lite instance.

Now moving on, as you know an API will have an end point you send your query to.
For example:

https://www.mysite.org/api/schedule/{0}

The above api, requires a school id so you can pull the correct schedule for that school/site.

In my example, I will show how I pulled a set of inventory tags down from the API site.

I have a basic method called GetTags

The api is called, several helper methods help pull the data and parse it and eventually inflate a List with POCO objects called TagItem which is a simple class with Getters and Setters.

public static List<TagItem> GetTags(string ApiURL,int SiteUId)

{

string uri = String.Format(“{0}/api/Inventory/GetTags?siteUId={1}”, ApiURL, SiteUId);

return JsonConvert.DeserializeObject<List<TagItem>>(GET(uri));

}

As you can see we pass in the GENERIC API URL and pre-pend it to the API Signature.

The real meat here is the GET method which in turn uses the GetBaseRequest and other helper methods. I will post the code below since it should be fairly obvious what it is doing…

private static string GET(string pUri, int pTimeOut)

{

HttpWebRequest request = null;

string retValue = “”;

try

{

request = GetBaseRequest(pUri, “GET”, “application/json; charset=utf-8”, pTimeOut);

retValue = ReadRequestResponse(request);

}

catch (Exception)

{

throw;

}

return retValue;

}

private static HttpWebRequest GetBaseRequest(string pUri, string pMethod, string pContentType, int pTimeout)

{

System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(pUri);

request.Headers.Add(“Authorization”, Token);

request.Method = pMethod;

request.Proxy = null;

request.KeepAlive = false;

request.Timeout = pTimeout;

request.ContentType = pContentType;

request.UserAgent = “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)”;

return request;

}

private static string ReadRequestResponse(HttpWebRequest pRequest)

{

string retval;

HttpWebResponse response = (HttpWebResponse)pRequest.GetResponse();

using (Stream responseStream2 = response.GetResponseStream())

using (StreamReader reader = new StreamReader(responseStream2))

retval = reader.ReadToEnd();

response.Close();

return retval;

}

That is the meat of it, hope that helps someone else. If you have questions please let me know.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s