How to Use WCF RIA Service in LightSwitch 2012

In this article you will learn how to make a WCF RIA Service and how to consume that using Visual Studio LightSwitch 2012.

What is WCF RIA Service

RIA services is a server-side technology that automatically generates client-side (Silverlight) objects that take care of the communication with the server for you and provide client-side validation. The main object inside a RIA service is a DomainService, usually a LinqToEntitiesDomainService that is connected to a LinqToEntities model.

Getting Started

  1. Open Visual Studio 2012.
  2. Go to "File" => "New" => "Project..."
  3. Select "LightSwitch" in installed templates.
  4. Select "LightSwitch Application (Visual C#)".
  5. Enter the Name and choose the location.
  6. Click "OK".

First of all let's create a new Class Library, right-click on the solution and "Add" => "New Project".

img1.jpg

Image 1.

Now add a new ADO.NET Entity Data Model using right-click and click "Add" => "New item".

img3.jpg

Image 3.

Choose Model Contents and click "Next".

img4.jpg

Image 4.

Choose your data connection.

img5.jpg

Image 5.

Now choose your database objects and settings and click "Finish".

img6.jpg

Image 6.

img7.jpg

Image 7.

Now right-click on the Class Library and click "Add" => "New Item" and select "Domain Service Class" and edit the name then click the "Add" button.

img8.jpg

Image 8.

img9.jpg

Image 9.

If you don't see your context class in the drop down, then read this:

With Visual Studio 2012, Entity Framework now creates "DbContext" based context classes by default, but WCF RIA Services does not support "DbContext" based context classes. In order to utilize your Entity Framework model with WCF RIA Services, you must convert it to an "ObjectContext" based model. This can be done using the following steps:

1. Open your entity model in the designer, change the "Code Generation Strategy" from "None" to "Default"
2. Delete the two ".tt" files that are adjacent to the model

3. Rebuild the project

img10.jpg

Image 10.

After following those steps, you will be able to select your entity model's context class in the "Available context classes" list. The side-effect of this procedure is that you have now converted your entity model from an Entity Framework DbContext-based model to an ObjectContext-based model. Now again add a new Domain Service Class and click "Ok".

img11.jpg

Image 11.

As you can see, your data context class in drop and uncheck Enable client access and select the entity and check "Enable editing" if you want to edit and click OK.

Now open the domain service class and add Isdefault.

// 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 'Customers' query.

      [Query(IsDefault=true)]

        public IQueryable<Customer> GetCustomers()

        {

            return this.ObjectContext.Customers;

        }

Now let's start work on the data source and screens. Add a new Data Source.

img12.jpg

Image 12.

Select "WCF RIA Service" and click "Next".

img13.jpg

Image 13.

img14.jpg

Image 14.

Now click on "Add Reference" and click "Next".

img15.jpg

Image 15.

Select the "Projects" tab and do check class library assembly and click OK.

Note: if you are using Framework 4.5 then perhaps the assembly reference won't display; if so then you must change Framework 4.5. To 4.0 in the Class Library.

img16.jpg

Image 16.

Click "Next".

img17.jpg

Image 17.

Now select "Entities" and click "Finish".

The solution structure will look like this:

img18.jpg

Image 18.

Now open App.Config and copy the connection string key.

<add name="NORTHWNDEntities1" connectionString="metadata=res://*/CustomerModel.csdl|res://*/CustomerModel.ssdl|res://*/CustomerModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=RAJ-PC;initial catalog=NORTHWND;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

img19.jpg

Image 19.

Now let's work on screens.

Add a new Editable Grid Screen and Screen data drop down list and click OK.

img20.jpg

Image 20.

Finally it is time to run the project.

img21.jpg

Image 21.

Click on the "Add" icon.

img22.jpg

Image 22.

Click on the "Edit" icon.

img23.jpg

Image 23.


Similar Articles