How to use WCF RIA Service in LightSwitch 2012

Datetime:2017-02-08 06:03:33         Topic: RIA  WCF          Share        Original >>
Here to See The Original Article!!!

In this article you will learn how to make 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 solution and Add new Project.


Image 1.

Now add a new ADO.NET Entity Data Model using right click and click add new item.


Image 3.

Choose Model Contents and click next.


Image 4.

Choose your data connection.


Image 5.

Now choose your database objects and settings and click finish.


Image 6.


Image 7.

Now right click on class library and click Add New Item and select Domain Service Class and edit the name and click Add button.


Image 8.


Image 9.

If you don't see your context class in 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


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.


Image 11.

As you can see your data context class in drop and uncheck Enable client access and select entity and check enable editing if you want to edit and click OK.
Now open 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.


        public IQueryable < Customer > GetCustomers()


            return this .ObjectContext.Customers ;


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


Image 12.

Select WCF RIA Service and click Next.


Image 13.


Image 14.

Now click on Add Reference and click next.


Image 15.

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

Note - if you are using framework 4.5 then may be assembly reference won't display so you have to change framework 4.5. To 4.0 in class library.


Image 16.

Click Next


Image 17.

Now select Entities and click Finish.

Solution structure will look like this.


Image 18.
Now open App.Config and copy 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 " />


Image 19.

Now let's work on screens.

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


Image 20.

Finally time to run the project.


Image 21.

Click on Add Icon.


Image 22.

Click on Edit Icon.


Image 23.