Silverlight is a great tool that you can use to work with data retrieved from different services. With some knowledge of asynchronous programming and callbacks, you can use Silverlight to develop applications that are bound to data retrieved from different services that can scale over time. This article presents a brief overview of WCF and Silverlight and discusses how they can be used in application development to design and implement applications that can scale over time with intuitive user interfaces.
Pre-Requisites
To work with Silverlight 4 and Windows Communication Foundation 4 you should have the following installed in your system:
[login]
- Visual Studio 2010
- Silverlight 4 Tools for Microsoft Visual Studio 2010
To take advantage of the concepts discussed in this article, you should be familiar with all of the following:
- Microsoft Visual Studio 2010
- C# Programming Language
- .NET Framework 4.0
- Basic knowledge of Silverlight and/or WPF
Introducing Windows Communication Foundation (WCF)
Windows Communication Foundation (WCF) is a platform that can be used to design and implement platform independent, scalable services and provides support for unification of a number of enterprise technologies under one roof. The MSDN states, “Windows Communication Foundation (WCF) is Microsoft’s unified programming model for building service-oriented applications. It enables developers to build secure, reliable, transacted solutions that integrate across platforms and interoperate with existing investments.” Reference: http://msdn.microsoft.com/en-us/library/ms735119.aspx
Note that WCF was introduced as part of .NET Framework 3.0 in 2006 and was initially named “Indigo”. WCF 4 is now available as part of Visual Studio 2010.
Windows Communication Foundation 4.0 includes a lot of interesting new features. These include:
- Support for Simplified Configuration
- Support for Standard Endpoints
- Support for Discovery
- Support for Simplified IIS Hosting
- Enhanced Support for REST
- Support for Routing service
- Support for Workflow services
Silverlight 4 — A Quick Look
Silverlight is a browser plug-in that promotes a collaborative development environment of rich online media content that enables developers and designers alike to integrate multimedia and graphics into web pages within the context of the managed environment. Over the past few years, Silverlight, formerly known as Windows Presentation Foundation Everywhere (WPF/E), has become popular worldwide for developing the next generation of cross-browser, cross-platform Rich Internet Applications (RIAs).
Silverlight 4 provides a lot of interesting new features that help developers to build cutting edge applications with awesome multimedia and graphics. You now have support for printing, rich reporting and charting, support for web camera and microphone, support for COM automation, support for local file access, support for new languages, rich text editing, and, improved support for data binding to name a few.
Data binding Silverlight 4 Controls using WCF
Data binding is a term that implies binding data retrieved from a data source to data controls. You can use WCF Data Services to create and consume OData services for the web seamlessly. Microsoft states: “The Open Data Protocol, referred to as OData, is a new data-sharing standard that breaks down silos and fosters an interoperative ecosystem for data consumers (clients) and producers (services) that is far more powerful than currently possible. It enables more applications to make sense of a broader set of data, and helps every data service and client add value to the whole ecosystem.” Reference: http://msdn.microsoft.com/en-us/data/aa937697
WCF Data Services (previously known as ADO.NET Data Services), provides support for the Open Data Protocol in a managed environment. To bind data to Silverlight 4 controls using WCF, you should follow the steps outlined below:
- Create a new application in Visual Studio 2010.
- Select the project you have created in the solution explorer and click “Add New Item”
- Select ADO.NET Entity Data Model from the list of the project templates displayed
- Specify the data connection as needed and select the AdventureWorks database
- To expose the data retrieved through the Entity Data Model for the AdventureWorks database, create a new WCF Data Service
- Configure your data service accordingly so that it can be used to pull data out of the AdventureWorks database
Your data service code would look like as shown in the code snippet below:
public class MyDataService : MyDataService{ ??public static void InitializeService(DataServiceConfiguration config) ??{ ?????config.SetEntitySetAccessRule("*", EntitySetRights.All); ?????config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; ??}}
- Next, create a new Silverlight 4 application and add the service reference to it.
- Create a Grid and then use the service reference to bind data to the Silverlight Grid.
- Lastly, build and execute the project
That's it!
Data-binding Improvements in Silverlight 4
Before Silverlight 4, to bind data, you were restricted to have the object (that would be used to bind data) derive from the FrameworkElement class. With Silverlight 4, you can now bind data using any object that is inhertited from DependencyObject.
Silverlight 4 introduces some other interesting new improvements in DataBinding. These include:IDataErrorInfo support, string formatting support, TargetNullValue, FallbackValue support, etc.
The following code snippet shows how String formatting can be used while binding data in Silverlight 4 controls:
You can use TargetNull value while data binding to Silverlight 4 controls if the element can contain a null value. You can use FallbackValue if no data is available to bind to the data control. The following code snippet shows how TargetNull and FallbackValue can be used:
Suggested Reading
Here are a few links to that provide further references on this topic:
http://www.microsoft.com/silverlight/
http://www.silverlight.net/getstarted/silverlight-4/
http://www.silverlight.net/learn/tutorials/silverlight-4/using-wcf-ria-services/
http://msdn.microsoft.com/en-us/library/ee354381.aspx
http://msdn.microsoft.com/en-us/library/ee621313.aspx
http://wildermuth.com/2009/11/18/Data_Binding_Changes_in_Silverlight_4
http://msdn.microsoft.com/en-us/data/bb931106
http://msdn.microsoft.com/en-us/data/aa937697
Summary
Windows Communication Foundation is a Microsoft framework that can be used to develop scalable, service oriented, REST-based services. There have been quite a few enhancements in WCF 4.0 that has eliminated the pain of specifying tedious configuration details to host services. In this article we have discussed the basics of WCF and Silverlight and how we can use WCF data services to bind data to Silverlight controls. Happy reading!