C#

Azure Mobile Services – Paging with REST APIs

Mobile Services
I would like to complete the code I’ve provided to call the REST APIs for Windows Azure Mobile Services in my two previous posts, Introduction to Windows Azure Mobile Services and Windows Azure Mobile Services – Update with REST APIs. Today, I’ve been facing a basic mistake I made for my application Earthquakes. You are more and more everyday to use this application, and I thank you for that :). Each time a new user start the application, the app will register on Azure Mobile Services in order to update the Live Tile on the Phone (see the picture below). It means I store a list of URLs on Mobile Services, and I’ll contact these URLs to update the Live Tile on your phone.

Earthquakes Live Tiles
If you’ve read and use the source code I’ve provided you’ve probably noticed an issue. How do you retrieve a list of more than 50 entries in Mobile Services? You have two possibilities:

  • The first idea would be to increase the number of items you want to retrieve when you call the REST API. In order to do that you have to specify the parameter $top of the REST API. You can ask up to 1000 items in one call. But what’s going on if you have more than a thousand items in your base? You’ll simply never get them and it’s what happened to me. I was stuck with the 50 firsts items…
    URL format: https://<ServiceName>.azure-mobile.net/tables/<TableName>?$top=<Limit>
  • The other idea and probably the better in most of the cases, is to use paging. The paging capability allows you to retrieve your elements by lot, 50 at a time for example, until you have all of them. To do so, we will combine the attributes $top and $skip. Also, by using the attribute $inlinecount with the value allpages we will get the number of elements in the table at each call. It will help us to track if we need to perform more call to get more elements.
    URL format: https://<ServiceName>.azure-mobile.net/tables/<TableName>?$top=<Limit>&$skip<Skip>&$inlinecount=allpages

Let’s start coding with a class named RestResult that we will use to deserialize the elements including the number of elements in the table.

[DataContract]
public sealed class RestResult<T>
    where T : class
{
    [DataMember(Name = "results")]
    public T[] Results { get; set; }

    [DataMember(Name = "count")]
    public int Count { get; set; }
}

Now, we can update the function Get to retrieve an object RestResult<T> that will contain the list of elements we asked and the number of elements in the table.

public RestResult<T> Get<T>(string tableName, string filter = "", int skip = 0, int top = 50)
    where T : class
{
    if (!string.IsNullOrWhiteSpace(filter))
        filter = string.Concat("&$filter=", filter);

    Uri address = new Uri(string.Format("https://{0}.azure-mobile.net/tables/{1}?$inlinecount=allpages&$skip={2}&$top={3}{4}",
                                        ServiceName, tableName, skip, top, filter));

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);

    request.Method = "GET";
    request.Headers = new WebHeaderCollection
                            {
                                {"X-ZUMO-APPLICATION", ApplicationId},
                                {"X-ZUMO-MASTER", MasterKey},
                            };

    RestResult<T> result;

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        Stream stream = response.GetResponseStream();

        result = stream.Deserialize<RestResult<T>>();
    }

    return result;
}

And to finish, here is the code that will retrieve all the elements of a table.

public IEnumerable<T> GetAll<T>(string tableName, string filter = "")
    where T : class
{
    List<T> result = new List<T>();
    bool needMore;

    do
    {
        var get = Get<T>(tableName, filter, result.Count);
        result.AddRange(get.Results);

        needMore = get.Count > result.Count;
    } while (needMore);

    return result;
}

Windows Azure Mobile Services – Update with REST APIs

Windows Azure Mobile Services

After the introduction to Windows Azure Mobile Services, I hope you had the chance to try this service. Di you know you can try Windows Azure for free for 90 days?

Today I would like to add a very short code snippet to help you updating an item in your Mobile Service. We have to use the HTTP method PATCH, send the id of the item to update and send the new item in the body of your request (serialized as a JSON message). You can use the following method in the class I gave to you in my introduction to Windows Azure Mobile Services.

