<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Poltergeist's blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/kirpas/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/kirpas/feed.xml</link>
    <description>On the DOT of the NET but within the FRAMEWORK!</description>
    <lastBuildDate>Wed, 17 Apr 2019 04:19:12 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Developing a Windows Phone 8.1 client for the sensor data tracker web API via a Portable Class Library</title>
      <link>https://docs.microsoft.com/archive/blogs/kirpas/developing-a-windows-phone-8-1-client-for-the-sensor-data-tracker-web-api-via-a-portable-class-library</link>
      <pubDate>Fri, 20 Jun 2014 17:41:10 GMT</pubDate>
      <dc:creator><![CDATA[Kirpa Singh]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/kirpas/2014/06/20/developing-a-windows-phone-8-1-client-for-the-sensor-data-tracker-web-api-via-a-portable-class-library/</guid>
      <description><![CDATA[Related Posts:     Writing a simple implementation of dependency injection in MVC 4 Web API with...]]></description>
      <content:encoded><![CDATA[<p><font size="2"></font></p>  <hr />  <p><font size="2"><strong><em>Related Posts:</em></strong></font></p>  <ol>   <li><a href="https://blogs.msdn.com/b/kirpas/archive/2013/08/06/writing-a-simple-implementation-of-dependency-injection-in-mvc-4-web-api-with-net-framework-4-5.aspx"><font size="2">Writing a simple implementation of dependency injection in MVC 4 Web API with .NET Framework 4.5</font></a></li>    <li><a href="https://blogs.msdn.com/b/kirpas/archive/2014/05/14/writing-bdd-style-unit-tests-to-verify-the-mvc-based-web-api.aspx"><font size="2">Writing BDD style unit tests to verify the MVC based Web API</font></a></li> </ol>  <hr />  <p><font size="2">To recap, so far we put together a <a href="https://blogs.msdn.com/b/kirpas/archive/2013/08/06/writing-a-simple-implementation-of-dependency-injection-in-mvc-4-web-api-with-net-framework-4-5.aspx">MVC based Web API and examined the mechanism for plugging in a very simple dependency injection mechanism</a> without using an established framework. Further on we added <a href="https://blogs.msdn.com/b/kirpas/archive/2014/05/14/writing-bdd-style-unit-tests-to-verify-the-mvc-based-web-api.aspx">BDD style unit tests</a> to verify the API behavior. Since the base framework is established and sensor readings are getting generated as expected, let’s develop a UI client to consume the generated data. Now there are several ways to consume and display the sensor data such as a desktop client, WPF client, browser client, Windows Store client and what not!</font></p>  <p><font size="2">How about a mobile client such as one based on Windows Phone 8.1 that also coincides with the mission statement of Microsoft:</font></p>  <h5><font style="background-color: #ffffff"><font color="#0080ff">“….To create a family of devices and services for individuals and businesses that empower people around the globe at home, at work and on the go, for the activities they value most….&quot;</font>&#160;</font></h5>  <p><font size="2">Without further ado, let’s dig into this idea! Start by adding a new <strong>Windows Phone</strong> project to the solution. </font></p>  <p><font size="2">Right click on the <strong>Solution</strong> and choose <strong>Add</strong>—&gt;<strong>New Project…</strong> Select Windows Phone as the project type. There are several templates available for creating a Windows Phone project. Since we need two pages, one for generating and storing sensor readings and another for browsing the sensor data, let’s choose <strong>Blank App</strong> template and add these pivots manually:</font></p>  <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/5556.image_4CBDD05E.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/5556.image_5F00_4CBDD05E.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/6136.image_thumb_738BE69E.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/6136.image_5F00_thumb_5F00_738BE69E.png" width="337" height="234" /></a></p>  <p><font size="2">To adhere to <a href="https://msdn.microsoft.com/en-us/library/windowsphone/develop/ff637522(v=vs.105).aspx">Globalization and Localization best practices for Windows Phone</a>, we’ll store all the resource strings in resource files. Create a new folder called <strong>Strings</strong> and add another folder under it to store <strong>en-US</strong> strings. Now add a new <strong>Resource file</strong> (<strong>Resources.resw</strong>) under <strong>en-US</strong> folder as shown below:</font></p>  <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/4073.image_5370D9E1.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/4073.image_5F00_5370D9E1.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/5554.image_thumb_4C519D69.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/5554.image_5F00_thumb_5F00_4C519D69.png" width="336" height="233" /></a></p>  <p><font size="2">Once the resource file is created, resource editor window opens, allowing you to enter the resource strings for your app. Copy and paste the following strings:</font></p>  <p><font size="2">     <table class="MsoTable15Grid4Accent1" style="border-top: medium none; border-right: medium none; border-collapse: collapse; border-bottom: medium none; border-left: medium none; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-yfti-tbllook: 1184; mso-padding-alt: 0in 5.4pt 0in 5.4pt" cellspacing="0" cellpadding="0" border="1"><tbody>         <tr style="mso-yfti-irow: -1; mso-yfti-firstrow: yes; mso-yfti-lastfirstrow: yes">           <td style="border-top: #5b9bd5 1pt solid; width: 155.8pt; background: #5b9bd5; border-bottom: #5b9bd5 1pt solid; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; border-left: #5b9bd5 1pt solid; border-right-color: #f0f0f0; padding-right: 5.4pt; mso-border-themecolor: accent1; mso-border-top-alt: solid #5b9bd5 .5pt; mso-border-top-themecolor: accent1; mso-border-left-alt: solid #5b9bd5 .5pt; mso-border-left-themecolor: accent1; mso-border-bottom-alt: solid #5b9bd5 .5pt; mso-border-bottom-themecolor: accent1; mso-background-themecolor: accent1" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-yfti-cnfc: 5"><b><span style="color: white; mso-themecolor: background1"><font size="3"><font face="Calibri">Name </font></font></span></b></p>           </td>            <td style="border-top: #5b9bd5 1pt solid; width: 155.85pt; background: #5b9bd5; border-bottom: #5b9bd5 1pt solid; border-left-color: #f0f0f0; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; border-right-color: #f0f0f0; padding-right: 5.4pt; mso-border-top-alt: solid #5b9bd5 .5pt; mso-border-top-themecolor: accent1; mso-border-bottom-alt: solid #5b9bd5 .5pt; mso-border-bottom-themecolor: accent1; mso-background-themecolor: accent1" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-yfti-cnfc: 1"><b><span style="color: white; mso-themecolor: background1"><font size="3"><font face="Calibri">Value </font></font></span></b></p>           </td>            <td style="border-top: #5b9bd5 1pt solid; border-right: #5b9bd5 1pt solid; width: 155.85pt; background: #5b9bd5; border-bottom: #5b9bd5 1pt solid; border-left-color: #f0f0f0; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; padding-right: 5.4pt; mso-border-themecolor: accent1; mso-border-top-alt: solid #5b9bd5 .5pt; mso-border-top-themecolor: accent1; mso-border-bottom-alt: solid #5b9bd5 .5pt; mso-border-bottom-themecolor: accent1; mso-background-themecolor: accent1; mso-border-right-alt: solid #5b9bd5 .5pt; mso-border-right-themecolor: accent1" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-yfti-cnfc: 1"><b><span style="color: white; mso-themecolor: background1"><font size="3"><font face="Calibri">Comment </font></font></span></b></p>           </td>         </tr>          <tr style="mso-yfti-irow: 0">           <td style="border-right: #9cc2e5 1pt solid; border-top-color: #f0f0f0; width: 155.8pt; background: #deeaf6; border-bottom: #9cc2e5 1pt solid; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; border-left: #9cc2e5 1pt solid; padding-right: 5.4pt; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9cc2e5 .5pt; mso-border-top-themecolor: accent1; mso-background-themecolor: accent1; mso-border-top-themetint: 153; mso-background-themetint: 51" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-yfti-cnfc: 68"><strong>PagingControl.Text</strong></p>           </td>            <td style="border-right: #9cc2e5 1pt solid; border-top-color: #f0f0f0; width: 155.85pt; background: #deeaf6; border-bottom: #9cc2e5 1pt solid; border-left-color: #f0f0f0; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; padding-right: 5.4pt; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9cc2e5 .5pt; mso-border-top-themecolor: accent1; mso-border-left-alt: solid #9cc2e5 .5pt; mso-border-left-themecolor: accent1; mso-border-bottom-themecolor: accent1; mso-background-themecolor: accent1; mso-border-right-themecolor: accent1; mso-border-top-themetint: 153; mso-background-themetint: 51; mso-border-bottom-themetint: 153; mso-border-right-themetint: 153; mso-border-left-themetint: 153" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-yfti-cnfc: 64">Total: {0} - Showing Page {1} of {2}</p>           </td>            <td style="border-right: #9cc2e5 1pt solid; border-top-color: #f0f0f0; width: 155.85pt; background: #deeaf6; border-bottom: #9cc2e5 1pt solid; border-left-color: #f0f0f0; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; padding-right: 5.4pt; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9cc2e5 .5pt; mso-border-top-themecolor: accent1; mso-border-left-alt: solid #9cc2e5 .5pt; mso-border-left-themecolor: accent1; mso-border-bottom-themecolor: accent1; mso-background-themecolor: accent1; mso-border-right-themecolor: accent1; mso-border-top-themetint: 153; mso-background-themetint: 51; mso-border-bottom-themetint: 153; mso-border-right-themetint: 153; mso-border-left-themetint: 153" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-yfti-cnfc: 64"></p>              <p><font color="#000000" size="3" face="Calibri">&#160;</font></p>           </td>         </tr>          <tr style="mso-yfti-irow: 1">           <td style="border-right: #9cc2e5 1pt solid; border-top-color: #f0f0f0; width: 155.8pt; border-bottom: #9cc2e5 1pt solid; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; border-left: #9cc2e5 1pt solid; padding-right: 5.4pt; background-color: transparent; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9cc2e5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-yfti-cnfc: 4"><strong>PivotPostReadings.Header</strong></p>           </td>            <td style="border-right: #9cc2e5 1pt solid; border-top-color: #f0f0f0; width: 155.85pt; border-bottom: #9cc2e5 1pt solid; border-left-color: #f0f0f0; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; padding-right: 5.4pt; background-color: transparent; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9cc2e5 .5pt; mso-border-top-themecolor: accent1; mso-border-left-alt: solid #9cc2e5 .5pt; mso-border-left-themecolor: accent1; mso-border-bottom-themecolor: accent1; mso-border-right-themecolor: accent1; mso-border-top-themetint: 153; mso-border-bottom-themetint: 153; mso-border-right-themetint: 153; mso-border-left-themetint: 153" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal">Record Readings</p>           </td>            <td style="border-right: #9cc2e5 1pt solid; border-top-color: #f0f0f0; width: 155.85pt; border-bottom: #9cc2e5 1pt solid; border-left-color: #f0f0f0; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; padding-right: 5.4pt; background-color: transparent; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9cc2e5 .5pt; mso-border-top-themecolor: accent1; mso-border-left-alt: solid #9cc2e5 .5pt; mso-border-left-themecolor: accent1; mso-border-bottom-themecolor: accent1; mso-border-right-themecolor: accent1; mso-border-top-themetint: 153; mso-border-bottom-themetint: 153; mso-border-right-themetint: 153; mso-border-left-themetint: 153" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"></p>              <p><font color="#000000" size="3" face="Calibri">&#160;</font></p>           </td>         </tr>          <tr style="mso-yfti-irow: 2">           <td style="border-right: #9cc2e5 1pt solid; border-top-color: #f0f0f0; width: 155.8pt; background: #deeaf6; border-bottom: #9cc2e5 1pt solid; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; border-left: #9cc2e5 1pt solid; padding-right: 5.4pt; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9cc2e5 .5pt; mso-border-top-themecolor: accent1; mso-background-themecolor: accent1; mso-border-top-themetint: 153; mso-background-themetint: 51" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-yfti-cnfc: 68"><strong>PivotSensorDataTracker.Title</strong></p>           </td>            <td style="border-right: #9cc2e5 1pt solid; border-top-color: #f0f0f0; width: 155.85pt; background: #deeaf6; border-bottom: #9cc2e5 1pt solid; border-left-color: #f0f0f0; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; padding-right: 5.4pt; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9cc2e5 .5pt; mso-border-top-themecolor: accent1; mso-border-left-alt: solid #9cc2e5 .5pt; mso-border-left-themecolor: accent1; mso-border-bottom-themecolor: accent1; mso-background-themecolor: accent1; mso-border-right-themecolor: accent1; mso-border-top-themetint: 153; mso-background-themetint: 51; mso-border-bottom-themetint: 153; mso-border-right-themetint: 153; mso-border-left-themetint: 153" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-yfti-cnfc: 64">SENSOR DATA TRACKER</p>           </td>            <td style="border-right: #9cc2e5 1pt solid; border-top-color: #f0f0f0; width: 155.85pt; background: #deeaf6; border-bottom: #9cc2e5 1pt solid; border-left-color: #f0f0f0; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; padding-right: 5.4pt; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9cc2e5 .5pt; mso-border-top-themecolor: accent1; mso-border-left-alt: solid #9cc2e5 .5pt; mso-border-left-themecolor: accent1; mso-border-bottom-themecolor: accent1; mso-background-themecolor: accent1; mso-border-right-themecolor: accent1; mso-border-top-themetint: 153; mso-background-themetint: 51; mso-border-bottom-themetint: 153; mso-border-right-themetint: 153; mso-border-left-themetint: 153" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-yfti-cnfc: 64"><font color="#000000" size="3" face="Calibri">&#160;</font></p>           </td>         </tr>          <tr style="mso-yfti-irow: 3">           <td style="border-right: #9cc2e5 1pt solid; border-top-color: #f0f0f0; width: 155.8pt; border-bottom: #9cc2e5 1pt solid; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; border-left: #9cc2e5 1pt solid; padding-right: 5.4pt; background-color: transparent; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9cc2e5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-yfti-cnfc: 4"><strong>PivotShowReadings.Header</strong></p>           </td>            <td style="border-right: #9cc2e5 1pt solid; border-top-color: #f0f0f0; width: 155.85pt; border-bottom: #9cc2e5 1pt solid; border-left-color: #f0f0f0; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; padding-right: 5.4pt; background-color: transparent; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9cc2e5 .5pt; mso-border-top-themecolor: accent1; mso-border-left-alt: solid #9cc2e5 .5pt; mso-border-left-themecolor: accent1; mso-border-bottom-themecolor: accent1; mso-border-right-themecolor: accent1; mso-border-top-themetint: 153; mso-border-bottom-themetint: 153; mso-border-right-themetint: 153; mso-border-left-themetint: 153" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><font color="#000000" size="3" face="Calibri">View Readings</font></p>           </td>            <td style="border-right: #9cc2e5 1pt solid; border-top-color: #f0f0f0; width: 155.85pt; border-bottom: #9cc2e5 1pt solid; border-left-color: #f0f0f0; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; padding-right: 5.4pt; background-color: transparent; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9cc2e5 .5pt; mso-border-top-themecolor: accent1; mso-border-left-alt: solid #9cc2e5 .5pt; mso-border-left-themecolor: accent1; mso-border-bottom-themecolor: accent1; mso-border-right-themecolor: accent1; mso-border-top-themetint: 153; mso-border-bottom-themetint: 153; mso-border-right-themetint: 153; mso-border-left-themetint: 153" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"></p>           </td>         </tr>          <tr style="mso-yfti-irow: 4">           <td style="border-right: #9cc2e5 1pt solid; border-top-color: #f0f0f0; width: 155.8pt; background: #deeaf6; border-bottom: #9cc2e5 1pt solid; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; border-left: #9cc2e5 1pt solid; padding-right: 5.4pt; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9cc2e5 .5pt; mso-border-top-themecolor: accent1; mso-background-themecolor: accent1; mso-border-top-themetint: 153; mso-background-themetint: 51" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-yfti-cnfc: 68"><strong>SensorReadingTitle.Text</strong></p>           </td>            <td style="border-right: #9cc2e5 1pt solid; border-top-color: #f0f0f0; width: 155.85pt; background: #deeaf6; border-bottom: #9cc2e5 1pt solid; border-left-color: #f0f0f0; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; padding-right: 5.4pt; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9cc2e5 .5pt; mso-border-top-themecolor: accent1; mso-border-left-alt: solid #9cc2e5 .5pt; mso-border-left-themecolor: accent1; mso-border-bottom-themecolor: accent1; mso-background-themecolor: accent1; mso-border-right-themecolor: accent1; mso-border-top-themetint: 153; mso-background-themetint: 51; mso-border-bottom-themetint: 153; mso-border-right-themetint: 153; mso-border-left-themetint: 153" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-yfti-cnfc: 64">Sensor Readings</p>           </td>            <td style="border-right: #9cc2e5 1pt solid; border-top-color: #f0f0f0; width: 155.85pt; background: #deeaf6; border-bottom: #9cc2e5 1pt solid; border-left-color: #f0f0f0; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; padding-right: 5.4pt; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9cc2e5 .5pt; mso-border-top-themecolor: accent1; mso-border-left-alt: solid #9cc2e5 .5pt; mso-border-left-themecolor: accent1; mso-border-bottom-themecolor: accent1; mso-background-themecolor: accent1; mso-border-right-themecolor: accent1; mso-border-top-themetint: 153; mso-background-themetint: 51; mso-border-bottom-themetint: 153; mso-border-right-themetint: 153; mso-border-left-themetint: 153" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-yfti-cnfc: 64"></p>              <p><font color="#000000" size="3" face="Calibri">&#160;</font></p>           </td>         </tr>          <tr style="mso-yfti-irow: 5">           <td style="border-right: #9cc2e5 1pt solid; border-top-color: #f0f0f0; width: 155.8pt; border-bottom: #9cc2e5 1pt solid; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; border-left: #9cc2e5 1pt solid; padding-right: 5.4pt; background-color: transparent; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9cc2e5 .5pt; mso-border-top-themecolor: accent1; mso-border-top-themetint: 153" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-yfti-cnfc: 4"><strong>SubmitButton.Content</strong></p>           </td>            <td style="border-right: #9cc2e5 1pt solid; border-top-color: #f0f0f0; width: 155.85pt; border-bottom: #9cc2e5 1pt solid; border-left-color: #f0f0f0; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; padding-right: 5.4pt; background-color: transparent; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9cc2e5 .5pt; mso-border-top-themecolor: accent1; mso-border-left-alt: solid #9cc2e5 .5pt; mso-border-left-themecolor: accent1; mso-border-bottom-themecolor: accent1; mso-border-right-themecolor: accent1; mso-border-top-themetint: 153; mso-border-bottom-themetint: 153; mso-border-right-themetint: 153; mso-border-left-themetint: 153" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal">Submit Reading</p>           </td>            <td style="border-right: #9cc2e5 1pt solid; border-top-color: #f0f0f0; width: 155.85pt; border-bottom: #9cc2e5 1pt solid; border-left-color: #f0f0f0; padding-bottom: 0in; padding-top: 0in; padding-left: 5.4pt; padding-right: 5.4pt; background-color: transparent; mso-border-alt: solid #9cc2e5 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 153; mso-border-top-alt: solid #9cc2e5 .5pt; mso-border-top-themecolor: accent1; mso-border-left-alt: solid #9cc2e5 .5pt; mso-border-left-themecolor: accent1; mso-border-bottom-themecolor: accent1; mso-border-right-themecolor: accent1; mso-border-top-themetint: 153; mso-border-bottom-themetint: 153; mso-border-right-themetint: 153; mso-border-left-themetint: 153" valign="top" width="208">             <p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"></p>           </td>         </tr>       </tbody></table>   </font></p>  <p><font size="2">We’ll create a Portable Class Library (PCL) so that we may share the code that accesses the web API between various clients. Add a new project to the solution called<strong> SensorDataTracker.CommonLogic</strong> as shown below:</font></p>  <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/1665.image_2C3690AC.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/1665.image_5F00_2C3690AC.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/8512.image_thumb_6EA932E2.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/8512.image_5F00_thumb_5F00_6EA932E2.png" width="352" height="245" /></a></p>  <p><font size="2">When you create a PCL project, <strong>Visual Studio</strong> asks you which frameworks you wish to target. Make sure that the following options are selected in the dialog:</font></p>  <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/5468.image_20A0D36D.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/5468.image_5F00_20A0D36D.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/0601.image_thumb_0085C6B0.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/0601.image_5F00_thumb_5F00_0085C6B0.png" width="257" height="281" /></a></p>  <p><font size="2"><em>[Note that the last two boxes are visible only if you’ve <a href="https://xamarin.com/">Xamarin platform</a> installed on your box.]</em></font></p>  <p><font size="2">In Visual Studio, right-click the <strong>SensorDataTracker.CommonLogic </strong>project, and then select <strong>Manage NuGet Packages for solution</strong>. In the <strong>Manage NuGet Packages</strong> dialog, select <strong>Online</strong>-&gt;<strong>All</strong> in the left pane, make sure that <strong>Include Prerelease</strong> is selected in the dropdown and then search for “<strong>Microsoft.AspNet.WebApi.Client</strong>” as follows:</font></p>  <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/6557.image_606AB9F2.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/6557.image_5F00_606AB9F2.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/2350.image_thumb_032E8261.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/2350.image_5F00_thumb_5F00_032E8261.png" width="353" height="235" /></a></p>  <p><font size="2">Install the selected NuGet package that enables us to use <strong>System.Net.Http</strong> namespace for formatting and content negotiation.</font></p>  <p><font size="2">Create a new folder called <strong>Services</strong> in the <strong>SensorDataTracker.CommonLogic project</strong>, and add an <strong>IHttpService</strong> interface. Copy and paste the following code:</font></p>  <pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">namespace</span> SensorDataTracker.CommonLogic.Services
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">{
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> System.Threading;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">using</span> System.Threading.Tasks;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IHttpService
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        Task&lt;<span style="color: #0000ff">string</span>&gt; PostAsync(Uri postUri, <span style="color: #0000ff">string</span> postBody);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        Task&lt;<span style="color: #0000ff">string</span>&gt; GetAsync(Uri requestUri);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        Task&lt;<span style="color: #0000ff">string</span>&gt; PostAsync(Uri postUri, <span style="color: #0000ff">string</span> postBody, CancellationToken cancellationToken);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        Task&lt;<span style="color: #0000ff">string</span>&gt; GetAsync(Uri requestUri, CancellationToken cancellationToken);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">}</pre></pre>

<p><font size="2">The interface <strong>IHttpService</strong> declares four <strong><a href="https://msdn.microsoft.com/en-us/library/hh191443.aspx">async Task</a></strong> methods, two each for GET and POST API calls with the only difference of cancellation token being passed in the overloaded version.</font></p>

<p><font size="2">Let’s go ahead and write a generic implementation of this service interface. Go ahead and create a class called <strong>HttpService</strong> in the same folder and copy/paste the following code:</font></p>

<pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; width: 580px; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; background-color: #fbfbfb"><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">namespace</span> SensorDataTracker.CommonLogic.Services
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">{
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> System;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">using</span> System.Net.Http;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> System.Text;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">using</span> System.Threading;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> System.Threading.Tasks;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> HttpService : IHttpService
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> async Task&lt;<span style="color: #0000ff">string</span>&gt; PostAsync(Uri postUri, <span style="color: #0000ff">string</span> postBody)
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">return</span> await <span style="color: #0000ff">this</span>.PostAsync(postUri, postBody, CancellationToken.None);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        <span style="color: #0000ff">public</span> async Task&lt;<span style="color: #0000ff">string</span>&gt; GetAsync(Uri requestUri)
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            <span style="color: #0000ff">return</span> await <span style="color: #0000ff">this</span>.GetAsync(requestUri, CancellationToken.None);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> async Task&lt;<span style="color: #0000ff">string</span>&gt; PostAsync(Uri postUri, <span style="color: #0000ff">string</span> postBody, CancellationToken cancellationToken)
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">using</span> (var httpClient = <span style="color: #0000ff">new</span> HttpClient())
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                var postData = <span style="color: #0000ff">new</span> StringContent(postBody, Encoding.UTF8, &quot;<span style="color: #8b0000">application/json</span>&quot;);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                var response = await httpClient.PostAsync(postUri, postData, cancellationToken);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                response.EnsureSuccessStatusCode();
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                <span style="color: #0000ff">return</span> await response.Content.ReadAsStringAsync();
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        <span style="color: #0000ff">public</span> async Task&lt;<span style="color: #0000ff">string</span>&gt; GetAsync(Uri requestUri, CancellationToken cancellationToken)
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            <span style="color: #0000ff">using</span> (var httpClient = <span style="color: #0000ff">new</span> HttpClient())
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                var response = await httpClient.GetAsync(requestUri, cancellationToken);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                response.EnsureSuccessStatusCode();
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                <span style="color: #0000ff">return</span> await response.Content.ReadAsStringAsync();
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">}</pre></pre>

<p><font size="2"></font></p>

<p><font size="2">As you can see, we’re using <strong>HttpClient</strong> (added via NuGet package reference) class to get an async JSON response from the web service and similarly posting a JSON payload to the post controller method of the API. Pretty straightforward!</font></p>

<p><font size="2">Since we’re encapsulating service interaction logic in a PCL, we might need to do some refactoring here in order to make the models definition available to the clients such as Windows Phone. Though in a real world project, you might consider moving the model to another shared library and hooking up with the service API using a <a href="https://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application">repository pattern</a> that allows nice separation of concerns with the ability to write unit tests against mock representation of data. However for this exercise, moving the <strong>Models</strong> folder (that has <strong>SendorReading</strong> and <strong>ResultPage</strong> classes) from web API project to PCL project suffices. After the move, <strong>SensorDataTracker.CommonLogic</strong> should look like below:</font></p>

<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/7140.image_150B162E.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/7140.image_5F00_150B162E.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/2843.image_thumb_22DD5C29.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/2843.image_5F00_thumb_5F00_22DD5C29.png" width="287" height="196" /></a></p>

<p><font size="2">Don’t forget that you’ll need to make appropriate namespace corrections and add a reference to PCL project in the web API project (<a href="https://blogs.msdn.com/b/kirpas/archive/2013/08/06/writing-a-simple-implementation-of-dependency-injection-in-mvc-4-web-api-with-net-framework-4-5.aspx">SensorDataTracker</a>) so that models are visible to the latter. Likewise you’ll need to add reference to PCL project in the test project (<a href="https://blogs.msdn.com/b/kirpas/archive/2014/05/14/writing-bdd-style-unit-tests-to-verify-the-mvc-based-web-api.aspx">SensorDataTracker.Test</a>) and fix the namespaces, relatively trivial tasks towards the cost of flexibility!</font></p>

<p><font size="2">Since the generic service classes are in place, let’s write another layer that makes use of the generic service methods to retrieve and post sensor data to the web API. Under <strong>Services</strong> folder, add a new interface <strong>ISensorDataTrackerService</strong> and copy/paste the following code:</font></p>

<pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">namespace</span> SensorDataTracker.CommonLogic.Services
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">{
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> SensorDataTracker.CommonLogic.Models;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">using</span> System.Threading.Tasks;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">interface</span> ISensorDataTrackerService
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        Task&lt;ResultPage&lt;SensorReading&gt;&gt; GetSensorReadingsAsync();
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        Task&lt;ResultPage&lt;SensorReading&gt;&gt; GetSensorReadingsAsync(<span style="color: #0000ff">int</span> pageNumber);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        Task PostSensorReadingAsync(SensorReading reading);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">}</pre></pre>

<p><font size="2">We’ve declared three methods, two for retrieving sensor readings and the third one for posting the sensor reading. Let’s implement them in a class called <strong>SensorDataTrackerService</strong> under the <strong>Services</strong> folder as before:</font></p>

<pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; width: 580px; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; background-color: #fbfbfb"><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">namespace</span> SensorDataTracker.CommonLogic.Services
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">{
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> System;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">using</span> System.Globalization;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> System.Threading.Tasks;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> Newtonsoft.Json;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> SensorDataTracker.CommonLogic.Models;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> SensorDataTrackerService : ISensorDataTrackerService
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> IHttpService _httpService;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">int</span> DefaultPageIndex = 1;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> ServerAddress = &quot;<span style="color: #8b0000">https://sensordatatracker.cloudapp.net</span>&quot;;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> <span style="color: #0000ff">string</span> sensorReadingBaseUrl = <span style="color: #0000ff">string</span>.Format(
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            CultureInfo.InvariantCulture,
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            &quot;<span style="color: #8b0000">{0}/api/SensorReading</span>&quot;,
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            ServerAddress);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> PaginationQueryString = &quot;<span style="color: #8b0000">{0}?pageNumber={1}</span>&quot;;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        <span style="color: #0000ff">public</span> SensorDataTrackerService(IHttpService httpService)
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            <span style="color: #0000ff">this</span>._httpService = httpService;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> async Task&lt;ResultPage&lt;SensorReading&gt;&gt; GetSensorReadingsAsync()
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">return</span> await <span style="color: #0000ff">this</span>.GetSensorReadingsAsync(DefaultPageIndex);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        <span style="color: #0000ff">public</span> async Task&lt;ResultPage&lt;SensorReading&gt;&gt; GetSensorReadingsAsync(<span style="color: #0000ff">int</span> pageNumber)
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            var requestUri = <span style="color: #0000ff">new</span> Uri(<span style="color: #0000ff">string</span>.Format(PaginationQueryString, <span style="color: #0000ff">this</span>.sensorReadingBaseUrl, pageNumber));
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            var responseContent = await <span style="color: #0000ff">this</span>._httpService.GetAsync(requestUri);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            <span style="color: #0000ff">return</span> JsonConvert.DeserializeObject&lt;ResultPage&lt;SensorReading&gt;&gt;(responseContent);            
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> async Task PostSensorReadingAsync(SensorReading reading)
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            var postUri = <span style="color: #0000ff">new</span> Uri(sensorReadingBaseUrl);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            await <span style="color: #0000ff">this</span>._httpService.PostAsync(postUri, JsonConvert.SerializeObject(reading));
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">}</pre></pre>

<p><font size="2">Let’s understand the code above before we inch ahead. <strong>SensorReadingTrackerService</strong> has a constructor that required a reference to an <strong>IHttpService</strong> that we defined previously. <strong>ServerAddress</strong> points to the web API location and <strong>sensorReadingBaseUrl</strong> contains the additional prefix for the GET and POST methods of the service controller. <strong>PaginationQueryString</strong> is concatenated to the <strong>sensorReadingBaseUrl</strong> that allows us to pass in a page number. <strong>GetSensorReadingsAsync(int pageNumber) </strong>composes a <strong>Uri</strong> with the given page number and asynchronously calls the <strong>GetAsync</strong> method of <strong>HttpService</strong>. It then uses <strong>DeserializeObject</strong> method of <strong>JsonConvert</strong> class to cast the response into a <strong>ResultPage</strong> of <strong>SensorReading</strong> objects before returning. Similarly <strong>PostSensorReadingAsync</strong> serializes a <strong>SensorReading</strong> and posts it to the service. </font></p>

<p><font size="2">With PCL in place, it’s time to revisit the Windows Phone 8.1 client. Add a reference to PCL project in Windows Phone 8.1 client project.</font></p>

<p><font size="2">Double click <strong>MainPage.xaml</strong> to open the designer and</font><font size="2"> paste the following code, overwriting the existing:</font></p>

<pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; width: 580px; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; background-color: #fbfbfb"><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">&lt;</span><span style="color: #800000">Page</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #ff0000">x</span>:<span style="color: #ff0000">Class</span>=<span style="color: #0000ff">&quot;SensorDataTracker.WindowsPhone.Client.MainPage&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #ff0000">xmlns</span>=<span style="color: #0000ff">&quot;https://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #ff0000">xmlns</span>:<span style="color: #ff0000">x</span>=<span style="color: #0000ff">&quot;https://schemas.microsoft.com/winfx/2006/xaml&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #ff0000">xmlns</span>:<span style="color: #ff0000">d</span>=<span style="color: #0000ff">&quot;https://schemas.microsoft.com/expression/blend/2008&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #ff0000">xmlns</span>:<span style="color: #ff0000">mc</span>=<span style="color: #0000ff">&quot;https://schemas.openxmlformats.org/markup-compatibility/2006&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #ff0000">mc</span>:<span style="color: #ff0000">Ignorable</span>=<span style="color: #0000ff">&quot;d&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #ff0000">Background</span>=<span style="color: #0000ff">&quot;{ThemeResource ApplicationPageBackgroundThemeBrush}&quot;</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">&lt;</span><span style="color: #800000">Grid.RowDefinitions</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            <span style="color: #0000ff">&lt;</span><span style="color: #800000">RowDefinition</span> <span style="color: #ff0000">Height</span>=<span style="color: #0000ff">&quot;*&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">RowDefinition</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">&lt;</span><span style="color: #800000">RowDefinition</span> <span style="color: #ff0000">Height</span>=<span style="color: #0000ff">&quot;30&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">RowDefinition</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Grid.RowDefinitions</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">&lt;</span><span style="color: #800000">Pivot</span> <span style="color: #ff0000">Title</span>=<span style="color: #0000ff">&quot;&quot;</span> <span style="color: #ff0000">x</span>:<span style="color: #ff0000">Uid</span>=<span style="color: #0000ff">&quot;PivotSensorDataTracker&quot;</span> <span style="color: #ff0000">Grid</span>.<span style="color: #ff0000">Row</span>=<span style="color: #0000ff">&quot;0&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">               <span style="color: #ff0000">x</span>:<span style="color: #ff0000">Name</span>=<span style="color: #0000ff">&quot;PivotContainer&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">               <span style="color: #ff0000">SelectionChanged</span>=<span style="color: #0000ff">&quot;PivotContainer_OnSelectionChanged&quot;</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            <span style="color: #0000ff">&lt;</span><span style="color: #800000">PivotItem</span> <span style="color: #ff0000">Header</span>=<span style="color: #0000ff">&quot;PivotItem 0&quot;</span> <span style="color: #ff0000">x</span>:<span style="color: #ff0000">Uid</span>=<span style="color: #0000ff">&quot;PivotPostReadings&quot;</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                <span style="color: #0000ff">&lt;</span><span style="color: #800000">Grid</span> <span style="color: #ff0000">Margin</span>=<span style="color: #0000ff">&quot;12,0,12,0&quot;</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                    <span style="color: #0000ff">&lt;</span><span style="color: #800000">StackPanel</span> <span style="color: #ff0000">Orientation</span>=<span style="color: #0000ff">&quot;Vertical&quot;</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                        <span style="color: #0000ff">&lt;</span><span style="color: #800000">TextBlock</span> <span style="color: #ff0000">Text</span>=<span style="color: #0000ff">&quot;&quot;</span> <span style="color: #ff0000">x</span>:<span style="color: #ff0000">Uid</span>=<span style="color: #0000ff">&quot;SensorReadingTitle&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                   <span style="color: #ff0000">Margin</span>=<span style="color: #0000ff">&quot;10,0,0,0&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                   <span style="color: #ff0000">FontFamily</span>=<span style="color: #0000ff">&quot;Verdana&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                   <span style="color: #ff0000">FontSize</span>=<span style="color: #0000ff">&quot;20&quot;</span><span style="color: #0000ff">/&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                        <span style="color: #0000ff">&lt;</span><span style="color: #800000">TextBlock</span> 
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                   <span style="color: #ff0000">x</span>:<span style="color: #ff0000">Name</span>=<span style="color: #0000ff">&quot;TextSensorReadingId&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                   <span style="color: #ff0000">FontFamily</span>=<span style="color: #0000ff">&quot;Verdana&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                   <span style="color: #ff0000">FontSize</span>=<span style="color: #0000ff">&quot;80&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                   <span style="color: #ff0000">FontStretch</span>=<span style="color: #0000ff">&quot;UltraExpanded&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                   <span style="color: #ff0000">FontWeight</span>=<span style="color: #0000ff">&quot;ExtraBlack&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                   <span style="color: #ff0000">HorizontalAlignment</span>=<span style="color: #0000ff">&quot;Center&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                   <span style="color: #ff0000">Margin</span>=<span style="color: #0000ff">&quot;10&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                   <span style="color: #ff0000">Foreground</span>=<span style="color: #0000ff">&quot;Khaki&quot;</span> <span style="color: #0000ff">/&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                        <span style="color: #0000ff">&lt;</span><span style="color: #800000">Button</span> <span style="color: #ff0000">Content</span>=<span style="color: #0000ff">&quot;&quot;</span> <span style="color: #ff0000">x</span>:<span style="color: #ff0000">Uid</span>=<span style="color: #0000ff">&quot;SubmitButton&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                <span style="color: #ff0000">Click</span>=<span style="color: #0000ff">&quot;SubmitButton_OnClick&quot;</span> <span style="color: #0000ff">/&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">StackPanel</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">&lt;/</span><span style="color: #800000">PivotItem</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            <span style="color: #0000ff">&lt;</span><span style="color: #800000">PivotItem</span> <span style="color: #ff0000">Header</span>=<span style="color: #0000ff">&quot;PivotItem 1&quot;</span> <span style="color: #ff0000">x</span>:<span style="color: #ff0000">Uid</span>=<span style="color: #0000ff">&quot;PivotShowReadings&quot;</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                <span style="color: #0000ff">&lt;</span><span style="color: #800000">Grid</span> <span style="color: #ff0000">Margin</span>=<span style="color: #0000ff">&quot;12,0,12,0&quot;</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                    <span style="color: #0000ff">&lt;</span><span style="color: #800000">StackPanel</span> <span style="color: #ff0000">Orientation</span>=<span style="color: #0000ff">&quot;Vertical&quot;</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                        <span style="color: #0000ff">&lt;</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                            <span style="color: #0000ff">&lt;</span><span style="color: #800000">TextBlock</span> <span style="color: #ff0000">Text</span>=<span style="color: #0000ff">&quot;&quot;</span> <span style="color: #ff0000">x</span>:<span style="color: #ff0000">Uid</span>=<span style="color: #0000ff">&quot;SensorReadingTitle&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                   <span style="color: #ff0000">Margin</span>=<span style="color: #0000ff">&quot;10,0,0,0&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                   <span style="color: #ff0000">FontFamily</span>=<span style="color: #0000ff">&quot;Verdana&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                   <span style="color: #ff0000">FontSize</span>=<span style="color: #0000ff">&quot;20&quot;</span><span style="color: #0000ff">/&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                        <span style="color: #0000ff">&lt;</span><span style="color: #800000">ItemsControl</span> <span style="color: #ff0000">Margin</span>=<span style="color: #0000ff">&quot;10&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                      <span style="color: #ff0000">Name</span>=<span style="color: #0000ff">&quot;SensorReadingList&quot;</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                            <span style="color: #0000ff">&lt;</span><span style="color: #800000">ItemsControl.Template</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                <span style="color: #0000ff">&lt;</span><span style="color: #800000">ControlTemplate</span> <span style="color: #ff0000">TargetType</span>=<span style="color: #0000ff">&quot;ItemsControl&quot;</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Border</span> <span style="color: #ff0000">BorderBrush</span>=<span style="color: #0000ff">&quot;Aqua&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                            <span style="color: #ff0000">BorderThickness</span>=<span style="color: #0000ff">&quot;1&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                            <span style="color: #ff0000">CornerRadius</span>=<span style="color: #0000ff">&quot;10&quot;</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                        <span style="color: #0000ff">&lt;</span><span style="color: #800000">ItemsPresenter</span> <span style="color: #0000ff">/&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Border</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                <span style="color: #0000ff">&lt;/</span><span style="color: #800000">ControlTemplate</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                            <span style="color: #0000ff">&lt;/</span><span style="color: #800000">ItemsControl.Template</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                            <span style="color: #0000ff">&lt;</span><span style="color: #800000">ItemsControl.ItemTemplate</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                <span style="color: #0000ff">&lt;</span><span style="color: #800000">DataTemplate</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                        <span style="color: #0000ff">&lt;</span><span style="color: #800000">Ellipse</span> <span style="color: #ff0000">Fill</span>=<span style="color: #0000ff">&quot;Silver&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                                 <span style="color: #ff0000">Opacity</span>=<span style="color: #0000ff">&quot;0.50&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                                 <span style="color: #ff0000">Stroke</span>=<span style="color: #0000ff">&quot;DarkSlateBlue&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                                 <span style="color: #ff0000">StrokeThickness</span>=<span style="color: #0000ff">&quot;2&quot;</span> <span style="color: #0000ff">/&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                        <span style="color: #0000ff">&lt;</span><span style="color: #800000">StackPanel</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                            <span style="color: #0000ff">&lt;</span><span style="color: #800000">TextBlock</span> <span style="color: #ff0000">Margin</span>=<span style="color: #0000ff">&quot;3,3,3,0&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                                       <span style="color: #ff0000">FontSize</span>=<span style="color: #0000ff">&quot;20&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                                       <span style="color: #ff0000">HorizontalAlignment</span>=<span style="color: #0000ff">&quot;Center&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                                       <span style="color: #ff0000">Text</span>=<span style="color: #0000ff">&quot;{Binding Path=Id}&quot;</span> <span style="color: #0000ff">/&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                            <span style="color: #0000ff">&lt;</span><span style="color: #800000">TextBlock</span> <span style="color: #ff0000">Margin</span>=<span style="color: #0000ff">&quot;3,0,3,7&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                                       <span style="color: #ff0000">FontSize</span>=<span style="color: #0000ff">&quot;20&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                                       <span style="color: #ff0000">HorizontalAlignment</span>=<span style="color: #0000ff">&quot;Center&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                                       <span style="color: #ff0000">Text</span>=<span style="color: #0000ff">&quot;{Binding Path=CreateDate}&quot;</span> <span style="color: #0000ff">/&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">StackPanel</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                <span style="color: #0000ff">&lt;/</span><span style="color: #800000">DataTemplate</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                            <span style="color: #0000ff">&lt;/</span><span style="color: #800000">ItemsControl.ItemTemplate</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">ItemsControl</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                        <span style="color: #0000ff">&lt;</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                            <span style="color: #0000ff">&lt;</span><span style="color: #800000">Grid.RowDefinitions</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                <span style="color: #0000ff">&lt;</span><span style="color: #800000">RowDefinition</span> <span style="color: #ff0000">Height</span>=<span style="color: #0000ff">&quot;30&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">RowDefinition</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                <span style="color: #0000ff">&lt;</span><span style="color: #800000">RowDefinition</span> <span style="color: #ff0000">Height</span>=<span style="color: #0000ff">&quot;50&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">RowDefinition</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                            <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Grid.RowDefinitions</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                            <span style="color: #0000ff">&lt;</span><span style="color: #800000">Grid.ColumnDefinitions</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                <span style="color: #0000ff">&lt;</span><span style="color: #800000">ColumnDefinition</span> <span style="color: #ff0000">Width</span>=<span style="color: #0000ff">&quot;120&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">ColumnDefinition</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                <span style="color: #0000ff">&lt;</span><span style="color: #800000">ColumnDefinition</span> <span style="color: #ff0000">Width</span>=<span style="color: #0000ff">&quot;*&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">ColumnDefinition</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                <span style="color: #0000ff">&lt;</span><span style="color: #800000">ColumnDefinition</span> <span style="color: #ff0000">Width</span>=<span style="color: #0000ff">&quot;120&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">ColumnDefinition</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                            <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Grid.ColumnDefinitions</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                            <span style="color: #0000ff">&lt;</span><span style="color: #800000">TextBlock</span> <span style="color: #ff0000">x</span>:<span style="color: #ff0000">Name</span>=<span style="color: #0000ff">&quot;PageInfo&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                       <span style="color: #ff0000">x</span>:<span style="color: #ff0000">Uid</span>=<span style="color: #0000ff">&quot;PagingControl&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                       <span style="color: #ff0000">Grid</span>.<span style="color: #ff0000">Column</span>=<span style="color: #0000ff">&quot;0&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                       <span style="color: #ff0000">Grid</span>.<span style="color: #ff0000">Row</span>=<span style="color: #0000ff">&quot;0&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                       <span style="color: #ff0000">Grid</span>.<span style="color: #ff0000">ColumnSpan</span>=<span style="color: #0000ff">&quot;3&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                       <span style="color: #ff0000">HorizontalAlignment</span>=<span style="color: #0000ff">&quot;Center&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                       <span style="color: #ff0000">VerticalAlignment</span>=<span style="color: #0000ff">&quot;Center&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                       <span style="color: #ff0000">FontSize</span>=<span style="color: #0000ff">&quot;16&quot;</span> <span style="color: #0000ff">/&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                            <span style="color: #0000ff">&lt;</span><span style="color: #800000">Button</span> <span style="color: #ff0000">x</span>:<span style="color: #ff0000">Name</span>=<span style="color: #0000ff">&quot;PrevPage&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                    <span style="color: #ff0000">Grid</span>.<span style="color: #ff0000">Column</span>=<span style="color: #0000ff">&quot;0&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                    <span style="color: #ff0000">Grid</span>.<span style="color: #ff0000">Row</span>=<span style="color: #0000ff">&quot;1&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                    <span style="color: #ff0000">IsEnabled</span>=<span style="color: #0000ff">&quot;False&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                    <span style="color: #ff0000">Content</span>=<span style="color: #0000ff">&quot;&amp;#xE100;&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                    <span style="color: #ff0000">FontFamily</span>=<span style="color: #0000ff">&quot;Segoe UI Symbol&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                    <span style="color: #ff0000">FontSize</span>=<span style="color: #0000ff">&quot;20&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                    <span style="color: #ff0000">Click</span>=<span style="color: #0000ff">&quot;BrowsePage_OnClick&quot;</span> <span style="color: #0000ff">/&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                            <span style="color: #0000ff">&lt;</span><span style="color: #800000">Button</span> <span style="color: #ff0000">x</span>:<span style="color: #ff0000">Name</span>=<span style="color: #0000ff">&quot;NextPage&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                    <span style="color: #ff0000">Grid</span>.<span style="color: #ff0000">Column</span>=<span style="color: #0000ff">&quot;2&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                    <span style="color: #ff0000">Grid</span>.<span style="color: #ff0000">Row</span>=<span style="color: #0000ff">&quot;1&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                    <span style="color: #ff0000">Content</span>=<span style="color: #0000ff">&quot;&amp;#xE101;&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                    <span style="color: #ff0000">FontFamily</span>=<span style="color: #0000ff">&quot;Segoe UI Symbol&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                    <span style="color: #ff0000">FontSize</span>=<span style="color: #0000ff">&quot;20&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                    <span style="color: #ff0000">Click</span>=<span style="color: #0000ff">&quot;BrowsePage_OnClick&quot;</span> <span style="color: #0000ff">/&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">StackPanel</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">&lt;/</span><span style="color: #800000">PivotItem</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Pivot</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        <span style="color: #0000ff">&lt;</span><span style="color: #800000">StackPanel</span> <span style="color: #ff0000">VerticalAlignment</span>=<span style="color: #0000ff">&quot;Top&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                    <span style="color: #ff0000">Grid</span>.<span style="color: #ff0000">Row</span>=<span style="color: #0000ff">&quot;1&quot;</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            <span style="color: #0000ff">&lt;</span><span style="color: #800000">ProgressBar</span> <span style="color: #ff0000">Margin</span>=<span style="color: #0000ff">&quot;10&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                         <span style="color: #ff0000">x</span>:<span style="color: #ff0000">Name</span>=<span style="color: #0000ff">&quot;NetworkActivity&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                         <span style="color: #ff0000">IsIndeterminate</span>=<span style="color: #0000ff">&quot;False&quot;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                         <span style="color: #ff0000">Height</span>=<span style="color: #0000ff">&quot;15&quot;</span> <span style="color: #0000ff">/&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">StackPanel</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Grid</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">Page</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre></pre>

<p><font size="2">In the XAML markup, we’re creating a <a href="https://msdn.microsoft.com/en-us/library/windowsphone/develop/ff941098(v=vs.105).aspx">Pivot control</a> with two pivot items, first for posting the sensor readings and second for viewing the pages of sensor readings.</font></p>

<p><font size="2">In the first pivot item, a grid control lays out a couple of text blocks to display a title and a large random sensor reading, finishing with a button to submit the reading. In the second pivot item, I tried to be a little bit artistic (and evidently I’m not good at it but at least I tried <img class="wlEmoticon wlEmoticon-nerdsmile" style="border-top-style: none; border-bottom-style: none; border-right-style: none; border-left-style: none" alt="Nerd smile" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/2671.wlEmoticon-nerdsmile_49AB7269.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/2671.wlEmoticon_2D00_nerdsmile_5F00_49AB7269.png" />) by creating a rounded rectangle with an ellipse inside it enclosing the sensor reading. A pager control follows the visually appealing rendition that displays information about the number of records out of total and the number of page we’re on, succeeded by a row containing previous and next buttons. </font><font size="2">A progress bar is included that is outside the pivot control (to track network activity) and thus is available on both pages.</font></p>

<p><font size="2">Okay…let’s now wire up the event handlers to enliven the dead UI elements! Open <strong>MainPage.xaml.cs</strong> and copy/paste the following code:</font></p>

<pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; width: 580px; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; background-color: #fbfbfb"><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">namespace</span> SensorDataTracker.WindowsPhone.Client
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">{
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> SensorDataTracker.CommonLogic.Models;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">using</span> SensorDataTracker.CommonLogic.Services;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> System;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">using</span> System.Collections.Generic;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> System.Globalization;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">using</span> Windows.UI.Xaml;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> Windows.UI.Xaml.Controls;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">using</span> Windows.UI.Xaml.Navigation;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">sealed</span> partial <span style="color: #0000ff">class</span> MainPage : Page
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> Random _readingGenerator;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">int</span> MaxNum = 10000;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span> _currentPage = 1;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">double</span> PageSize = 5;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> IDictionary&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">int</span>&gt; _buttonPageActions = <span style="color: #0000ff">new</span> Dictionary&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">int</span>&gt;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                {&quot;<span style="color: #8b0000">PrevPage</span>&quot;,-1},
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                {&quot;<span style="color: #8b0000">NextPage</span>&quot;,1},
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            };
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> SensorDataTrackerService _sensorService = <span style="color: #0000ff">new</span> SensorDataTrackerService(<span style="color: #0000ff">new</span> HttpService());
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> _pagerFormatString;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> MainPage()
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">this</span>.InitializeComponent();
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">this</span>.NavigationCacheMode = NavigationCacheMode.Required;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            _readingGenerator = <span style="color: #0000ff">new</span> Random();
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> PivotContainer_OnSelectionChanged(<span style="color: #0000ff">object</span> sender, SelectionChangedEventArgs e)
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">if</span> (PivotContainer.SelectedIndex == 1)
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                <span style="color: #0000ff">this</span>.LoadSensorReadings();
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">else</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                <span style="color: #0000ff">this</span>.ShowNextReading();
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">private</span> async <span style="color: #0000ff">void</span> LoadSensorReadings()
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            NetworkActivity.IsIndeterminate = <span style="color: #0000ff">true</span>;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            var sensorReadings = await _sensorService.GetSensorReadingsAsync(_currentPage);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">this</span>.UpdateControls(sensorReadings);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            NetworkActivity.IsIndeterminate = <span style="color: #0000ff">false</span>;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> ShowNextReading()
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            TextSensorReadingId.Text = <span style="color: #0000ff">string</span>.Format(CultureInfo.CurrentCulture, &quot;<span style="color: #8b0000">{0}</span>&quot;, _readingGenerator.Next(0, MaxNum));
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        <span style="color: #0000ff">private</span> async <span style="color: #0000ff">void</span> SubmitButton_OnClick(<span style="color: #0000ff">object</span> sender, RoutedEventArgs e)
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            var sensorReading = <span style="color: #0000ff">new</span> SensorReading()
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                    Id = Convert.ToInt32(TextSensorReadingId.Text),
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                                    CreateDate = DateTime.Now
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                                };
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            NetworkActivity.IsIndeterminate = <span style="color: #0000ff">true</span>;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            await _sensorService.PostSensorReadingAsync(sensorReading);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            <span style="color: #0000ff">this</span>.ShowNextReading();
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            NetworkActivity.IsIndeterminate = <span style="color: #0000ff">false</span>;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> BrowsePage_OnClick(<span style="color: #0000ff">object</span> sender, RoutedEventArgs e)
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            var button = (Button)sender;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            var key = button.Name;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            var increment = <span style="color: #0000ff">this</span>._buttonPageActions.ContainsKey(key) ? <span style="color: #0000ff">this</span>._buttonPageActions[key] : 0;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            _currentPage += increment;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            LoadSensorReadings();
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> UpdateControls(ResultPage&lt;SensorReading&gt; resultPage )
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            var totalCount = Convert.ToInt32(resultPage.TotalCount);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            var totalNumberOfPages = (totalCount &lt;= PageSize)
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                ? 1
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                : Math.Ceiling(totalCount / PageSize);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            <span style="color: #0000ff">if</span> (<span style="color: #0000ff">string</span>.IsNullOrWhiteSpace(_pagerFormatString)) _pagerFormatString = PageInfo.Text;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            PageInfo.Text = <span style="color: #0000ff">string</span>.Format(CultureInfo.CurrentCulture,
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                _pagerFormatString,
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                resultPage.TotalCount,
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                _currentPage,
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                totalNumberOfPages);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            PrevPage.IsEnabled = _currentPage &gt; 1;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            NextPage.IsEnabled = _currentPage &lt; totalNumberOfPages;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            SensorReadingList.ItemsSource = resultPage.Results;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">}
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre></pre>

<p><font size="2">Let’s start dissecting the code from beginning. Fields include:</font></p>

<ol>
  <li><font size="2">A random reading generator that generates a random number under a specified <strong>MaxNum</strong>.</font> </li>

  <li><font size="2">Current page number tracker.</font> </li>

  <li><font size="2">Number of records to fetch per page indicated by <strong>PageSize</strong>.</font> </li>

  <li><font size="2">A dictionary to keep track of browse button clicks, –1 for previous and 1 for next.</font> </li>

  <li><font size="2"><strong>SensorDataTrackerService</strong> instance to invoke the service methods.</font> </li>
</ol>

<p><font size="2">Event handler <strong>PivotContainer_OnSelectionChanged</strong>, handles the cycling between pivot items and based on the index either readings are fetched or a new reading is generated for submission.</font></p>

<p><font size="2"><strong>LoadSensorReadings</strong> fetches the readings from the web API asynchronously and updates the controls including the progress indicator.</font></p>

<p><font size="2">Let’s take a closer look at <strong>UpdateControls</strong> method:</font></p>

<pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> UpdateControls(ResultPage&lt;SensorReading&gt; resultPage )
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            var totalCount = Convert.ToInt32(resultPage.TotalCount);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            var totalNumberOfPages = (totalCount &lt;= PageSize)
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                ? 1
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                : Math.Ceiling(totalCount / PageSize);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            <span style="color: #0000ff">if</span> (<span style="color: #0000ff">string</span>.IsNullOrWhiteSpace(_pagerFormatString)) _pagerFormatString = PageInfo.Text;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            PageInfo.Text = <span style="color: #0000ff">string</span>.Format(CultureInfo.CurrentCulture,
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                _pagerFormatString,
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                resultPage.TotalCount,
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                _currentPage,
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                totalNumberOfPages);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            PrevPage.IsEnabled = _currentPage &gt; 1;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            NextPage.IsEnabled = _currentPage &lt; totalNumberOfPages;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            SensorReadingList.ItemsSource = resultPage.Results;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }</pre></pre>

