Tuesday, June 29, 2010

Querying WCF RIA Services and handling async results

I had a good question in the comments of Part 2 of my series on WCF RIA Services that I thought I would answer here so more could find the information.

To paraphrase the question: “How can I retrieve a set of entities into my client and process them when the retrieval is complete?

I didn’t go into detail on the latter part of this in the article because of space limitations. But basically the answer is that you can be notified of the completion of any async operation (Load and SubmitChanges primarily) that you call on a RIA Services domain context. There are really two programming models, but I’ll just show one for now as the other is very similar.

When you call DomainContext.Load, RIA services executes the retrieval in the background and the call is non-blocking to the calling thread. But often you need to get those results, do some processing on them, and then move on to make another query or do something else.

An easy way to handle things is to use an overload of Load (and SubmitChanges) that takes a callback delegate that will be called when the async work is complete. This model is slightly different than the two other familiar async patterns in .NET: BeginXXX/EndXXX method pairs and XXXAsync/XXXCompleted method/event.

In the RIA Services async pattern, you can pass an Action<XXX> delegate to the async method you are calling, and that target method will be executed when the async operation is complete. It will pass you results and any error information if applicable.

In the case of a Load call, the code would look something like this:

private void RetrieveAndProcessTasks()
{
    TasksDomainContext context = new TasksDomainContext();
    EntityQuery<Task> query = context.GetTasksQuery();
    Action<LoadOperation<Task>> completeProcessing = delegate(LoadOperation<Task> loadOp)
    {
            if (!loadOp.HasError)
            {
                ProcessTasks(loadOp.Entities);
            }
            else
            {
                LogAndNotify(loadOp.Error);
            }

    };
    LoadOperation<Task> loadOperation = context.Load(query.Where(t=>t.EndDate > DateTime.Now),completeProcessing,null);
}

private void ProcessTasks(IEnumerable<Task> entities)
{
    // Do what you need to do
}
private void LogAndNotify(Exception error)
{
    // Log the error and notify user if appropriate
}

Before calling Load on the domain context, you set up an Action<LoadOperation<T>> delegate to point to a handling method with the signature void TargetMethod(LoadOperation<T> op). In this example I do that with an anonymous method that checks whether there are any errors, and if not hands off to a processing method the entities that were returned. If there is an error, it hands off the Exception to a handling method as well.

Then it is just a matter of calling Load and passing that delegate as a second argument (the first is always the EntityQuery you want to execute), and a final argument that is an arbitrary state object, similar to the Begin/End signatures of the original .NET async pattern.

Unfortunately there is no easy way to shield you entirely from the async nature of service calls in Silverlight if you want to do something other than data bind to the results immediately after the load occurs.





Tuesday, June 29, 2010 8:10:25 PM (GMT Standard Time, UTC+00:00)
Comments [4]  | 


Tuesday, June 29, 2010 10:14:21 PM (GMT Standard Time, UTC+00:00)
Thanks for the help.

Took a little time and some trial and error (mostly error on my part) to get everything working right, but its working just fine now.

really apperciate your time and help.

Wednesday, June 30, 2010 5:01:28 AM (GMT Standard Time, UTC+00:00)
As soon as you play mixmaster, you will love it. A good game is precious, [url=http://www.ffxivgold.com]FFXIV Gil[/url] is precious in the mixmaster. It is as equip as the [url=http://www.ffxivgold.com]FFXIV Gold[/url]. Why not do some try and spend more time thinking how to get more [url=http://www.ffxivgold.com]FF14 Gil[/url]? If you do as I said just now, getting the [url=http://www.ffxivgold.com]FF14 gold[/url] will be so easy. In fact, most of the players are very clever, they are the stronger in making the [url=http://www.ffxivgold.com]Final Fantasy XIV Gil[/url].
Wednesday, June 30, 2010 5:01:40 AM (GMT Standard Time, UTC+00:00)
"fifa2gold" will bring a player after a century of war, the collapse of the world, to go, in such a world must learn self-protection and look for the fifa2 gold, restoration there are fighting. Player first and foremost task is to rebuild the social order and make enough fifa2 online gold. They also need to take care of their buy fifa2 gold. The game also allows players to carry cheap fifa2 gold, which can be controlled vehicle will also appear in the game. Camp, the game is now known to have fifa2 money, from trying to restore the old constitution's “Enforcer”.
Wednesday, July 07, 2010 12:47:16 AM (GMT Standard Time, UTC+00:00)
Microsoft Office 2007 can give you more convenient life.
Choose Office 2007 Professional is the most lucky thing in the world.
Many people like the Office 2007 Pro with their work.
Microsoft company invent the Office 2007 home for many people.
MS Office 2007 can change people’s life.
Office Professional 2007 is many people’s favorite.
Buy Office ultimate 2007 you can get many convenience.
The invention of Microsoft Office 2010 is a big change of the world.
By using Office 2010 Professional, you can save your money and time
Office 2010 Pro is the best software of all Microsoft
MS Office 2010 –save your time and save your money.
Many people like buy Office 2010 Home.
Office Professional 2010 is great!
Windows 7 Home makes life beautiful.

Comments are closed.















February, 2012 (1)
January, 2012 (1)
November, 2011 (4)
October, 2011 (1)
September, 2011 (2)
August, 2011 (1)
July, 2011 (1)
May, 2011 (5)
March, 2011 (4)
February, 2011 (2)
January, 2011 (3)
November, 2010 (4)
October, 2010 (1)
September, 2010 (5)
August, 2010 (5)
July, 2010 (6)
June, 2010 (8)
May, 2010 (2)
April, 2010 (2)
January, 2010 (1)
December, 2009 (3)
November, 2009 (2)
October, 2009 (3)
September, 2009 (3)
August, 2009 (2)
July, 2009 (3)
May, 2009 (3)
April, 2009 (2)
March, 2009 (1)
February, 2009 (2)
January, 2009 (2)
December, 2008 (1)
November, 2008 (2)
October, 2008 (5)
September, 2008 (4)
August, 2008 (2)
July, 2008 (1)
June, 2008 (2)
May, 2008 (2)
April, 2008 (3)
February, 2008 (6)
January, 2008 (3)
December, 2007 (1)
November, 2007 (1)
October, 2007 (5)
September, 2007 (1)
July, 2007 (3)
June, 2007 (8)
April, 2007 (2)
March, 2007 (4)
February, 2007 (1)
December, 2006 (2)
November, 2006 (9)
October, 2006 (5)
September, 2006 (3)
August, 2006 (2)
July, 2006 (4)
June, 2006 (5)
May, 2006 (10)
April, 2006 (4)
March, 2006 (2)
February, 2006 (12)
January, 2006 (7)
December, 2005 (2)
November, 2005 (15)
October, 2005 (6)
September, 2005 (7)
August, 2005 (3)
July, 2005 (10)
June, 2005 (11)
May, 2005 (7)
April, 2005 (8)
March, 2005 (6)
February, 2005 (2)
January, 2005 (6)
December, 2004 (3)
November, 2004 (5)
October, 2004 (2)
September, 2004 (5)
August, 2004 (13)
July, 2004 (6)
June, 2004 (14)
May, 2004 (17)
April, 2004 (12)
March, 2004 (8)
February, 2004 (10)
January, 2004 (14)
December, 2003 (9)
November, 2003 (13)
October, 2003 (3)





Sign In
Copyright © 2006-2007 Brian Noyes. All rights reserved.

designed by NUKEATION STUDIOS