RIA Services mainly targets Business Application Development, and comes across good for this purpose from a few different angles.

  • RIA Services simplifies the traditional n-tier application pattern by bringing together the ASP.NET and Silverlight platforms. If you have ever gone through the motions of creating a WCF Service, and then adding a service reference to your Silverlight project you will quickly find that the service calls are all asynchronous, and require you to specify an OnRequestCompleated method for calls that you make to that web service. While there are asynchronous programming patterns out there most business application developers prefer the tried and true N-Tier approach. Asynchronous Method calls make N-Tier Architecture difficult
  • As stated above Ria simplifies tiered architecture this means it is a great choice for creating applications using some of the more popular architectural patterns like MVVM, and MVC.
  • Because RIA Services is from Microsoft, you get tight Visual Studio Integration, and lots of Blogs, and Documentation covering it's use. While some of the open source projects out there are pretty good, most lake adequate documentation and have a steep learning curve.
  • Ria Services is flat out great for Rapid Application Development. There is Code Generation generation built into Visual Studio to generate Domain Services directly from ADO.NET Entities Models that your Silverlight applications can consume.

Where Can I Get WCF RIA Services

You can Get Wcf Ria Services From the Microsoft Web Platform Installer, located here.

  1. Highlight the Web Platform Tab
  2. Scroll Down to Tools
  3. Click the customize Tab
  4. and check off what you need

If you don't have Visual Studio go ahead and check off to get the Express Version. You obviously want to get the RIA Services Toolkit, and while you're here get the the Silverlight 4 Toolkit, and Tools for Visual Studio as well. After the install completes you will have everything you need to work with Silverlight 4 and RIA Services from within Visual Studio.


Options for Integrating WCF RIA Services into your Projects

There are a few different way out of the box to integrate WCF RIA Services into your projects using Visual Studio

  • Using the Silverlight Business Application Project Template
  • RIA Services in the Silverlight Application Project Template
  • Using the RIA Services Class Library Project Template

In this post we take a look at each of these approaches, talk about when and how they are useful, and show how implement each one. For this purpose I have created a Visual Studio Solution containing each implementation listed above
In the real world you are not likely to have a solution which implements any combination of these approaches, but for this example it is nice to have them all in one place to look at.

The Silverlight Business Application Project

If you are creating an application that requires Login, Registration, and User Authentication the Silverlight Business Application Project Template is a good way to go. It includes all this right out of the box. It also has website like layout and styles you can customize and some useful helper methods. You can actually learn a few things just by creating this project and then giving it a gook look over.

To create a Silverlight Business Application:

Right Click on your Solution and select "Add New Project", Select "Silverlight" from the Tree Menu on the Left, and then Select "Silverlight Business Application".
This will create a new two Projects in you solution automatically
You now have a Web Application Project to host your Silverlight Application and Domain Services, and The RIA Link between the two projects is all set up so you can begin adding and consuming WCF RIA Services. If you run the project now you will see all the out of the box features I mentioned are there.

  • Configurable Layout and Styles
  • Login
  • Registration

Not bad for right out of the box. I do have a couple of small issues with this though. Sure, it's great Microsoft has given us all this stuff without having to write a single line of code, but, I would have a couple things different if it were up to me.

  • 1. I wish the project template had a few config options upon creation. For example, for larger projects I may want all my services to be in a WCF RIA Services Class Library (RIA Class Library covered in detail below) in  instead of in the Web Application Project. Now I would have to move them.
  • 2. I'm not really sure what kind of Architectural Pattern they are trying to implement here. As you can see from the image of the Solution Explorer above there is a folder named "Models" in both the Web Application Project, and in the silverlight project. Then there is a Views folder in the Silverlight project that has views in it, but then no ViewModels or Controllers. Okay, so my point is what are they going for here? It makes me want to start moving the code around. If I'm not alone on this please leave me a comment. Anyway, small issues compared to the big picture.

WCF RIA Services Enabled Silverlight Application

If you don't need all the stuff that's included in the Silverlight Business Application above, or plan to just write them yourself, you can also enable RIA Services in a regular silverlight application.

To create a WCF RIA Services Enabled Silverlight Application:

Right Click on your Solution and select "Add New Project", Select "Silverlight" from the Tree Menu on the Left, and then Select "Silverlight Application".
When you click OK to add the new Silverlight Application a Dialog will appearer allowing you create a New Web Application to host your Silverlight Application in, or to host your Silverlight Application in an existing Web Project. Either way all you need to do is check the box to "Enable WCF RIA Services".
Then when your Project is created, it will already be set up to use RIA Services. You can now start adding domain services to your web project and the RIA link is in place and ready to go.

WCF RIA Services Class Library

