Silverlight 2.0 Application Development with LINQ to SQL and a WCF Service
This is my third article in a series focusing on Silverlight 2.0 (RC 1) development using Visual Studio 2008. In this article, we will create a simple “LINQ to SQL” model, access the model with a WCF Service and consume the same using a Silverlight 2.0 application. Technically, we will have Silverlight 2.0 controls accessing a regular WCF Service and fetch information from the database using the “LINQ to SQL” model.
This article assumes that you have a minimum knowledge of technologies like “LINQ to SQL,” “WCF,” “Silverlight,” etc. If you don’t, and you need step-by-step walkthroughs for the above technologies, please go through the following links.
For this article, I upgraded my development machine from “Silverlight 2.0 tools beta 2” to “Silverlight 2.0 tools RC 1.” My machine is now equipped with the following environment:
Windows Server 2003 with SP2
IIS
SQL Server 2008
Visual Studio 2008 (comes with .NET 3.5) with SP1
Microsoft Silverlight 2.0 Tools (RC 1) for Visual Studio 2008 (includes Silverlight, Silverlight SDK and Tools for VS 2008).
The entire source code for this article is available in the form of a free downloadable zip file. The solution was developed using Microsoft Visual Studio 2008 Team Edition (with SP1) with Microsoft SQL Server 2008 Developer Edition on Microsoft Windows Server 2003 Standard Edition (with SP3) with Silverlight 2.0 RC 1. I didn’t really test it in any other environment. I request that you post in the discussion area if you have any problems in execution.
As I mentioned earlier, if you are quite new to these technologies and need complete walkthroughs, consider reading my earlier articles (listed in the previous section).
The following are the steps you need to take to create the environment:
Create a blank solution (DemoSln).
Add “WCF Service Application” Project (DemoEmpService) to the solution.
Add “LINQ to SQL classes” item (DemoEmp.dbml) to the WCF project.
Using “Server Explorer,” drag and drop the “Emp” table onto the “LINQ to SQL” designer (of DemoEmp.dbml)
Go to properties of the current data context, and change “Serialization Mode” to “Unidirectional” (Fig 01). This automatically adds each “LINQ to SQL” entity (named “Emp”) with the necessary attributes (like Table, DataContract, Column, DataMember etc.) to be serialized (with respect to WCF standards).
Make sure the connection string is added to “web.config” file automatically.
Open “DemoEmpService” project and rename “Service1.svc” and “IService1.vb” to “EmpService.svc” and “IEmpService.vb” respectively (together with all of their associations). The steps are clearly explained in my previous article.
Configure the virtual path (using Project properties) to point to the local IIS and add ClientAccessPolicy.xml to IIS root (ex: c:inetpubwwwroot). The file is included in the attached source code and the steps are well explained in my previous article.
Open “IEmpService.vb” and modify the code so that it looks like the following:
<ServiceContract()> _
Public Interface IEmpService
<OperationContract()> _
Function GetEmployeeList() As List(Of Emp)
End Interface
Open “EmpService.svc” and modify the code as follows:
Imports System.Data.Linq
Public Class EmpService
Implements IEmpService
Public Sub New()
End Sub
Public Function GetEmployeeList() As System.Collections.Generic.List(Of Emp) Implements IEmpService.GetEmployeeList
Using ctxt As New DemoEmpDataContext
ctxt.ObjectTrackingEnabled = False
Return ctxt.Emps.ToList
End Using
End Function
End Class
Build your solution and test “EmpService.svc” using “WCF Test client” (explained in my previous article). You can also test the service using a WinForm application with the following code (the Win Form project is included in the attached source code):
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Now we need to develop a Silverlight 2 application which consumes the previously mentioned WCF Service. You need to take the following steps.
Add a new “Silverlight Application” project to the existing solution and name it “DemoSL.” You should get two projects, “DemoSL” and “DemoSL.Web” (as explained in my previous article).
Configure the virtual path of “DemoSL.Web” to point to your local IIS (just as you did in the previous section).
Add “Service Reference” to the “WCF Service” project available within your solution.
Go to File || Add || New Project (to the existing solution).
Configure “ServiceReferences.ClientConfig” in “DemoSL” accordingly (explained in previous article). Rebuild the solution.
Modify your code in Page.xaml, so that it looks like the following:
Once the datagrid control is added to the Silverlight application, you should have an assembly named “System.Windows.Controls.Data” added to your reference list (if not, you should add it manually). You should also observe the namespace “my” added to the “UserControl” tag.
Go to the code-behind of “Page.xaml” and modify the code to look like the following:
Partial Public Class Page
Inherits UserControl
Public Sub New()
InitializeComponent()
End Sub
Private Sub btnShow_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnShow.Click
The following are the important configurations which you need to remember for troubleshooting errors during execution. All of the following are customized according to the current application (and are different from the default configurations added by Visual Studio).
“LINQ to SQL” configuration to work with WCF Service:
The “Serialization mode” property of “DataContext” should be changed to “Unidirectional” (using "LINQ to SQL Designer”).
the “ObjectTrackingEnabled” property of the “DataContext” object (in your programming) must always be set to “false.”
WCF configurations:
Configure the WCF service to the local IIS (with both Integrated Security and Anonymous access options in security) using the IIS snap-in. Make sure that it is configured with ASP.NET 2.0 runtime.
Make sure that connection string information is properly included in your web.config.
Make sure that the debug is set to true in web.config as shown below:
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
These troubleshooting configurations are continued from the previous section.
Silverlight application configurations:
Configure the Silverlight application (DemoSL.Web) to the same IIS root as the WCF service discussed above (and also with the same options). This is just to minimize errors during development. However, it could be totally different during deployment.
Make sure that MIME types are added as shown below
Make sure that you add “Service reference” to the WCF project in the current solution for better debugging capabilities.
Make sure that “ClientAccessPolicy.xml” is added to IIS root (usually, c:inetpubwwwroot) as follows:
<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*" />
</allow-from>
<grant-to>
<resource include-subpaths="true" path="/" />
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
Make sure that “ServiceReferences.ClientConfig” is provided as shown below:
“vwin2k3” is my machine name. You need to replace to yours.
Make sure that the debug is set to true in web.config (in DemoSl.Web)
In my upcoming articles, we will see more and more examples of Silverlight 2.0 development together with LINQ to SQL and WCF. I hope you enjoyed the article and any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com