.NET

Access Data with REST in Windows 8 Apps

Develop and consume a REST service that provides data to a Windows Store app that uses the Grid app  using C# and XAML.

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:

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:

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.

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.

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.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!