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

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

  • Comments 1

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"

http://www.microsoft.com/downloads/details.aspx?FamilyId=c22d6a7b-546f-4407-8ef6-d60c8ee221ed&displaylang=en

 

 

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.

 

    [ServiceContract]

    public interface IFirstService

    {

        [OperationContract]

        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:

    [ServiceContract]

    public interface IFirstService

    {

        [OperationContract]

        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();

        }

        #endregion

    }

 

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.

 

        <services>

            <service behaviorConfiguration="FirstSilverlight.Web.FirstServiceBehavior"

                name="FirstSilverlight.Web.FirstService">

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

                    <identity>

                        <dns value="localhost" />

                    </identity>

                </endpoint>

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

            </service>

        </services>

 

 

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();

        }

        #endregion

    }

 

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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="500" Width="500">

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

        <Grid.RowDefinitions>

            <RowDefinition Height="30" />

            <RowDefinition Height="*" />

        </Grid.RowDefinitions>

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

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

    </Grid>

</UserControl>

 

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()

        {

            InitializeComponent();

        }

 

        private void Search_Click(object sender, RoutedEventArgs e)

        {

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

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

           client.GetAllAuthorsAsync();

        }

 

        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.

 

*** SILVERLIGHT ***

"The Official Microsoft Silverlight Site"

http://silverlight.net/Default.aspx

"Get Started Building Silverlight 2 Applications"

http://silverlight.net/GetStarted/

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

http://weblogs.asp.net/scottgu/pages/silverlight-2-end-to-end-tutorial-building-a-digg-search-client.aspx

 

*** WCF ***

"Windows Communication Foundation"

http://msdn.microsoft.com/en-us/library/ms735119.aspx

"What Is Windows Communication Foundation?"

http://msdn.microsoft.com/en-us/library/ms731082.aspx

"Introduction to Building Windows Communication Foundation Services"

http://msdn.microsoft.com/en-us/library/aa480190.aspx

 

***LINQ ***

"LINQ to SQL"

http://msdn.microsoft.com/en-us/library/bb386976.aspx

"Language-Integrated Query (LINQ)"

http://msdn.microsoft.com/en-us/library/bb397926.aspx

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

http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

 

 

Leave a Comment
  • Please add 3 and 6 and type the answer here:
  • Post