<p><font size="2"><strong>UpdateControls</strong> gets a JSON deserialized <strong>ResultPage</strong> that holds <strong>SensorReading</strong> objects in the form of <strong>Results</strong> collection and a <strong>TotalCount</strong> of records, the latter being required to calculate the number of pages. <strong>TotalCount</strong> is divided by <strong>PageSize</strong> to obtain the number of pages if it’s greater than the latter else one page is assumed. Pager format string is initialized with the resource string <strong>PageInfo.Text </strong>and respective placeholders are replaced with total count, current page and total number of pages, e.g. <em><strong>Total: 12 – Showing page 1 of 3</strong>. </em>Based on the current page and total number of pages, prev and next buttons are enabled or disabled using a simple calculation. Finally the <strong>Results</strong> collection is assigned as the source of items for <strong>ItemsControl</strong> declared in XAML.</font></p>

<p><font size="2">Event handler <strong>SubmitButton_OnClick</strong> creates a new <strong>SensorReading</strong> with the random number as <strong>Id</strong> and current date as <strong>CreateDate</strong> that is posted asynchronously to the web API.</font></p>

<p><font size="2">Event handler <strong>BrowsePage_OnClick</strong> handles the paging between sensor readings pages. First it determines which button was clicked based on the sender’s <strong>Name</strong> property, looks it up as a key in the dictionary and then uses the corresponding value to advance the current page forward or backward.</font></p>

