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:
- Select File | Add | New Project… The Add New Project dialog box will appear.
- 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).
- Select Portable Class Library.
- 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. - 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
- 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
. - Delete the automatically generated class,
Class1
. - Add a new interface,
IEntity
: - Add a new class,
Entity
, that implements the previously createdIEntity
interface: - Add a new class, Item, that inherits from
Entity
: - Add a new class,
Group
, that inherits fromEntity
and groups items:

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

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

Figure 3: The DrDobbsApp.Entities Portable Class Library in the Solution Explorer.
namespace DrDobbsApp.Entities { public interface IEntity { string Id { get; set; } } }
{ public class Entity : IEntity { public string Id { get; set; } } }
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; } } }
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.