Aug 20

Written by: Michael Washington
8/20/2011 10:37 AM  RssIcon

image

LightSwitch is a powerful application builder when you use WCF RIA Services and Silverlight Custom Controls (or Silverlight Control Extensions). Otherwise, it is merely just a fast and easy application builder. In order to be fast and easy, LightSwitch has it’s limitations. However, when you employ WCF RIA Services and Silverlight Custom Controls, you are able to overcome any limitations.

When you use WCF RIA Services and Silverlight Custom Controls, there is nothing that LightSwitch cannot do, that you could do with a normal Silverlight application. The http://LightSwitchHelpWebsite.com site has a number of articles on Silverlight Custom Controls, this article will concentrate on WCF RIA Services.

Note: You must have Visual Studio Professional (or higher) to create WCF RIA Services using the method described in this article.

 

Why You Need WCF RIA Services

Let’s say you have this requirement:

Show the latest status for the warehouse (where the order is being placed)

This may not seem like a difficult requirement, except when the status for each warehouse is in a different table, and each table has a different structure. LightSwitch is able to handle this situation, just place each Entity (table) on the screen using a Tab Control, and allow the user to switch between the Tabs.

With a little programming, you may be able to automatically switch between the Tabs based on the order being viewed. This solution, like many “out-of-the-box” LightSwitch solutions, eats up a lot of “screen real estate” (also, if you have a lot of warehouses you would eventually run out of room on the screen).

Furthermore, a big complex user interface is difficult for users to learn, and time-consuming to use, because the users have to switch between screens and Tab containers.

An optimal solution is to simply show the status of the warehouse as the order is being placed… Smile

 

The LightSwitch Application

image

We create a new LightSwitch application.

 

image

image

We create two different Entities (tables) to hold the status of each warehouse. Notice they have different structures. In the “real world” we are faced with situations like this (perhaps each warehouse uses a different application to report their status).

Yes we could create a SQL Server View to tie the two Entities together, but a WCF RIA Service will also allow you to update the data if you needed to.

 

image

When we complete the application, the user is able to manually switch tabs to see the status for the warehouses. However, what if there were dozens of warehouses?

 

The WCF RIA Service

We follow the directions, to create a WCF RIA Service, in this article: WCF RIA Service: Combining Two Tables.

We use this class to return the results:

 

    public class WarehouseStatus
    {
        [Key]
        public int ID { get; set; }
        [Key]
        public string Warehouse { get; set; }
        public string StatusMessage { get; set; }
        public DateTime EffectiveDate { get; set; }
    }

 

(notice that two fields are marked [Key] because LightSwitch needs to know what fields comprise a unique key for each record)

We must have a method that returns the collection that does not take parameters:

 

        [Query(IsDefault = true)]
        public IQueryable<WarehouseStatus> GetAllStatusMessages()
        {
            var colNorthWarehouses = from NorthWarehouses in this.Context.NorthWarehouses
                                     select new WarehouseStatus
                                     {
                                         ID = NorthWarehouses.Id,                                         
                                         Warehouse = "North",
                                         StatusMessage = NorthWarehouses.Status,
                                         EffectiveDate = NorthWarehouses.EntryDate
                                     };
            var colSouthWarehouses = from SouthWarehouses in this.Context.SouthWarehouses
                                     select new WarehouseStatus
                                     {
                                         ID = SouthWarehouses.Id,                                         
                                         Warehouse = "South",
                                         StatusMessage = SouthWarehouses.StatusMessage,
                                         EffectiveDate = SouthWarehouses.EffectiveDate
                                     };
            return colNorthWarehouses.Union(colSouthWarehouses).AsQueryable();
        }

 

Yes this method could also be called by normal LightSwitch code that uses normal Linq queries, however, we will demonstrate how to make a method that takes a parameter for those cases where you must have a method that takes a parameter, such as when the method calls a stored procedure.

The following is the method that takes a parameter:

 

        public IQueryable<WarehouseStatus> GetAllStatusByWarehouse(string Warehouse)
        {
            var colWarehouses = from Warehouses in GetAllStatusMessages()
                                where Warehouses.Warehouse == Warehouse
                                select Warehouses;
    
            return colWarehouses;
        }

 

 

Note, you also need to add the following method for any paging of the collections to work:

 

        // Override the Count method in order for paging to work correctly
        protected override int Count<T>(IQueryable<T> query)
        {
            return query.Count();
        }

 

image

First, we build the WCF RIA Service project (ignore any warnings you may see in the output window).

Next, we add the new Data Source.

 

image

We select WCF RIA Service.

 

image

We Add Reference.

 

image

We select the WCF_RIA_Project.

 

image

Note, you may have to wait up to a minute for the Service Class to display in the list box.

 

image