<p><font size="2">That pretty much sums up the Windows Phone 8.1 client functionality! </font><font size="2">There is an inherent issue with Windows Phone emulator. Since Windows Phone 8.1 emulator employs a virtual machine under the covers, it has the same IP address as the host machine - so all the references that use the localhost IP address end up in loopback. To get around this issue, you need to configure the IIS express or IIS server so that it may distinguish between the requests coming from the emulator and local machine. See <a href="https://curah.microsoft.com/58063/how-to-configure-windows-phone-8-emulator-to-connect-to-web-api-or-wcf-service-deployed-on-local-iis-server-or-iis-express">this article</a> on <a href="https://curah.microsoft.com/">curah.microsoft.com</a> for some possible workarounds. <em>However on a corporate domain, the emulator appears as a separate network device that is not joined to the domain. As a result, you may also have to get an exception from your IT department before the emulator can connect to services that are running on the domain-joined development computer. </em>D</font><font size="2">ue to the aforementioned nuisance <img class="wlEmoticon wlEmoticon-angrysmile" style="border-top-style: none; border-bottom-style: none; border-right-style: none; border-left-style: none" alt="Angry smile" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/1185.wlEmoticon-angrysmile_7BA312F3.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/1185.wlEmoticon_2D00_angrysmile_5F00_7BA312F3.png" /> I prefer to deploy my web API to <a href="https://manage.windowsazure.com">Microsoft Azure Cloud</a> which is trivial comparatively!</font></p>

