Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


Access Data with REST in Windows 8 Apps

In Customizing the Appearance of Windows 8 Apps, the previous article in this series on developing a Windows 8 Store app, I explained how customize the appearance for a Grid app and how the app used a sample data source. Here, I explain how to create a new REST service from scratch based on the new ASP.NET Web API introduced in .NET Framework 4.5. After creating the REST service, I'll consume it in the sample Grid app by using the new asynchronous methods provided by the new System.Net.Http.HttpClient.

Creating a Portable Class Library

If you want to share code between a Web service and a WPF application, both based on .NET Framework 4.5, you may think about creating a Class Library project. However, as you already know, a Windows 8 Store app uses .NET for Windows Store apps; therefore, you cannot add a .NET Framework 4.5 Class Library as a reference in a Windows 8 Store app. Visual Studio 2012 Professional or greater versions include a new kind of project template that comes to the rescue — the Portable Class Library (PCL). Notice that the Express versions of Visual Studio 2012 don't include this useful project template.

A Portable Class Library enables you to build assemblies that work on one the following Microsoft platforms:

  • .NET for Windows Store apps
  • .NET Framework 4.0 and higher, 4.03 and higher, or 4.5
  • Silverlight 4 and higher, or Silverlight 5
  • Windows Azure SDK (when you have Windows Azure SDK for .NET installed)
  • Windows Phone 7 and higher, Windows Phone 7.5 or Windows Phone 8 (when you have Windows Phone SDK 8.0 installed)
  • Xbox 360

In my case, I want to share entities between a REST service that will use the new ASP.NET Web API (.NET Framework 4.5) and the Windows Store app (.NET for Windows Store apps). This way, I don't need to duplicate code in both the client and the server.

In Visual Studio 2012 Professional or greater, follow these steps to add a new PCL project to the existing DrDobbsApp solution:

  1. Select File | Add | New Project… The Add New Project dialog box will appear.
  2. Select Templates | Visual C# | Windows in the left pane. The templates list will display many templates for the selected target, including Portable Class Library (Figure 1).

  3. Figure 1: The Add New Project dialog box displaying the details about the Portable Class Library template.

  4. Select Portable Class Library.
  5. Enter the desired name in the Name textbox. I'll use DrDobbsApp.Entities because both my sample app and a REST service will share the entities defined in this new PCL.
  6. Click OK and the Add Portable Class Library dialog box will appear. Leave only the following target frameworks checked (Figure 2):
    • .NET Framework 4.5
    • .NET for Windows Store apps

    Figure 2: The Add Portable Class Library dialog box displaying the two target frameworks for the Portable Class Library.

  7. Finally, click OK and the IDE will add the new DrDobbsApp.Entities PCL to the solution. If you open References, you will see only one reference to .NET Portable Subset (Figure 3). The PCL includes an initial class, Class1.

  8. Figure 3: The DrDobbsApp.Entities Portable Class Library in the Solution Explorer.

  9. Delete the automatically generated class, Class1.
  10. Add a new interface, IEntity:

  11.   namespace DrDobbsApp.Entities
        public interface IEntity
            string Id { get; set; }
  12. Add a new class, Entity, that implements the previously created IEntity interface:

  13. {
        public class Entity : IEntity
            public string Id { get; set; }
  14. Add a new class, Item, that inherits from Entity:

  15. namespace DrDobbsApp.Entities
        public class Item : Entity
            public string Title { get; set; }
            public string Subtitle { get; set; }
            public string Description { get; set; }
            public string Content { get; set; }
            public string MainPage { get; set; }
  16. Add a new class, Group, that inherits from Entity and groups items:

  17. namespace DrDobbsApp.Entities
        using System.Collections.Generic;
        public class Group : Entity
            public string Title { get; set; }
            public string Subtitle { get; set; }
            public string Description { get; set; }
            public string MainPage { get; set; }
            public IEnumerable<Item> Items { get; set; }

Now, you have a library to use in both the .NET for Windows Store app and future ASP.NET Web API project, thanks to the Portable Class Library.

Go back to the DrDobbsApp project that generates a Windows Store app, right click on References in Solution Explorer, and select Add Reference… The Reference Manager dialog box will appear. Click on Solution | Projects in the left pane, check DrDobbsApp.Entities (Figure 4) and click OK. This way, the Windows Store app can use the entities defined in the PCL.

Figure 4: Adding the reference to the Portable Class Library in the Windows Store app.

Creating a REST Service with ASP.NET Web API

Now, I'll explain how to create a new simple REST service from scratch based on the new ASP.NET Web API introduced in .NET Framework 4.5. The REST service is just going to retrieve all the groups with their items. However, this simple example will enable you to understand how to provide data to your Windows Store apps with easy to create REST services. I will add an ASP.NET Web API project to the existing solution and I will use the previously created PCL.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.