Select the collection and click Finish.

 

image

The Entity (table) will display.

 

image

It will also display in the Solution Explorer. Also note that the method that takes a parameter (GetAllStatusByWarehouse) also displays.

LightSwitch treats the GetAllStatusByWarehouse method as it would any other Pre-process Query, the only difference is that you cannot edit it in the normal LightSwitch editor (you must alter the code in the method in the WCF RIA project).

 

image

On the Screen we can Add Data Item…

 

image

… and select the GetAllStatusByWarehouse collection.

 

image

When it is added to the Screen’s collection (the Screen’s View Model), it indicates that it has a parameter that must be supplied.

 

image

We can click on the parameter and in the properties for the parameter, bind it to the SelectedItem in the Orders collection.

 

image

We select the Warehouse field.

 

image

We can also edit the query for the GetAllStatusByWarehouse collection…

 

image

… and set the Sort.

We could also set additional parameters to constrain the query. At this level, LightSwitch treats this data source as it would any other Entity.

 

image

When we run the application, we now have a user interface that will automatically show the status for the proper warehouse.

 

WCF RIA Services Are Part Of LightSwitch

If you avoid using WCF RIA Services with LightSwitch, it would be like buying a sports car and never going past 3rd gear. LightSwitch was designed to be used with WCF RIA Services. This is what allows LightSwitch to be used for professional applications.

You may decide, “I will just use FindControl(“MyTabControl”) and automatically switch Tabs”. The problem you will face, is that if there are any errors, you will not find them until the application is running (meaning you may never find them, your end-users will). If you use the methods described here, any errors will show up at “compile time”  (meaning the application will not run and it will show you exactly where any errors are).

In addition, the methods described here require LESS code than it would if you used another method.

 

Further Reading

WCF RIA Service: Combining Two Tables

Creating a Simple LightSwitch RIA Service (using POCO)

Saving Files To File System With LightSwitch (Uploading Files)

Using WCF RIA Services to connect to LightSwitch Security:

How to reference security entities in LightSwitch

Linking LightSwitch Data to Logged in Users

 

Download Code

The LightSwitch project is available at http://lightswitchhelpwebsite.com/Downloads.aspx

16 comment(s) so far...


Gravatar

Re: Using WCF RIA Services In LightSwitch To Simplify Your Application UI

Hi Michael,

It seems that the RIA query can only take a String as a parameter. Anything else, I get and error which attaching the RIA service to the LS application, say that the parameter must be nullable. Did I do something wrong?

Best regards,
CT

By CT on   8/31/2011 6:49 AM
Gravatar

Re: Using WCF RIA Services In LightSwitch To Simplify Your Application UI

@CT - You can also pass a nullable integer or a nullable bool and perhaps other nullable primative properties.

By Michael Washington on   8/31/2011 7:00 AM
Gravatar

Re: Using WCF RIA Services In LightSwitch To Simplify Your Application UI

Ha! This is what i've been trying to do.

But my is supposed to have a button for invocation. That is you click on a button and the method is call.

My whole approach of using a button on the command bar of an object of type ReportMonth { Year, Month}

could be wrong or long. All i want to do is call a stored procedure from my lightswtich - when click a button or do some action to invoke it. Then i could fetch my data (result of stored procedure) some other way possible.


Thanke for you help

By spidergeuse on   9/30/2011 4:23 AM
Gravatar

Re: Using WCF RIA Services In LightSwitch To Simplify Your Application UI

Rather than connect a Lightswitch app directly to a WCF RIA Service is it possible to hook up a Silverlight User Control to a WCF RIA Service?This way the uc can be imported into any lightswitch app / screen and consume data without any tweaking of LS apps?

By Paul Hale on   1/11/2012 12:42 PM
Gravatar

Re: Using WCF RIA Services In LightSwitch To Simplify Your Application UI

@Paul Hale - A Silverlight user control is always bound to a Screen collection. That collection can either be an internal data source, external data source, or a WCF RIA Service

By Michael Washington on   1/11/2012 12:44 PM
Gravatar

Re: Using WCF RIA Services In LightSwitch To Simplify Your Application UI

i have a question:
what can i do if the paramater in this ria services should be optional?
in my application the user only somtimes wants to filter the information in the query.
example:
public IQueryable GetLectureViewByDate(DateTime? start, DateTime? end, string audience,string district)
{
return this.Context.Activities.Where(a => a.ActivityDate >= start && a.ActivityDate a.Topic).Select(g =>
new LectureView()
{.....
});
}
what if the audience and district are parameters that i dont all the times want to filter by?
the only thing i always what to filter is ths date..
thank you for the help

By adi on   6/21/2012 4:12 AM
Gravatar

Re: Using WCF RIA Services In LightSwitch To Simplify Your Application UI