<p><font size="2">Let’s convert the web API that we created previously to an Azure Cloud project. Right click on <strong>SensorDataTracker</strong> project and choose <strong>Convert—&gt;Convert to Windows Azure Cloud Service Project</strong> option:</font></p>

<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/5327.image_428C35F1.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/5327.image_5F00_428C35F1.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/1108.image_thumb_3762ABA7.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/1108.image_5F00_thumb_5F00_3762ABA7.png" width="244" height="62" /></a></p>

<p><font size="2">A new cloud wrapper project gets created as shown below:</font></p>

<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/8055.image_17479EEA.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/8055.image_5F00_17479EEA.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/2287.image_thumb_5E30C1E7.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/2287.image_5F00_thumb_5F00_5E30C1E7.png" width="244" height="88" /></a></p>

<p><font size="2">Right click on <strong>SensorDataTracker.Azure</strong> project and choose <strong>Publish</strong> option. The following dialog appears:</font></p>

<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/1682.image_10286272.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/1682.image_5F00_10286272.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/6472.image_thumb_04FED828.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/6472.image_5F00_thumb_5F00_04FED828.png" width="244" height="165" /></a></p>

<p><font size="2">Choose your Azure subscription and a new or existing Cloud service to deploy to (in the next dialog shown below) and click on the <strong>Publish</strong> button. You may optionally save the target profile giving it a name of your choice or leave the defaults.</font></p>

