Create a LINQ/WCF/Silverlight application in 10 simple steps.

Ok, today I will show how to create a super simple application that uses some of the new stuff you find in .Net 3.5.

Try this and then you can throw cool words like Windows Communication Foundation (WCF), LINQ to SQL and Silverlight around you.

This is not intended to be a deep dive into any of these technologies, more a simple example of how easy it is to get started.

In this example we will create a table that contains some authors and books (feel free to use Sql Server 2008 and then you have yet another new technologyJ)

and then display it in a Silverlight application. The data will be collected from a WCF web service which uses LINQ to Sql.

Firstly, make sure you have downloaded and installed the Silverlight tools for Visual Studio. Found here.

"Microsoft® Silverlight™ Tools for Visual Studio 2008 SP1"

Step 1. Create the Sql table and insert some rows:

create table Authors (id int identity, title nvarchar(50), fname nvarchar(50), lname nvarchar(50))

insert into Authors values ('The first book','John', 'Johnsson')

insert into Authors values ('The cook book','Per','Persson')

insert into Authors values ('The paint book','Lee','Leeson')

insert into Authors values ('The computer book', 'Michael', 'Michaelson')

insert into Authors values ('The car book','Steve', 'Stevenson')

Step 2. Start Visual Studio 2008 and create a new Silverlight Application project, call it “FirstSilverlight”, and select the “Add a new ASP.NET Webproject to the solution option”.

This will create all you need, including test pages for the Silverlight application.

Step 3. Create the LINQ classes. Do this by right clicking the “FirstSilverlight.Web” project and select Add->New Item->Data->LINQ to SQL classes, call it “FirstLinq.dbml” and OK.

This opens the designer, now just open the server explorer and select the Author table you just created and drag and drop it onto the designer surface.

Step 4. Create the WCF service. Do this by right clicking the “FirstSilverlight.Web” project and select Add->New Item->WCF Service, call it “FirstService.svc” and OK.

This will create the IFirstService interface with the ServiceContract attribute, it should look like this.


    public interface IFirstService



        void DoWork();


We want the WCF web service to return a list of Authors, so remove the DoWork() method and add code for this, like so:


    public interface IFirstService



        List<Author> GetAllAuthors();


Step 5. Implement the Interface, this is super simple. Just go to the “FirstService.svc.cs” class, then right click the interface (IFirstService) and select “Implement Interface”.

Now you should have the following code generated for you (the DoWork has been removed).

    public class FirstService : IFirstService


        #region IFirstService Members

        public List<Author> GetAllAuthors()


            throw new NotImplementedException();




Step 6. Create a Service Reference to the WCF Service, do this by right clicking the References under the FirstSilverlight project (not the FirstSilverlight.Web project) and select “Add Service Reference”.

Now just click Discover (this will find the local services) and it should find “FirstService”, expand it and select the “IFirstService” interface. Name the reference: “AuthorReference” and click OK.

Step 7. Configure the web.config in the FirstSilverlight.Web project to use basicHttpBinding. Open it and go to the system.serviceModel section and change “binding” basicHttpBinding.


            <service behaviorConfiguration="FirstSilverlight.Web.FirstServiceBehavior"


                <endpoint address="" binding="basicHttpBinding" contract="FirstSilverlight.Web.IFirstService">


                        <dns value="localhost" />



                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />



Step 8. Create the LINQ statement that gets the Authors and sends it to the Silverlight application. This is done in the “FirstService.svc.cs” class, replace the NotImplementedException with this:

    public class FirstService : IFirstService


        #region IFirstService Members

        public List<Author> GetAllAuthors()


            FirstLinqDataContext db = new FirstLinqDataContext();

            var authors = from a in db.Authors select a;

            return authors.ToList();




Step 9. Set up the Silverlight application to collect the data. In the page.xaml file, remove all the existing content and paste this in.

It just creates a grid with two rows. Adds a button to the top row and a datagrid to contain the author data in the second.

<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="FirstSilverlight.Page"


   xmlns:x="" Height="500" Width="500">

    <Grid x:Name="LayoutRoot" Background="White">


            <RowDefinition Height="30" />

            <RowDefinition Height="*" />


        <Button x:Name="Search" Content="Get Authors" Click="Search_Click" Grid.Row="0"/>

        <data:DataGrid Name="AuthorDataGrid" Grid.Row="1"></data:DataGrid>



Step 10. Create code for the button that contacts the WCF service and gets the Authors and shows them in the datagrid. Open the backing file for the page.xaml file, ie page.xaml.cs

And simple write the code (or copy this code) like this:

    public partial class Page : UserControl


        public Page()




        private void Search_Click(object sender, RoutedEventArgs e)


           AuthorReference.FirstServiceClient client = new FirstSilverlight.AuthorReference.FirstServiceClient();

           client.GetAllAuthorsCompleted += new EventHandler<FirstSilverlight.AuthorReference.GetAllAuthorsCompletedEventArgs>(client_GetAllAuthorsCompleted);



        void client_GetAllAuthorsCompleted(object sender, FirstSilverlight.AuthorReference.GetAllAuthorsCompletedEventArgs e)


            AuthorDataGrid.ItemsSource = e.Result;



Run it by pressing F5, this should start IE, and display the page. Press the button and hopefully the Authors should be displayed.

So now you have a Silverlight application that contacts a WCF Web service that utilizes LINQ to get data from SQL Server and then sends it all back and displays it in the browser.

As mentioned, this is only intended to show how simple it is incorporate all the technologies, not explaining them.

However, each of these technologies offers a lot more and I encourage you to have a look at the documentation.


"The Official Microsoft Silverlight Site"

"Get Started Building Silverlight 2 Applications"

"Scott Guthrie - Silverlight 2 End to End Tutorial: Building a Digg Search Client"

*** WCF ***

"Windows Communication Foundation"

"What Is Windows Communication Foundation?"

"Introduction to Building Windows Communication Foundation Services"

***LINQ ***


"Language-Integrated Query (LINQ)"

"Scott Guthrie - Using LINQ to SQL (Part 1)"