Replace WCF with RIA Services for Non-CRUD Operations

RIA services is one of the coolest technologies that have been introduced with Silverlight 3, and gets it boom with 4.  It just made a lot of things easier like,
  • Cross Domain Calls - Creating wrappers for the services in different domain had been made easier.
  • No need of instantiation and adding references.  So another workload is gone.
  • There is a provision for debugging an application consuming service and service together.
We all might know that RIA supports the basic CRUD operation, and is easier to deal with its queries.  But what if I am dealing with something other than CRUD? Let me tell you a scenario.

I have a WCF Hello world service, which has an operation which returns a string "Hello World" to the caller.  Now I decided to go for RIA over the WCF.  This is basically a small operation returning only a string. 

There is an attribute Invoke and you can mark the operation with that [By default it will be applied for most of the times, but explicit markup is considered a good practice].  Let me tell you step by step.  Open a new Silverlight application in Visual Studio and don't forget to mark enable RIA services.  

1.gif
 
Now add a Domain Service class to the asp.net project.

2.gif
 
Since we hadn't created any entity, it will go for an empty data service class.  Make sure that "enable client access" is checked.

3.gif
 
Now add your operation to Domain class.

4.gif
 
Now build the application to make it visible to Silverlight.  As you might have read, the service will be exposed as context in Silverlight app.  A common mistake that I do here is forget about namespaces.  Thanks to VS, its always fixed.  Coming back, we consume the service like this

5.gif
 
And here is the result

6.gif
   
This is just for demo and you can even invoke the complex WCF operations like this.  If you want the source code, here it is.

Up Next
    Ebook Download
    View all
    Learn
    View all