<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/4338.image_4BE7FB25.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/4338.image_5F00_4BE7FB25.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/2275.image_thumb_2BCCEE68.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/2275.image_5F00_thumb_5F00_2BCCEE68.png" width="244" height="168" /></a></p>

<p><font size="2">As you may’ve already noted, I used the service URL of the Azure deployed web API service in <strong>SensorDataTrackerService </strong>class. Set Windows Phone 8.1 client as the Startup project and click on emulator 8.1 option to deploy the package to Windows Phone 8.1 emulator and start the app:</font></p>

<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/5417.image_0BB1E1AB.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/5417.image_5F00_0BB1E1AB.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/1207.image_thumb_3DA98235.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/1207.image_5F00_thumb_5F00_3DA98235.png" width="244" height="42" /></a></p>

<p><font size="2">When the emulator starts it appears as below:</font></p>

<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/5430.image_0492A533.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/5430.image_5F00_0492A533.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/3386.image_thumb_64779875.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/3386.image_5F00_thumb_5F00_64779875.png" width="178" height="327" /></a></p>

<p><font size="2">Click on <strong>Submit Reading</strong> button. Once the reading is posted, a new random reading appears. Play around by posting several readings and then scroll to <strong>View Readings</strong> pivot item that should appear somewhat like below:</font></p>

