When I first got the opportunity to write a MEWA (mesh-enabled web application) using the Live Framework November 2009 CTP, my first task was to create a Silverlight web app that would store user requests (claims) into the Live Mesh objects (Data Feeds). While there was some twist associated with the way you add Mesh objects into a Mesh hierarchy (by using an object reference), the solution was a pretty much straight forward. I'll show you here a quick code snippet that accomplishes the task of adding a new XML based claim into the user's Live Mesh Data Feed.

In the Live Framework November 2008 CTP:

//Get the current Mesh Application Service
MeshApplicationService meshApp = Application.Current.GetMeshApplicationService();

//Create a new Data Feed
DataFeed claimFeed = new DataFeed("Claims");

//Add a data feed object by reference
meshApp.DataFeeds.Add(ref claimFeed);

//Create a new Data Entry object
DataEntry entry = new DataEntry("Claim");

//Write a Linq XML stream to the data entry object
entry.Resource.SetUserData<string>(claim.WriteToXml().ToString(System.Xml.Linq.SaveOptions.None)); entry.Update();

//Add data entry object to Data Entries collection passing it by reference
claimFeed.DataEntries.Add(ref entry); claimFeed.Update();

Well, with recent release of the Live Framework April 2009 CTP, we've got a bunch of new functionality and support, along with a long list of "legacy" methods and classes being depreciated. To find more details on what new features have been added and what classes and methods have been removed, see this post: http://blogs.msdn.com/liveframework/archive/2009/03/11/live-framework-updated.aspx

The main change I found in this new CTP release, is that all sync roundtrip calls to the Live Mesh server have been replaced with asynchronous methods. For instance, instead of using DataFeeds.Add() method to add a new Data Feed object, now you have to use a sequence of asynchronous calls and in the correct order. Being mostly on my own facing this new challenge (it is needless to say that after I redirected my project to reference the new Live Framework assemblies, my project build was broken), I spent about 2 days to figure out the correct sequence and the right methods to call when adding a new Data Feed Entry object into my Live Mesh. Below, is the snippets of the sequence calls you must perform in order to achieve the same objective you saw earlier in this post.

In the Live Framework April 2009 CTP:

1.      Step 1: Add DataFeed to the DataFeeds collection

       MeshApplicationService meshApp = Application.Current.GetMeshApplicationService();

       DataFeed claimFeed = new DataFeed(claim.RefNumber);

       meshApp.DataFeeds.AddCompleted +=new EventHandler<LiveItemEventArgs<DataFeed>>(DataFeeds_AddCompleted);

       meshApp.DataFeeds.AddAsync(claimFeed, claimFeed);

 

2.      Step 2:  Load DataFeeds (upon DataFeeds_AddCompleted() event handler call)

       MeshApplicationService meshApp = Application.Current.GetMeshApplicationService();

       meshApp.DataFeeds.LoadCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(DataFeeds_LoadCompleted);

       meshApp.DataFeeds.LoadAsync(e.UserState);

 

3.      Step 3: Load a newly added DataFeed (upon DataFeeds_LoadCompleted() event hander call)

       DataFeed claimFeed = (DataFeed)e.UserState;

       claimFeed.LoadCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(claimFeed_LoadCompleted);

       claimFeed.LoadAsync(claim.RefNumber);

 

4.      Step 4: Get loaded DataFeed and add a new DataEntry to its DataEntries collection (upon claimFeed_LoadCompleted() event handler call)

       MeshApplicationService meshApp = Application.Current.GetMeshApplicationService();

 

       //Verify that the new DataFeed object is loaded

       int count = meshApp.DataFeeds.Entries.Where(f => f.Resource.Title == e.UserState.ToString()).Count();

       if (count <> 1) throw new Exception(String.Format("Found {0} identical Data Entries", count));

       //Get a new DataFeed

       DataFeed claimFeed = meshApp.DataFeeds.Entries.Where(f => f.Resource.Title == e.UserState.ToString()).Single();

 

       //then add a new DataEntry

       DataEntryResource entryResource = new DataEntryResource();

       entryResource.Type = "Claim";

       DataEntry claimEntry = new DataEntry(entryResource);

                   

       //Set the user data

       claimEntry.Resource.SetUserData<string>(claim.WriteToXml().ToString(System.Xml.Linq.SaveOptions.None));

 

       //Add DataEntry to the DataEntries collection

       claimFeed.DataEntries.AddCompleted +=new EventHandler<LiveItemEventArgs<DataEntry>>(DataEntries_AddCompleted);

       claimFeed.DataEntries.AddAsync(claimEntry, claimFeed);

 I would also recommend adding a public event to this class in order to notify the caller upon successful (or failed) submission of the request (claim)  to the Live Mesh.

 

Conclusion

 Hopefully this helps clarify how you should be expecting to use and operate Mesh objects in the new Live Framework April 2009 CTP release.

Another thing to remember, is that before you can do anything with DataFeed, DataEntries or DataEntry objects, they must be loaded within the Mesh Application domain space. Trying to operate on these objects before they were loaded, would result in exception thrown from your code.

I plan to follow up with more details on my demo featuring various Windows Azure Live Framework features I am writing these days, and will write up the summary of findings about new features and challenges each of us faces when exploring a new and fresh bits of code from our product team.