The last option is the WCF RIA Services Class Library. In my experience the more separation you have in your solution the better. This project template allows you to house your Domain Services in separate assemblies.

To create a Solution that uses a WCF RIA Services Class Library:

Right Click on your Solution and select "Add New Project", Select "Silverlight" from the Tree Menu on the Left, and then Select "Silverlight Application".
When the project Dialog box appears allowing you to host your silverlight application in an existing or new Web Application, leave the "Enable WCF RIA Services" checkbox UN-CHECKED. The RIA Link will exist in the RIA Services Class Library.

Now right Click on your Solution and select "Add New Project", Select "Silverlight" from the Tree Menu on the Left, and then Select "WCF RIA Services Class Library".
This will actually create two projects. The RIA Services Class Library Consists of a Windows Class Library and a Silverlight Class Library and the two projects created contain the RIA Link. The project ending in .Web is the Windows Class Library and will run on the server while the other is the Silverlight Class Library will run on the client. Your solution should now look something like this.
All you need to do now is:

  • Add a reference to the Silverlight Class Library in your Silverlight Application
  • Add a reference to the Windows Class Library in your Web Application

And you are now set up and ready to start adding Domain Services to your server project for consumption in your Web Project. When using this set up you should add your domain services to you Windows Class Library section of your RIA Services Class Library.

Creating and Consuming Domain Services

Now that we know how to set up our RIA Services Project, Lets take a look at adding a Domain Service and consuming it in out Silverlight client to test that we have everything wired up correctly. In all of the scenarios listed above you will always add your Domain Service on the server side and consume that service on the Silverlight Client.
To show you how quick and easy it is to get a Domain Service up and running we are going to create a Domain Service form and ADO.NET Entities Data Model.

  • Start by Creating a Database. Mine consist of only one table with a very simple schema for this example.
  • Now right Click on your Web Project and select "Add New Item", Select "Data" from the Tree Menu on the Left, and then Select "ADO.NET Entity Data Model".
  • Highlight "Generate From Database" and click "Next".
  • Select your Target Database and click "Next".
  • Select the tables you want to generate the Model from and click "Finish".
  • Now that your Entities Data Model has been created do a Rebuild on the project before you do any thing else. This is important because if you forget to rebuild what will happen is when you go to create the Domain Service it will not be able to find your Entities. So go ahead and rebuild now and then we can create out first Domain Service.
  • Now right Click on your Web Project and select "Add New Item", Select "Web" from the Tree Menu on the Left, and then Select "Domain Service Class".
  • Click "Add" and you will get a Dialog Box Allowing you to generate your Domain Service from your entities. Select Enable Editing if you want it to Generate Update, Insert and Delete methods as well.
  • Now Click "OK" and it will generate your Domain Service class. It will look something like this.

    public class ContactDomainService : LinqToEntitiesDomainService  

        // TODO:  
        // Consider constraining the results of your query method.  If you need additional input you can  
        // add parameters to this method or create additional query methods with different names.  
        // To support paging you will need to add ordering to the 'Contacts' query.  
        public IQueryable GetContacts()  
            return this.ObjectContext.Contacts;  

        public void InsertContact(Contact contact)  
            if ((contact.EntityState != EntityState.Detached))  
                this.ObjectContext.ObjectStateManager.ChangeObjectState(contact, EntityState.Added);  

        public void UpdateContact(Contact currentContact)  
            this.ObjectContext.Contacts.AttachAsModified(currentContact, this.ChangeSet.GetOriginal(currentContact));  

        public void DeleteContact(Contact contact)  
            if ((contact.EntityState == EntityState.Detached))  

Notice it created services methods to preform all the CRUD opperations we need against our Database.
Now in our Silverlight project we will create a Grid in our MainPage.xaml to display our data like so:

<sdk:DataGrid x:Name="DataGrid" Margin="12" AutoGenerateColumns="True" />

Then in the code behind (MainPage.xaml.cs) we will add the code to call our domain service and populate our grid.

  • You will need to add a using directive to the Namespace in your server project where your domain services are located.
  • Create a private instance of your DomainContext
  • In the constructor call the load method of your context and pass in your GetContactsQuery from your Domain Service
  • Set the ItemSource Property of your DataGrid to the Collection of Contacts now available in your DataContext.

using SilverlightApplication3.Web;    
namespace SilverlightApplication3  
    public partial class MainPage : UserControl  
        private ContactDomainContext _context = new ContactDomainContext(); 

        public MainPage()  
            DataGrid.ItemsSource = _context.Contacts;  

Now hit F5 and run the application. TADA! You should now see your data in the grid!
I hope this helped you in getting started with .NET RIA Services. I will be covering more in future posts.