public void Update<T>(string tableName, int idToUpdate, T item)
    where T : class
{
    Uri address = new Uri(string.Format("https://{0}.azure-mobile.net/tables/{1}/{2}", ServiceName, tableName, idToUpdate));

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);

    request.Method = "PATCH";
    request.Headers = new WebHeaderCollection
                            {
                                {"X-ZUMO-APPLICATION", ApplicationId},
                                {"X-ZUMO-MASTER", MasterKey},
                            };

    string serialization = item.Serialize();
    byte[] byteData = Encoding.UTF8.GetBytes(serialization);
    request.ContentLength = byteData.Length;

    using (Stream postStream = request.GetRequestStream())
    {
        postStream.Write(byteData, 0, byteData.Length);
    }

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    response.Close();
}

50 free Windows 8 Design Templates

windows8_logo

Microsoft Malaysia has recently published 50 application templates for Windows 8 to help developers to shape their apps for the Windows Store. These templates are ready to publish, in other words, you (almost) just have to open them in Visual Studio, change the name of the application and publish it in the store, that’s as easy. Each package is available in C# and JS/HTML. It’s about 500MB you can download on this SkyDrive.

Here are some samples of what you’ll get in the package:

You don’t have anymore excuse for not trying making applications for Windows 8.

[Update 1/26/2013] It seems the links are down, please look at Free Windows 8 Design Templates are coming back soon.

JSON Serialization–Deserialization

JSON is the new “trending” format used to transfer data between server and client. Today I’m going to start a series of post about Windows Azure Mobile Services, but I want to start very easy with something I’ll need for the next parts.

Here is the code of a helper to serialize and deserialize any object. Start with this using:

using System.Runtime.Serialization.Json;

Here is the static class you can use to serialize and deserialize your objects:

public static class JsonHelper
{
  public static string Serialize<T>(this T value)
     where T : class
  {
     var serializer = new DataContractJsonSerializer(typeof(T));

     using (MemoryStream stream = new MemoryStream())
     {
          serializer.WriteObject(stream, value);
          return Encoding.Default.GetString(stream.ToArray());
     }
  }

  public static T Deserialize<T>(this Stream stream)
     where T : class
  {
     var serializer = new DataContractJsonSerializer(typeof(T));
     return (T)serializer.ReadObject(stream);
  }
}

How to use Yahoo! Query Language with C#

Yahoo Query

What is the Yahoo! Query Language?

“The Yahoo! Query Language is an expressive SQL-like language that lets you query, filter, and join data across Web services.” Source Yahoo!     Yahoo! is a huge database, and we can consume all these data with a very simple query. The data can be financial data, amazon content, geographical information, weather forecast… Well, you can get almost anything from Yahoo, and if you don’t find what you’re looking for, you can still create your own table and share it with the community.

Where to get the information?

Yahoo offers an excellent tool on the Yahoo! Developer Network, the Yahoo! Query Language console. From this interface, you can see the list of tables available, try them before using them in your applications.Yahoo Query Console

On the picture above, I show you how to get the forecast of the zip code 94123 (San Francisco, CA). The result is an XML file that you can easily parse with C#. You can also get the result in JSON format.

How to use it with C#?

Let’s try to display the list of counties in a state. I’ll use the table geo.counties. The example for this table is: select * from geo.counties where place=”CA” and return the list of the 57 counties in California. You see that you can easily change the state by replacing “CA” in the query by another state like “NY” for New York. The interface gives you the REST URL to call to retrieve all the information.

private static void RetrieveCounties(string state)
{
    // Create the Query URL
    string url = string.Format(BASE_URL, state);

    // Load the document from the URL
    XDocument doc = XDocument.Load(url);

    // The XML contains this namespace
    // we need to specify it in order to be able to parse the xml
    XNamespace xNamespace = "http://where.yahooapis.com/v1/schema.rng";

    // Retrieve the list of counties
    var xPlaces = doc.Root.Element("results").Elements(xNamespace + "place");
    foreach (var xPlace in xPlaces)
    {
        // Display the element name
        Console.WriteLine(xPlace.Element(xNamespace + "name").Value);
    }
}

To make the code easier to read / understand, I did not include any caching of exception…
Yahoo Query result