Using Visual Studio LightSwitch Applications with WCF RIA Services

 

LightSwitch is primarily targeted at developers who need to rapidly product business applications.
Visual Studio LightSwitch application could  connect to a variety of data sources. For now it can connect to database servers, SharePoint Lists and WCF RIA Services .
Connection with WCF RIA Services requires some settings that are not "out of the box".  Despite this it is a very often expected scenario and developers want to have a sample where step by step to create a LightSwitch application, using as data source WCF RIA Services. That is the reason to create this walkthrough.

Demo Application:

Requirements:

Steps to implement the application:

  • Create a sample database (HYPO database)
  • Create a WCF RIA Services Class Library
  • Add an ADO.NET Entity Data Model
  • Add a Domain Service Class
  • Create a Visual Studio LightSwitch application
  • Add a WCF RIA Services data source
  • Create a screen, using a WCF RIA Services data source

Steps to Reproduce:

  • Create a sample database

Create a database, named HYPO

 

Set a fields in the table “hippos”.

 

  • Create a WCF RIA Services Class Library, named RIAServicesLibrary1

 

  • Add an ADO.NET Entity Data Model

Delete generated Class1 and add an ADO.NET Entity Data Model using data from database “HYPO”.

 

  • Add a Domain Service Class

Create a Domain Service Class, named HippoDomainService using the created ADO.NET Entity Data Model (HYPOEntityModel).

 

LightSwitch applications require WCF RIA Data Source to has default query and key for entities.

Modify HippoDomainService class in  HippoDomainService.cs : add an attribute [Query(IsDefault=true)] to the method HippoDomainService.GetHippos().

   1: [Query(IsDefault=true)]
   2: public IQueryable<hippos> GetHippos()
   3: {
   4:     return this.ObjectContext.hippos;
   5: }

 

Add an attribute [Key] to the ID field in the hippos class (HippoDomainService.metadata.cs)

   1: internal sealed class hipposMetadata
   2: {
   3:  
   4:     // Metadata classes are not meant to be instantiated.
   5:     private hipposMetadata()
   6:     {
   7:     }
   8:  
   9:     public Nullable<int> Age { get; set; }
  10:  
  11:     [Key]
  12:     public long ID { get; set; }
  13:  
  14:     public string NAME { get; set; }
  15:  
  16:     public string Region { get; set; }
  17:  
  18:     public Nullable<decimal> Weight { get; set; }
  19: }

 

  • Create a Visual Studio LightSwitch application

Add a new LightSwitch application (C#) to the solution with WCF RIA Class Library:

 

  • Add a WCF RIA Services data source

 

Add a reference to RIAServicesLibrary1.Web and add HippoDomainService as data source.

 

Add the entity “hippos” as a data source object:

 

Ensure “hippos” properties:

 

Switch view type for the LightSwitch application to “File View” and display hidden files.

Open in RIAServicesLibrary1.Web   App.config file and copy the connection string to Web.config  in a LightSwitch ServerGenerated project. 

 

Connection string:

   1: <add name="HYPOEntities" connectionString="metadata=res://*/HYPOEntityModel.csdl|res://*/HYPOEntityModel.ssdl|res://*/HYPOEntityModel.msl; providerName="System.Data.EntityClient" /
   2: provider=System.Data.SqlClient;
   3: provider connection string=&quot;
   4: Data Source=.\SQLEXPRESS;Initial Catalog=HYPO;Integrated Security=True;
   5: MultipleActiveResultSets=True&quot;"
   6: ></connectionStrings>
  • Create a screen, using a WCF RIA Services data source

Add a new Search Data Screen, named Searchhippos using hippos entity:

 

Ensure screen properties:

 

Run the application: hippos data is displayed properly.

 

Modify hippos data.

 

 Source code of the demo application you could download here: LSRIADemo.zip


Comments  (3 )

bsarton
on Sat, Jan 22 2011 9:47 AM

Hi Mihail, thanks for this great post.

Would you please explain how all this should be dispatched in a 3-tiers scenario: database on a firewalled SQL server, services on a public iis server and lightswitch app on a remote client? Is this something that is possible as of lightswitch beta 1 ?

Also could you explain what would be the difference beetween your solution, and a standard 3-tier deployment that is announced by lightswitch for their release version ?

Thanks

Benoit

[Infragistics] Mihail Mateev
on Mon, Jan 31 2011 8:04 AM

Hi Benoit,

I didn't use this scenario, but hope that i can propose a sample next days. For me firewalled connection between SQL server and services is only question of settings.

Deployment could not include database in this case.

Hope it is much better to create a sample solution and post it here.

Thank you for the intersting question!

Mihail

Add a Comment

Please Login or Register to add a comment.