<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/4857.image_2B60BB73.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/4857.image_5F00_2B60BB73.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/5751.image_thumb_001C246C.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/5751.image_5F00_thumb_5F00_001C246C.png" width="180" height="326" /></a></p>

<p><font size="2">It has been a pretty long post but if you’ve carefully followed all the steps up till now, you should’ve a full fledged Windows Phone 8.1 client for your web API and well on your way towards developing the next one that is, well, not a rocket science to guess! Since we already have a nice separation of functionality in a PCL, we’ll look into extending our client base in the next post via a Windows Store client! As always your feedback is highly appreciated! Please don’t hesitate to point out any errors or omissions.</font></p>

<hr />

<p><font size="2"><strong><em>Related Posts:</em></strong></font></p>

<ol>
  <li><a href="https://blogs.msdn.com/b/kirpas/archive/2013/08/06/writing-a-simple-implementation-of-dependency-injection-in-mvc-4-web-api-with-net-framework-4-5.aspx"><font size="2">Writing a simple implementation of dependency injection in MVC 4 Web API with .NET Framework 4.5</font></a></li>

  <li><a href="https://blogs.msdn.com/b/kirpas/archive/2014/05/14/writing-bdd-style-unit-tests-to-verify-the-mvc-based-web-api.aspx"><font size="2">Writing BDD style unit tests to verify the MVC based Web API</font></a></li>
</ol>

