Consuming webservices with Portable Class Library (PCL)

This a sample to consume a webservice using Portable Class Library (PCL). In this sample we are retrieving some geographical information from the site. Because we use portable class libraries, we can reuse this code on the different .NET platforms: Silverlight, .NET Framework 4.5, Windows Phone 7.5/8 and Windows 8 Store Apps.

Support for PCL is coming for platforms like Mono, Xamarin.iOS and Xamarin.Android. Write once, run everywhere!


Step 1/5: Create Portable Class Library project in Visual Studio 2012

File –> New-> Project –> Visual C# –> Portable Class Library

Be sure to select .NET Framework 4.5:



Set target frameworks as follows:



Step 2/5: Install nuget packages

In the NuGet package manager console type:

1: ‘install-package –pre’ : installs the PCL version of HttpClient

Result should be (without license text):

PM> install-package -pre

Attempting to resolve dependency ‘Microsoft.Bcl (≥ 1.0.16-rc)’.

Attempting to resolve dependency ‘Microsoft.Bcl.Build (≥ 1.0.0-rc)’.

Successfully installed ‘Microsoft.Bcl.Build 1.0.0-rc’.

Successfully installed ‘Microsoft.Bcl 1.0.16-rc’.

Successfully installed ‘Microsoft.Net.Http 2.1.3-beta’.

Successfully added ‘Microsoft.Bcl.Build 1.0.0-rc’ to PortableClassLibrary4.

Successfully added ‘Microsoft.Bcl 1.0.16-rc’ to PortableClassLibrary4.

Successfully added ‘Microsoft.Net.Http 2.1.3-beta’ to PortableClassLibrary4.


2: ‘install-package newtonsoft.json – pre’: installs the PCL version of JSON.NET




Step 3/5: Write some code in the PCL

In this sample we are retrieving some information about earthquakes:

Copy the json result of the earthquakes in a new c# file. Use paste –> paste special –> paste JSON as classes.



Results should be:

Geonames models


Now write the code to request the webservice and parse the results:

GeoNames Webservice Client Api


public Earthquake[] GetEarthquakes() {

var client = new HttpClient();

client.BaseAddress = new Uri(;

var response = client.GetAsync("earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=bertt").Result;

var earthquakesJson = response.Content.ReadAsStringAsync().Result;

var rootobject = JsonConvert.DeserializeObject<Rootobject>(earthquakesJson);

return rootobject.earthquakes;



Step 4/5:  Add a Windows 8 client

Add a Windows 8 client to the solution

Solution-> Add new Project… –> Windows Store –>  Blank App Xaml

Add a reference to the portable class library:

Select Project –> add reference –> select the new portable class library


Step 5/5: Add 1 line of code to the Windows 8 client

Open the mainpage.xaml.cs file and add to the OnNavigatedTo method:

var earthquakes = new GeonamesApi().GetEarthquakes();

if everything works ok, there will be some earthquakes coming back. Or there are no earthquakes in this area Glimlach . Otherwise inspect with

Fiddler to see whats going on.



Next step is to put the results somewhere in the user interface but I’ll leave that for another post. Good candidate for this is the MVVM Cross Library:



8 thoughts on “Consuming webservices with Portable Class Library (PCL)

  1. A note for other readers: To install I had to update the NuGet package manager from 2.0 to 2.2

    This was the error it solved: Could not install package ‘Microsoft.Bcl 1.0.16-rc’. You are trying to install this package into a project that targets ‘.NETPortable,Version=v4.0,Profile=Profile104’, but
    the package does not contain any assembly references that are compatible with that framework.

  2. Also make sure you chose “Windows Phone 7.5 and higher” (and NOT WP 7). I overlooked this and then it does not work.

Leave a Reply

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

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s