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();
}

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);
  }
}

BugSense is back with the support of Windows 8, Windows Phone 8, HTML 5

BugSense

I already talked about BugSense, this company that provides to the developers of mobile apps the power to track crashes and problems in their applications (way better than the solution provided by the Microsoft marketplace). They are constantly evolving, adapting to the new market, the new technologies and today they announced the Windows 8 SDK for C# and JavaScript!

If you are familiar with NuGet, it has never been so easy to integrate a third-party library in your current project. Right click on you project, then Manage NuGet Packages…

image_20

C# 5.0 – Caller Information

visual_studio_logoOne of the new feature I really like in C# 5.0 was introduced for the very first time at the Microsoft BUILD in September 2011 in California. Microsoft presented to the world of the developers the next Windows and the APIs WinRT. Today I’ would like to talk a bit about the Caller Information. This feature consists of the use of the attributes CallerFilePathAttribute, CallerLineNumberAttribute and CallerMemberNameAttribute. These attributes can be use with optional parameters in a function, method, delegate, Property…

The basic usage of this feature is when you want to implement a log system in your software. By writing the following code, you can retrieve the calling member name, the number of the line of code that called this method and the file path without having to do reflection or anything by yourself.

public void Do()
{
    Log("Example");
}

public void Log(string message,
        [CallerMemberName] string memberName = "",
        [CallerFilePath] string sourceFilePath = "",
        [CallerLineNumber] int sourceLineNumber = 0)
{
    // Implement your log system }

Another cool usage is with the interface INotifyPropertyChanged. In C# 4 and before you have to write the following code in order to raise the event PropertyChanged when you update the property Name.

public class Product : INotifyPropertyChanged {

    #region Name

    private string _name;

    /// <summary>
    /// Gets or sets the Name.
    /// </summary>
    public string Name
    {
        get { return _name; }
        set {
            if (value == _name) return;

            _name = value;

            NotifyPropertyChanged("Name");
        }
    }

    #endregion #region INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion }

The problem here, is when you have several properties and you want to do a basic Copy & Paste. If you’re doing it too fast, you’ could forget the line NotifyPropertyChanged(“Name”); or type a wrong property name and because it is a string value, your Visual Studio or ReSharper will not say anything, and the compilation will not crash. So in C# 5.0 you can write it like that:

public class Product : INotifyPropertyChanged {

    #region Name

    private string _name;

    /// <summary>
    /// Gets or sets the Name.
    /// </summary>
    public string Name
    {
        get { return _name; }
        set {
            if (value == _name) return;

            _name = value;

            NotifyPropertyChanged();
        }
    }

    #endregion #region INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion }

Codeplex OAuth providers for .NET Framework 4.0 and Windows Phone 7

File:Oauth logo.svg

The project

I started few weeks ago, a new project that allows me to do trading using the APIs provided by E*Trade. The authorization system used is OAuth. My first idea was to create a Windows Phone 7 application for E*Trade, what I started to do. Did you already try to debug web services that you don’t control? Especially when the documentation is not very good. When you send a bad request to the server, you always get the same answer:  The remote server returned an error: NotFound. But what does it mean? Basically everything. That’s why I created a new WPF project to have the full .NET Framework in order to get more information and help me to debug the Web service calls.

What libraries I use

I’m using the Reactive Extensions (Rx) to “wrap” my asynchronous event into observable objects. Then, because E*Trade APIs are based on OAuth protocol for the authorization of the users, I needed an library that helps me to send queries to the server. I’m using the project ReactiveOAuth because it is one of the best I found. I just had to use the source code and modify it, because it was originally created for Twitter, and it seems that every single OAuth provider does not respect the standards.

What’s next

I decided to publish the full source code of my project via CodePlex under the name OAuth Providers. I currently published an updated version of ReactiveOAuth and a basic SDK for E*Trade. I also included an example using WPF that connect to E*Trade and download some stocks information. I’ll publish soon an update of the SDK that will contain more API support.

My next objective is to publish more and more SDK for OAuth providers like Twitter, Facebook, LinkedIn, Foursquare…

Download the SDK

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