Updating OData data source from WP7 .. Part 2

This post is a continuation of the first part (here) on Updating the underlying data behind an OData service.

So, we continue from where we left off. We have a SQL Azure table called “Team” which has its data exposed as an OData service. This can obviously be read easily by various clients; but what about updates? In the last post, we talked about how we can host a WCF Service in Azure that allows us CRUD operations on the underlying SQL Azure data. This does work; but what if you did not want to add a Service Reference to your project? Isn’t the promise of OData to be able to make plain HTTP requests to perform CRUD on data?

Yes, you are right. In this post, we see how to insert records into the SQL Azure DB table through OData by simply doing native HTTP Posts at the correct URL. Now, I will do this from a Windows Phone app, that is, Silverlight. This brings in the asynchronous factor since we cannot lock up the UI thread. Essentially, we need to open a Request channel at the right URL, write some byte content into the request stream & then expect a response back. In Silverlight, this means jumping through two different threads before coming back to the main UI thread. Check out this wonderful post on why we need to do this (here).

Now, in addition to the thread-hopping, our HTTP Post request needs to be formatted correctly in header & content for the OData endpoint to honor it. Here’s some code using the native “HttpWebRequest” from a Windows Phone app. Essentially, I am making an HTTP Post request against my SQL Azure OData service to insert a new Team member to our DB table:


       private void SubmitPost()
        {
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(new Uri("YourODataEndpoint"));
            myRequest.Method = "POST";
            myRequest.Accept = "application/atom+xml";
            myRequest.ContentType = "application/atom+xml;type=entry";
            myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
        }

        private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            System.IO.Stream postStream = request.EndGetRequestStream(asynchronousResult);

            XNamespace ds = "http://schemas.microsoft.com/ado/2007/08/dataservices";
            XNamespace dsmd = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
            var content =
              new XElement(dsmd + "properties",
                new XElement(ds + "Name", "Test"),
                new XElement(ds + "TwitterHandle", "@Test")
              );

            XNamespace atom = "http://www.w3.org/2005/Atom";
            var entry =
              new XElement(atom + "entry",
                new XElement(atom + "title", "A new team member"),
                new XElement(atom + "id", string.Format("urn:uuid:{0}", Guid.NewGuid())),
                new XElement(atom + "updated", DateTime.Now),
                new XElement(atom + "author",
                new XElement(atom + "name", "Sam")),
                new XElement(atom + "content",
                  new XAttribute("type", "application/xml"),
                  content)
              );

            byte[] postContentBytes = Encoding.UTF8.GetBytes(entry.ToString());
            postStream.Write(postContentBytes, 0, postContentBytes.Length);
            postStream.Close();
            request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
        }

        private void GetResponseCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);
            string responseString = streamRead.ReadToEnd();   
            // Update some UI if needed.         
            streamResponse.Close();
            streamRead.Close();           
            response.Close();
        }

In addition to the native “HttpWebRequest”, the “WebClient” class also seems to get the job done; but you need to set the “WebClient.Headers” property correctly to make the right HTTP request. Check out this post (here) on details about HTTP communication from the Silverlight world. Anyway, here’s the corresponding code using WebClient class:


       private void PostUsingWebClient()
        {
            WebClient client = new WebClient();
            client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);

            XNamespace ds = "http://schemas.microsoft.com/ado/2007/08/dataservices";
            XNamespace dsmd = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
            var content =
              new XElement(dsmd + "properties",
                new XElement(ds + "Name", "Test"),
                new XElement(ds + "TwitterHandle", "@Test")
              );

            XNamespace atom = "http://www.w3.org/2005/Atom";
            var entry =
              new XElement(atom + "entry",
                new XElement(atom + "title", "A new team member"),
                new XElement(atom + "id", string.Format("urn:uuid:{0}", Guid.NewGuid())),
                new XElement(atom + "updated", DateTime.Now),
                new XElement(atom + "author",
                new XElement(atom + "name", "Sam")),
                new XElement(atom + "content",
                  new XAttribute("type", "application/xml"),
                  content)
              );

            client.UploadStringAsync(new Uri("YourODataEndpoint", UriKind.Absolute), entry.ToString());
        }
        
        void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                // Oops
            }
            else
            {
                // Success
            }
        }

So, that’s it. Performing CRUD operations against an OData service isn’t all that bad, is it?

Hope this was of some interest. Please drop comments.

Adios!

Advertisement

3 thoughts on “Updating OData data source from WP7 .. Part 2

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s