<hr />]]></content:encoded>
    </item>
    <item>
      <title>Writing BDD style unit tests to verify the MVC based Web API</title>
      <link>https://docs.microsoft.com/archive/blogs/kirpas/writing-bdd-style-unit-tests-to-verify-the-mvc-based-web-api</link>
      <pubDate>Wed, 14 May 2014 12:52:00 GMT</pubDate>
      <dc:creator><![CDATA[Kirpa Singh]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/kirpas/2014/05/14/writing-bdd-style-unit-tests-to-verify-the-mvc-based-web-api/</guid>
      <description><![CDATA[[Previous post –&gt;Writing a simple implementation of dependency injection in MVC 4 Web API with...]]></description>
      <content:encoded><![CDATA[<p><font size="2"></font></p>  <p><font size="2">[Previous post –&gt;</font><a href="https://blogs.msdn.com/b/kirpas/archive/2013/08/06/writing-a-simple-implementation-of-dependency-injection-in-mvc-4-web-api-with-net-framework-4-5.aspx?CommentPosted=true#commentmessage"><font size="2">Writing a simple implementation of dependency injection in MVC 4 Web API with .NET Framework 4.5</font></a>]</p>  <p><font size="2">In my <a href="https://blogs.msdn.com/b/kirpas/archive/2013/08/06/writing-a-simple-implementation-of-dependency-injection-in-mvc-4-web-api-with-net-framework-4-5.aspx">previous blog post</a>, we developed a simple REST based web API that exposes web methods using MVC 4 controller. We also discussed how to implement a simple dependency injection mechanism using the inbuilt <a href="https://msdn.microsoft.com/en-us/library/system.web.mvc.idependencyresolver(v=vs.118).aspx">IDependencyResolver</a> interface, which may be useful to understand the <a href="https://en.wikipedia.org/wiki/Inversion_of_control">IoC pattern</a> for academic purposes though the practical implementation inevitably requires a <a href="https://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx">DI framework</a> such as <a href="https://unity.codeplex.com/">Unity framework</a> by Microsoft <a href="https://pnp.azurewebsites.net/en-us/">Patterns and Practices team</a>! <img class="wlEmoticon wlEmoticon-winkingsmile" style="border-top-style: none; border-bottom-style: none; border-right-style: none; border-left-style: none" alt="Winking smile" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/8561.wlEmoticon-winkingsmile_210D56F9.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/8561.wlEmoticon_2D00_winkingsmile_5F00_210D56F9.png" /> (Winking for the shameless promotion though it’s among the best out there and we recently utilized it in our latest guidance that shows <a href="https://msdn.microsoft.com/en-us/library/dn735912.aspx">how to use Microsoft Azure Media Services to build an On-Demand Video Service</a>. And here goes another <img class="wlEmoticon wlEmoticon-winkingsmile" style="border-top-style: none; border-bottom-style: none; border-right-style: none; border-left-style: none" alt="Winking smile" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/8561.wlEmoticon-winkingsmile_210D56F9.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/8561.wlEmoticon_2D00_winkingsmile_5F00_210D56F9.png" /> for the latter!</font></p>  <p><font size="2">Okay, let’s get back to the topic under discussion before I deviate any further. In this post, first we’ll write a couple of <a href="https://dannorth.net/introducing-bdd/">BDD style unit tests</a> to verify the API methods.</font></p>  <p><font size="2">To the solution created in the <a href="https://blogs.msdn.com/b/kirpas/archive/2013/08/06/writing-a-simple-implementation-of-dependency-injection-in-mvc-4-web-api-with-net-framework-4-5.aspx">previous step</a>, add a new solution folder named <strong>Test</strong> and create a new <strong>Unit Test</strong> <strong>Project</strong> named <strong>SensorDataTracker.Test</strong> under the <strong>Test</strong> folder:</font></p>  <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/1207.image_55ADB334.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/1207.image_5F00_55ADB334.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/7506.image_thumb_3592A677.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/7506.image_5F00_thumb_5F00_3592A677.png" width="292" height="94" /></a></p>  <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/6011.image3_678A4701.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/6011.image3_5F00_678A4701.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/5023.image3_thumb_5C60BCB7.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/5023.image3_5F00_thumb_5F00_5C60BCB7.png" width="529" height="370" /></a></p>  <p><font size="2">Rename the default source file <strong>UnitTest1.cs</strong> to <strong>TestHarness.cs</strong>:</font></p>  <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/5751.image7_032ED2F8.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/5751.image7_5F00_032ED2F8.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/1541.image7_thumb_1C2AA33D.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/1541.image7_5F00_thumb_5F00_1C2AA33D.png" width="289" height="152" /></a></p>  <p><font size="2">Replace the code in <strong>TestHarness.cs</strong> with the following:</font></p>  <pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; width: 580px; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; background-color: #fbfbfb"><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">namespace</span> SensorDataTracker.Test
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">{
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> System;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">using</span> System.Collections.Generic;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">using</span> SensorDataTracker.Models;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> SensorDataTracker.Store;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">internal</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">class</span> TestHarness
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">internal</span> <span style="color: #0000ff">static</span> IEnumerable&lt;SensorReading&gt; GenerateAndStoreReadings(
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            ISensorReadingStore store,
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">int</span> count,
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            Func&lt;<span style="color: #0000ff">int</span>, <span style="color: #0000ff">int</span>&gt; genMethod)
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            var readings = <span style="color: #0000ff">new</span> List&lt;SensorReading&gt;();
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">for</span> (var i = 0; i &lt; count; i++)
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                var sensorReading = <span style="color: #0000ff">new</span> SensorReading { Id = genMethod(i) };
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                readings.Add(sensorReading);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                store.Save(sensorReading);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">return</span> readings;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">}</pre></pre>

<p><font size="2">As you can see, we’ve created a utility method that generates and store readings. For the sake of brevity, the same method is generating as well as storing the readings though ideally in order to adhere to </font><a href="https://en.wikipedia.org/wiki/Single_responsibility_principle"><font size="2">SRP</font></a><font size="2">, you’d like to refactor! The method <font face="Courier New">GenerateAndStoreReadings</font> expects a <font face="Courier New">SensorReadingStore</font> object, a count of readings to generate and a method that acts as the reading generator. A <font face="Courier New">SensorReading</font> is generated and added to a list as well as to a <font face="Courier New">SensorReadingStore</font><font face="Segoe UI"> and finally the list of readings is returned to the caller.</font></font></p>

<p><font size="2">Now since the harness is in place, we’re ready to add our first unit test. Add a new class to <strong>SensorDataTracker.Test</strong> project and name it <strong>SensorReadingStoreTest.cs</strong> as shown below:</font></p>

<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/2112.image_29FCE938.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/2112.image_5F00_29FCE938.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/5344.image_thumb_5BF489C2.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/5344.image_5F00_thumb_5F00_5BF489C2.png" width="314" height="182" /></a></p>

<p><font size="2">Paste the following code in <strong>SensorReadingStoreTest.cs</strong> file:</font></p>

<pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; width: 580px; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; background-color: #fbfbfb"><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">namespace</span> SensorDataTracker.Test
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">{
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> System.Collections.Generic;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">using</span> System.Linq;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">using</span> Microsoft.VisualStudio.TestTools.UnitTesting;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">using</span> SensorDataTracker.Models;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> SensorDataTracker.Store;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    [TestClass]
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> SensorReadingStoreTest
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        [TestMethod]
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> WhenSavingItemsShouldReturnSincereRepresentationOfTheItems()
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #008000">// Given</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            var store = <span style="color: #0000ff">new</span> SensorReadingStore();
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            var readings = TestHarness.GenerateAndStoreReadings(store, 3, i =&gt; 2 * i + 1);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #008000">// When</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            var results = store.Get().Results.ToList();
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            <span style="color: #008000">// Then</span>
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            CollectionAssert.AreEqual(
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                readings.ToArray(),
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                results,
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">                Comparer&lt;SensorReading&gt;.Create((x, y) =&gt; x.Id.CompareTo(y.Id)));
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">    }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">}
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre></pre>

<p><font size="2">Carefully examine the language I used while naming the test that self-documents the intent of the test. The test is nicely structured to depict that <em></em></font></p>

<blockquote>
  <h6><font size="2"><em><font style="background-color: #ffffff" color="#800000">given</font></em> a specific set of initial conditions</font></h6>

  <h6><font size="2"><em><font style="background-color: #ffffff" color="#800000">when</font></em> certain operations are performed that triggers a scenario</font></h6>

  <h6><font size="2"><em><font style="background-color: #ffffff" color="#800000">then</font></em> specific set of outcomes </font><font size="2"><em><font style="background-color: #ffffff" color="#800000">should</font></em> be obtained. </font></h6>
</blockquote>

<p><font size="2">The test first creates 3 sensor readings using any random function such as an odd number generation algorithm in this particular case and saves the readings to the store. When readings are obtained from the in memory store they should match with the list of readings returned by the method that generates and stores the sensor readings. Note the use of a Comparer delegate that uses a lambda expression to compare the corresponding ids of two collections of sensor readings, pretty elegant! <img class="wlEmoticon wlEmoticon-smile" style="border-top-style: none; border-bottom-style: none; border-right-style: none; border-left-style: none" alt="Smile" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/0576.wlEmoticon-smile_7EB85230.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/0576.wlEmoticon_2D00_smile_5F00_7EB85230.png" /></font></p>

<p><font size="2">Let’s add another test. Add the following code in <strong>SensorReadingStoreTest.cs</strong>:</font></p>

<pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; width: 580px; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; background-color: #fbfbfb"><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        [TestMethod]
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> WhenSecondPageIsRequestedShouldReturnTheSecondPageOfResults()
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            var store = <span style="color: #0000ff">new</span> SensorReadingStore();
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">const</span> <span style="color: #0000ff">int</span> TwoPages = 2 * SensorReadingStore.PageSize;
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">for</span> (var i = 0; i &lt; TwoPages; i++)
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            {
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                store.Save(<span style="color: #0000ff">new</span> SensorReading() { Id = i, CreateDate = DateTime.Now });
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            }
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            var page = store.Get(2);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            Assert.AreEqual(SensorReadingStore.PageSize, page.Results.Count());
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">            Assert.AreEqual(SensorReadingStore.PageSize, page.Results.First().Id);
</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }</pre></pre>

<p><font size="2">Here we’re verifying that</font> <font size="2">on requesting the second page of sensor readings, the test should indeed return the second page. And how do we verify if we’re actually getting the second page? First we initialize a constant integer to double the size of a page e.g. if one page stores 5 readings then <font face="Courier New">TwoPages</font> is initialized to 10. Now we store sensor readings with ids from 0 to 9 in the <font face="Courier New">SensorReadingStore</font> instance and then request the second page of results (<font face="Courier New">ResultPage</font>) from the Web API controller using <font face="Courier New">Get</font> method. And all we’re left to do, is to verify that the number of results and the <font face="Courier New">Id</font> of the first reading in the second page both match the <font face="Courier New">PageSize</font>!</font></p>

<p><font size="2">Now let’s run the tests and verify that they are passing. I prefer to run all my tests in the <strong>Test Explorer</strong> window. To bring up <em>Test Explorer</em> in Visual Studio 2013, choose <strong>TEST—&gt;Windows—&gt;Test Explorer</strong> from the main menu. Select <strong>Run All</strong> option in the toolbar of Test Explorer window and you should see something similar to below if all goes well!</font></p>

<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/5367.image_02C2A003.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/5367.image_5F00_02C2A003.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/4762.image_thumb_49ABC300.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/4762.image_5F00_thumb_5F00_49ABC300.png" width="283" height="269" /></a></p>

<p><font size="2">That’s it! In the next blog post, we’ll add a Windows Phone client that consumes the Web API so stay tuned. <img class="wlEmoticon wlEmoticon-mobilephone" style="border-top-style: none; border-bottom-style: none; border-right-style: none; border-left-style: none" alt="Mobile phone" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/51/metablogapi/8004.wlEmoticon-mobilephone_2990B643.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-51-metablogapi/8004.wlEmoticon_2D00_mobilephone_5F00_2990B643.png" />.</font></p>

<p><font size="2">[Next Post—&gt;<a href="https://blogs.msdn.com/b/kirpas/archive/2014/06/20/developing-a-windows-phone-8-1-client-for-the-sensor-data-tracker-web-api-via-a-portable-class-library.aspx"><font size="3">Developing a Windows Phone 8.1 client for the sensor data tracker web API via a Portable Class Library</font></a>]</font></p>]]></content:encoded>
    </item>
    <item>
      <title>Writing a simple implementation of dependency injection in MVC 4 Web API with .NET Framework 4.5</title>
      <link>https://docs.microsoft.com/archive/blogs/kirpas/writing-a-simple-implementation-of-dependency-injection-in-mvc-4-web-api-with-net-framework-4-5</link>
      <pubDate>Tue, 06 Aug 2013 13:49:00 GMT</pubDate>
      <dc:creator><![CDATA[Kirpa Singh]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/kirpas/2013/08/06/writing-a-simple-implementation-of-dependency-injection-in-mvc-4-web-api-with-net-framework-4-5/</guid>
      <description><![CDATA[ASP.NET Web API is a new feature release with the .NET Framework 4.5. It allows a single Web service...]]></description>
      <content:encoded><![CDATA[<p><font size="2">ASP.NET Web API is a new feature release with the .NET Framework 4.5. It allows a single Web service to communicate with multiple clients in various formats such as XML, JSON and OData. Although many protocols exist for communicating data on the Internet, HTTP seems to be dominating due to its ease of use and wide acceptance.</font></p>  <p><font size="2">ASP.NET Web API allows developers to expose data and services to the Web directly over HTTP. This approach offers developers the ability to fully harness the richness of HTTP as an application layer protocol to communicate with a broad set of clients, including browsers, mobile devices, desktop applications, or backend services. The architecture is designed to support applications built with REST, but it does not force new applications to use a REST-style architecture.</font></p>  <p><font size="2">This article does not intend to be a full blown walkthrough or tutorial for developing Web API services using .NET Framework 4.5 but rather demonstrates the use of a simple implementation of inbuilt dependency injection (DI) [also known as Inversion of Control (IOC)] mechanism. Microsoft Patterns and Practices team have developed their own DI framework called Unity that is the more suitable candidate in elaborated scenarios. Latest version of Enterprise Library that includes Unity framework can be found on Codeplex. Here is the link: </font><a href="https://entlib.codeplex.com/releases/view/64243"><font size="2">https://entlib.codeplex.com/releases/view/64243</font></a><font size="2">. It's highly recommended to read the accompanying developers guide to get a thorough understanding of the DI even if you're looking to use a different DI framework!</font></p>  <p><font size="2">I’m using Visual Studio 2012 Update 2 though Visual Studio 2012 or Visual Studio 2012 Express, Visual Studio 2010 (with ASP.NET Web API extension installed) should work well. Some familiarity with MVC architecture is assumed but is not a requirement for understanding the code.</font></p>  <p><font size="2">The first step to creating a Web API service is to fire up the IDE and create a new project of type ASP.NET MVC 4 Web Application:</font></p>  <p>&#160;<a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/57/0882.Create_New_Proj.PNG" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-57/0882.Create_5F00_New_5F00_Proj.PNG"><img border="0" alt="" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/57/0882.Create_New_Proj.PNG" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-57/0882.Create_5F00_New_5F00_Proj.PNG" /></a></p>  <p><font size="2">In the next dialog, choose <strong>Web API</strong> as the template and leave rest of the options as they are.</font></p>  <p>&#160;<a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/57/1856.WebAPI_Project.PNG" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-57/1856.WebAPI_5F00_Project.PNG"><img border="0" alt="" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/57/1856.WebAPI_Project.PNG" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-57/1856.WebAPI_5F00_Project.PNG" /></a></p>  <p><font size="2">Click on <strong>OK </strong>button and a new Web API project based on MVC 4 is generated for you. Since the intent of this tutorial is to exhibit simple inbuilt DI mechanism in Web API MVC 4, we'll not go into the details of the architecture or MVC in general. We'll start by adding a couple of model classes to represent a fictional sensor reading and a results page displaying all the readings for a user. Right click on <strong>Models</strong> folder and add a new class named <strong>SensorReading</strong> as shown:</font></p>  <p>&#160;<a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/57/4606.AddModelClass_SensorReading.PNG" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-57/4606.AddModelClass_5F00_SensorReading.PNG"><img border="0" alt="" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/57/4606.AddModelClass_SensorReading.PNG" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-57/4606.AddModelClass_5F00_SensorReading.PNG" /></a></p>  <p><font size="2">Replace the code in <strong>SensorReading.cs </strong>with the following code:</font></p>  <pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; width: 580px; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; background-color: #fbfbfb"><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">namespace</span> SensorDataTracker.Models
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">{
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> System;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> SensorReading
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> Id { <span style="color: #0000ff">get</span>; <span style="color: #0000ff">set</span>; }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> DateTime CreateDate { <span style="color: #0000ff">get</span>; <span style="color: #0000ff">set</span>; }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">}</pre></pre>

<p><font size="2">As it’s trivial to figure out, we’re creating a class to represent a sensor reading:</font></p>

<ol>
  <li><font size="2"><strong>Id</strong> – Actual numeric reading </font></li>

  <li><font size="2"><strong>CreateDate</strong> – Date and time when the reading was taken </font></li>
</ol>

<p><font size="2">Similarly create another model class and name it <strong>ResultPage</strong>. Replace the code in <strong>ResultPage.cs</strong> with the following code:</font></p>

<pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; width: 580px; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; background-color: #fbfbfb"><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">namespace</span> SensorDataTracker.Models
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">{
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> System.Collections.Generic;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ResultPage&lt;T&gt;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> TotalCount { <span style="color: #0000ff">get</span>; <span style="color: #0000ff">set</span>; }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> PageNumber { <span style="color: #0000ff">get</span>; <span style="color: #0000ff">set</span>; }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> IEnumerable&lt;T&gt; Results { <span style="color: #0000ff">get</span>; <span style="color: #0000ff">set</span>; }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">}</pre></pre>

<p><font size="2">Similarly we’re creating a generic collection holder <strong>ResultPage</strong> class that has:</font></p>

<ol>
  <li><font size="2"><strong>TotalCount</strong> – Total number of sensor readings. </font></li>

  <li><font size="2"><strong>PageNumber</strong> – Current page number </font></li>

  <li><font size="2"><strong>Results</strong> – Generic collection of objects that may be used to store any object. </font></li>
</ol>

<p><font size="2">Now we'll implement a mechanism for fake persistence of the readings using an in memory store that simply stores the readings for the duration of the session in a memory collection, though a real implementation shall have a backend database to persist and retrieve the values at will.</font></p>

<p><font size="2">Add a new folder to the project and call it <strong>Store</strong>. </font></p>

<p><font size="2">Under folder <strong>Store</strong>, add an interface named <strong>ISensorReadingStore</strong>. Replace the code in <strong>ISensorReadingStore.cs</strong> with the following code:</font></p>

<pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; width: 580px; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; background-color: #fbfbfb"><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">namespace</span> SensorDataTracker.Store
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">{
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> SensorDataTracker.CommonLogic.Models;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> ISensorReadingStore
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">void</span> Save(SensorReading reading);
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        ResultPage&lt;SensorReading&gt; Get();
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        ResultPage&lt;SensorReading&gt; Get(<span style="color: #0000ff">int</span> pageNumber);
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">}</pre></pre>

<p><font size="2">Again under folder <strong>Store</strong>, add a new class named <strong>SensorReadingStore</strong>. Replace the code in <strong>SensorReadingStore.cs</strong> with the following code:</font></p>

<pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; width: 580px; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; background-color: #fbfbfb"><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">namespace</span> SensorDataTracker.Store
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">{
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> SensorDataTracker.Models;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> System.Collections.Generic;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> System.Linq;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> SensorReadingStore : ISensorReadingStore
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> IList&lt;SensorReading&gt; _internalStore = <span style="color: #0000ff">new</span> List&lt;SensorReading&gt;();
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">int</span> PageSize = 5;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Save(SensorReading reading)
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            _internalStore.Add(<span style="color: #0000ff">new</span> SensorReading { Id = reading.Id, CreateDate = reading.CreateDate });
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> ResultPage&lt;SensorReading&gt; Get()
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.Get(1);
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> ResultPage&lt;SensorReading&gt; Get(<span style="color: #0000ff">int</span> pageNumber)
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            var skipCount = (pageNumber - 1) * PageSize;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> ResultPage&lt;SensorReading&gt;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                       {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                           TotalCount = _internalStore.Count(),
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                           PageNumber = pageNumber,
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                           Results = _internalStore.Skip(skipCount).Take(PageSize)
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                       };
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">}</pre></pre>

<p><font size="2">Notice how we’re using an interface to represent our store. In order to plug-in DI, we need to design our application with loose coupling and no hard dependencies in place since the DI framework accesses the system via interfaces. This is also very handy approach for unit testing since fake representations of the underlying store may be created with a very few lines of code and tests are executed against the latter as opposed to running the test against say a store that hosts SQL database!</font></p>

<p><font size="2">Next step is to create the Web API controller that handles all the requests from user interface and simultaneously interact with our store to retrieve and save values. Locate the <strong>Controllers</strong> folder and delete the default API controller <strong>ValuesController.cs</strong> file since we’re plugging in our own.</font></p>

<p><font size="2">[Note: You may have to delete the corresponding Unit Test controller if you chose to create the unit test project initially, otherwise you’ll see compile errors.]</font></p>

<p><font size="2">Right click on <strong>Controllers</strong> folder and select <strong>Add</strong>--&gt;<strong>Controller. </strong>Make sure that the dialog looks like as below:</font></p>

<p>&#160;<a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/57/6507.AddController.PNG" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-57/6507.AddController.PNG"><img border="0" alt="" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/60/57/6507.AddController.PNG" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-57/6507.AddController.PNG" /></a></p>

<p><font size="2">Replace the code in <strong>SensorReadingController.cs</strong> with the following code:</font></p>

<pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; width: 580px; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; background-color: #fbfbfb"><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">namespace</span> SensorDataTracker.Controllers
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">{
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> SensorDataTracker.Models;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> SensorDataTracker.Store;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> System.Net;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">using</span> System.Web.Http;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> SensorReadingController : ApiController
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> ISensorReadingStore _store;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> SensorReadingController(ISensorReadingStore store)
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            _store = store;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Post(SensorReading reading)
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">if</span>(reading==<span style="color: #0000ff">null</span>) <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> HttpResponseException(HttpStatusCode.BadRequest);
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            _store.Save(reading);
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> ResultPage&lt;SensorReading&gt; Get()
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">return</span> Get(1);
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> ResultPage&lt;SensorReading&gt; Get(<span style="color: #0000ff">int</span> pageNumber)
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">return</span> _store.Get(pageNumber);
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">}</pre></pre>

<p>&#160;</p>

<p><font size="2">As you can see, we’ve methods mapping to GET and POST requests with one overload of GET that takes a page number as a parameter. Notice the constructor of <strong>SensorReadingController</strong> class that takes an interface as a parameter – classic paradigm of DI implementation. This way the controller doesn’t need to be dependent on a specific implementation of store and DI framework may inject the dependency at runtime.</font></p>

<p><font size="2">Okay…so now since we’ve the initial wiring setup, it’s time to plug-in the implementation of the build in DI framework. Right click on App_Start folder and add a new class <strong>ResolveController.cs</strong> and insert the following code:</font></p>

<pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; width: 580px; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; background-color: #fbfbfb"><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">using</span> System;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">using</span> System.Collections.Generic;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">using</span> System.Web.Http.Dependencies;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">using</span> SensorDataTracker.Controllers;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">using</span> SensorDataTracker.Store;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">namespace</span> SensorDataTracker.App_Start
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">{
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ResolveController : IDependencyResolver
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">readonly</span> ISensorReadingStore ReadingStore = <span style="color: #0000ff">new</span> SensorReadingStore();
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">object</span> GetService(Type serviceType)
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">return</span> serviceType == <span style="color: #0000ff">typeof</span>(SensorReadingController) ? <span style="color: #0000ff">new</span> SensorReadingController(ReadingStore) : <span style="color: #0000ff">null</span>;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> IEnumerable&lt;<span style="color: #0000ff">object</span>&gt; GetServices(Type serviceType)
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> List&lt;<span style="color: #0000ff">object</span>&gt;();
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> IDependencyScope BeginScope()
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Dispose()
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">           
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">}</pre></pre>

<p>&#160;</p>

<p><font size="2">Let’s quickly walkthrough the code before moving further. Here we’re creating a DI controller by implementing the inbuilt <strong>IDependencyResolver</strong> interface. You need to provide implementation of service finder methods <strong>GetService</strong> and <strong>GetServices</strong> both accepting a type as parameter. Since we’re creating a simple implementation of DI framework, we may ignore the <strong>GetServices</strong> method for the time being and simply return a collection of objects which is the recommended approach. In our over-simplistic model, we’re leaving <strong>Dispose</strong> method empty since we’re not holding onto an expensive resource that needs to be released. For <strong>BeginScope</strong>, since in this case we don’t have a concept of a scope or resource release, we may simply return <strong>this</strong> object!</font></p>

<p><font size="2">Crux of the implementation is <strong>GetService</strong> method. Since we’re dealing with only one implementation of controller, we’re checking the type of the passed in controller and if it’s of type <strong>SensorReadingController</strong> we’re creating a new instance of controller and passing it the default implementation of <strong>ISensorReadingStore</strong> and that’s it!</font></p>

<p><font size="2">Next step is to let the framework know about the dependency resolver we’re using and we’re going to do this in <strong>WebApiConfig.cs</strong> file that contains the HTTP route mapping table.</font></p>

<p><font size="2">Expand the <strong>App_Start</strong> folder and open <strong>WebApiConfig.cs</strong> file. Replace the existing code with the following:</font></p>

<pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; width: 580px; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; background-color: #fbfbfb"><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">using</span> System;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">using</span> System.Collections.Generic;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">using</span> System.Linq;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">using</span> System.Web.Http;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">using</span> SensorDataTracker.App_Start;
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"><span style="color: #0000ff">namespace</span> SensorDataTracker
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">{
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">class</span> WebApiConfig
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Register(HttpConfiguration config)
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        {
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            config.Routes.MapHttpRoute(
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                name: &quot;<span style="color: #8b0000">DefaultApi</span>&quot;,
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                routeTemplate: &quot;<span style="color: #8b0000">api/{controller}/{id}</span>&quot;,
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">                defaults: <span style="color: #0000ff">new</span> { id = RouteParameter.Optional }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            );
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #008000">// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable&lt;T&gt; return type.</span>
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #008000">// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.</span>
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #008000">// For more information, visit https://go.microsoft.com/fwlink/?LinkId=279712.</span>
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #008000">//config.EnableQuerySupport();</span>
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #008000">// To disable tracing in your application, please comment out or remove the following line of code</span>
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <span style="color: #008000">// For more information, refer to: https://www.asp.net/web-api</span>
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            config.EnableSystemDiagnosticsTracing();
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">            <font style="background-color: #ffff00">config.DependencyResolver = <span style="color: #0000ff">new</span> ResolveController();</font>
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">        }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">    }
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb">}
</pre><pre style="font-size: 13px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #fbfbfb"></pre></pre>

<p><font size="2">Notice the <span><font style="background-color: rgb(255,255,0)">highlighted</font></span> statement where we’re telling the Web API infrastructure which DI implementation to use. Rest of the code remains unchanged.</font></p>

<p><font size="2">So now we’ve completed all the steps that are required to provide a simple implementation of inbuilt DI framework in a Web API service. Now you may build and run the service in the IIS Express host that comes with the IDE. If you set a breakpoint in <strong>GetService</strong> method and invoke the service from web browser such as below:</font></p>

<p><strong><font size="2">https://&lt;host&gt;:&lt;protocol&gt;/api/SensorReading</font></strong></p>

<p><font size="2">You should hit the breakpoint in <strong>GetService</strong> method which proves that our DI implementation is working as expected! On completion of the call, you should get a JSON string or HTML based on the default accept-header of the browser you’re working with.</font></p>

<p><font size="2">And that’s it for now! In the next series of articles, I’ll demonstrate how to write a WPF client, a Windows Phone 8 client, Windows Store client and unit testing strategies to go along this approach.</font></p>

<p><font size="2"><strong>NOTE</strong>: The application shown in this example is part of the code that I wrote for </font><a title="https://tig.codeplex.com/releases/view/108411" href="https://blogs.msdn.com/controlpanel/blogs/posteditor.aspx/Continuous Delivery Release Pipeline with TFS"><font size="2">Continuous Delivery Release Pipeline with TFS</font></a><font size="2"> project (by </font><a title="https://msdn.microsoft.com/en-us/library/ff921345.aspx" href="https://blogs.msdn.com/controlpanel/blogs/posteditor.aspx/Microsoft Patterns and Practices team"><font size="2">Microsoft Patterns and Practices team</font></a><font size="2">).</font></p>

<p><font size="2"><strong>Next in Series</strong></font> –&gt;<a href="https://blogs.msdn.com/b/kirpas/archive/2014/05/14/writing-bdd-style-unit-tests-to-verify-the-mvc-based-web-api.aspx"><font size="3">Writing BDD style unit tests to verify the MVC based Web API</font></a></p>]]></content:encoded>
    </item>
  </channel>
</rss>