In this article you will learn how to make a WCF RIA Service and how to consume that using Visual Studio 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 ServiceRIA 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
First of all let's create a new Class Library, right-click on the 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 the Class Library and click "Add" => "New Item" and select "Domain Service Class" and edit the name then click the "Add" button.Image 8.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 model3. Rebuild the projectImage 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 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.Image 12.Select "WCF RIA Service" and click "Next".Image 13.Image 14.Now click on "Add Reference" and click "Next".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.Image 16.Click "Next".Image 17.Now select "Entities" and click "Finish".The solution structure will look like this: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="data source=RAJ-PC;initial catalog=NORTHWND;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" 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 it is time to run the project.Image 21.Click on the "Add" icon.Image 22.Click on the "Edit" icon.Image 23.
Printing in C# Made Easy