With Visual Studio LightSwitch 2012 you must make sure your WCF RIA Service is ".NET 4" NOT ".NET 4.5"

By Michael Washington on   8/20/2012 3:11 PM
Gravatar

Re: Using WCF RIA Services In LightSwitch To Simplify Your Application UI

Hi Micheal,
I followed your instructions on creating a WCF RIA service in LightSwitch.
In my project I work with an Oracle Database with an ADO.NET Entity Data Model.

I had a lot of struggle to establish a working project ;-(

Problem:
Some data in the Oracle database gets updated by the database itself (trigers), this creates a concurrency problem.
That's why I had to set up the WCF RIA service to overcome that problem.

I work with Visual Studio 12

Lucky I found the way to get it working, with help from you ;-).

Things to pay attention to:
- The WCF RIA project has to be .NET Framework 4.
- The created .tt files in project have to be deleted.
- In the properties screen of the .edmx file/part the "Code Generation Strategy" has to be set to "Default"
- In the .edmx file/part select a field from a table that will be update in the database and set in the properties screen the value of "StoreGeneratePattern" to "Identity" (for the Id) or "Computed" for the other fields. This garanties that the data of that field in the database will be returned to the screen collection, on an insert or update action. Otherwise you get an error message like this "The context is already tracking a different entity with the same resource Uri." The reason for this error was that the new added entries had a Id = 0.

I hope others can do something with this extra info.
Best Regards

Norbert Kramer



By Norbert Kramer on   9/5/2012 2:32 AM
Gravatar

Re: Using WCF RIA Services In LightSwitch To Simplify Your Application UI

@Norbert Kramer - Thank you for the answer to the "The context is already tracking a different entity with the same resource Uri" error. I got that error recently and I googled it and hit your comment. In my case I create a unique ID in my WCF RIA service, but in my screen behind code I must explicitly set a unique ID when I create the object that will later be passed to the WCF RIA Service insert method (this value will then be overwritten with the unique counter ID in the table of the underlying database).

By Michael Washington on   9/8/2012 1:35 PM
Gravatar

Re: Using WCF RIA Services In LightSwitch To Simplify Your Application UI

Hi Michael,

We want to deploy WCF RIA Service and the Lightswitch application on two separate servers.

How to we modify LightSwitch application so that it can pick up the location of the WCF RIA Service?

Cheers
Raj

By Raj on   5/27/2013 8:21 PM
Gravatar

Re: Using WCF RIA Services In LightSwitch To Simplify Your Application UI

@Raj - You will want to make a post to the official Microsoft Forums: http://social.msdn.microsoft.com/Forums/en-US/lightswitch/threads

By Michael Washington on   5/27/2013 8:22 PM
Gravatar

Re: Using WCF RIA Services In LightSwitch To Simplify Your Application UI

Hi Michael, thanks very much for this post; I have used it to create my WCF and the WCF is working fine. I have done everything as described; however, I am using a SilverLight Custom Control to display the data, but my binding in the custom control is not displaying the data (having problems with the custom control binding). Here is my XAML code: As you can see, I think I am binding the Grid and the TextBox correctly
















By Creo Abraham on   6/4/2013 2:53 PM
Gravatar

Re: Using WCF RIA Services In LightSwitch To Simplify Your Application UI

@Creo Abraham - You will want to make a post in the Official LightSwitch Forums at: http://social.msdn.microsoft.com/Forums/en-US/lightswitch/threads

By Michael Washington on   6/4/2013 2:54 PM
Gravatar

Re: Using WCF RIA Services In LightSwitch To Simplify Your Application UI

Hi Michael - I did, please use below link for my post - any help will greatly be appreciated; been at this for days now.

http://social.msdn.microsoft.com/Forums/en-US/lightswitch/thread/39059433-2685-435f-9d9d-8319dc220d23

By Creo Abraham on   6/4/2013 4:07 PM
Gravatar

Re: Using WCF RIA Services In LightSwitch To Simplify Your Application UI

When building a WCF RIA service I am using VS2013. Do I still need to create the WCF RIA Service as a "4.0" framework or can i use "4.5"? The above comment mentions using 4.0 but it is with VS2012. I wasn't sure if VS2013 can now use 4.5.

By Matt on   6/25/2015 11:03 AM
Gravatar

Re: Using WCF RIA Services In LightSwitch To Simplify Your Application UI

@Matt - See "Creating a WCF RIA Service for Visual Studio LightSwitch 2013" at: http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/2226/Creating-a-WCF-RIA-Service-for-Visual-Studio-2013.aspx

By Michael Washington on   6/25/2015 11:46 AM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 
Microsoft Visual Studio is a registered trademark of Microsoft Corporation / LightSwitch is a registered trademark of Microsoft Corporation