Using WCF RIA Services with your POCO, Part 3: Calling the Domain Service form a Silverlight Client

by mheydt 13. December 2009 22:18 >
In part 2 I showed how to create a domain service and get the code generated for that service (heck that was automatic).  In this post I'll show how to call that service from Silverlight.

In the the first post I added a reference to the Silverlight project to the Silverlight.RIA project, which will allow the Silverlight application to use those classes.  I modify the MainPage.XAML to have a TextBox named 'result' and modify the MainPage.XAML.cs file to be as follows to call the service and display the result:



What's going on here?  We'll, there is a whole new syntax to these calls, which I will explain briefly. 

On lines 14 and 15 I add two namespaces as they are needed for the InvokeOperation (System.Windows.Ria), and the SocialBus.RIA.Web is the namespace in our RIA web project that contains the domain service.

The domain service we created is implemented by the SocialBusDomainContext class.  It has a bunch of methods for managing entities, as well as the method I added to the class, 'Add'.  To call the service, I create a new instance of this class, and call the 'Add' method.  As is the case in Silverlight, all calls are asynchronous, so to capture the result a delegate is assigned to the method calls return value's (which is an InvokeOperation object) Completed event handler.  I choose to use the lamba syntax for this, and the method is actually passed the InvokeOperation object and an EventArgs object.  I cast the 's' parameter to InvokeOperation<double> (the generic is important), get the return Value, convert it to a string and assign it to the text box on the UI.

Sure, there is a lot more detail that can be covered here, such as error/exception handling and such, but this show the basics of calling the service and using the result.  In the next post, I'll show how to create a POCO and pass it to the client.

Tags:

RIA | Silverlight | SocialBus | WCF

blog comments powered by Disqus

about the author

I'm a .NET, XAML, and iOS polyglot that loves playing with new things and making cool and innovative stuff.  I am also a Mac junkie.

I am Principal Technologist for SunGard Global Services in NYC, in their Advanced Technologies practice, and I work extensively with SunGard's energy and financial customers.

Note the the posting on this blog are my own and do not represent the position, strategies or opinions of SGS.

twitter

Wow tonight's surfing has landed me on YouTube / rush / Exit Stage Left / Time to old school it! Wait, that was high school for me! LOL :)
Saturday 12:08AM via Microsoft
The Rise &Rise of the World's Biggest Cult Band http://t.co/emjoC4UL
Friday 8:49PM via Microsoft
I'm at Outback Steakhouse (Wilton, CT) w/ 2 others http://t.co/voxX9aNL
Friday 5:01PM via foursquare
@jeremylikness: Presentation patterns for #XAML based applications http://t.co/tl493BsR #wpf #Silverlight #windows8 #Metro #WinRT#mhtnd
Thursday 7:46AM via Twitter for Mac
I'm at Freepoint Commodities LLC (Greenwich, CT) http://t.co/ggTIcTGO
Monday 11:08AM via foursquare
Follow me on Twitter

recent comments

None

month list