I would like to add this basic introduction to WCF where I create a Silverlight Application hosted in a ASP.NET Web Application and a WCF Service to it.
I explain it here using Visual Studio 2008, but you may also use Visual Studio 2010.
If you don’t have Silverlight development toolkit, I would recommend the following download:
Silverlight 3 tools for Visual Studio 2008 SP1
http://www.microsoft.com/downloads/details.aspx?familyid=9442b0f2-7465-417a-88f3-5e7b5409e9dd&displaylang=en
Also, Silverlight Web Platform installer in case you haven’t done this previously.
http://www.microsoft.com/web/gallery/install.aspx?appid=silverlight4tools;silverlight4toolkit;riaservicestoolkit
Don’t forget to install the Silverlight Toolkit. You can find it here.
http://silverlight.codeplex.com/
I installed Silverlight 3 November 2009 tools for VS 2008 and think its pretty stable.
In case you find debugging errors saying you will need Silverlight Managed debugging package (I mean after installing the toolkit and the web platform installer).
Something like this.

Try reinstalling the Silverlight Package (Visual Studio tools, SDK and Web Platform Installer or Silverlight Developer Runtime ).
Though reinstalling the Silverlight 3 stuff will provide a straight forward solution, if you find the errors coming up again. Do the following:
Download the chainer package. Go to command line and then to the directory where you downloaded the chainer.
Execute this command: silverlight_chainer.exe /x:SLChainer \Folder
The package gets extracted in the <currentDirectory>\SLChainer\Folder location.
Run the Silverlight 2.0 executable in case you have Silverlight 2.0 or less or the Silverlight SDK if you have Silverlight 2.0 installed.
Try giving a restart to the system. You should be fine.
Alright now. After the above steps, you should be good to start a Silverlight Application hosted in ASP.NET website.
Open a new project as usual.

Select Silverlight and choose Silverlight Application in the right pane

Note: If you have Silverlight 2.0, you will be able to create Silverlight Navigation Application only.
Select Host the project in a new web site. (You can also uncheck the checkbox, but you will need to run your Silverlight (SL) Application out-of-browser in order to make the WCF service run and to avoid any Communication Exceptions.
So to keep it simple, lets start by hosting the SL Applicatoin in a new website.

Alright now. We are all set to rock and roll. 🙂
Now open the MainPage.xaml and create a textblock and a button inside a StackPanel.
Note: though VS2010 shows us the designer view of the SL Page, as a best practice we will write the MainPage.xaml without looking at the Designer view.
Now. This is my MainPage.xaml
<UserControl x:Class="WCFIntroduction.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<Grid.Background>
<LinearGradientBrush>
<GradientStop Color="Azure" Offset="0.5" />
<GradientStop Color="White" Offset="0.2" />
</LinearGradientBrush>
</Grid.Background>
<StackPanel x:Name="StackPanel">
<TextBlock x:Name="txtblock" Text="Text" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Button Content="Click Me!" Click="btn1_Click"></Button>
</StackPanel>
</Grid>
</UserControl>
I just created a textblock control and a button control giving them appropriate names and declaring an event handler for the button’s Click event.
Now, add the WCF service to the ASP.NET Web Site by right-clicking the project and Add-> Add New Item-> Silverlight Enabled WCF Service

You can also add the regular WCF Service. It works too.

Having added the Service, I add the following code.
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
namespace WCFIntroduction.Web
{
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WCFService
{
[OperationContract]
public string PrintTime(string txt)
{
return string.Format("The Time now is: {0}", DateTime.Now);
}
// Add more operations here and mark them with [OperationContract]
}
}
This code returns a string with a Time Stamp on it. Having created the Service, Build the solution and check for errors. When the service is error-free, right click the service and select View in Browser. This is essential because, if you miss this step, the port for the service will not be opened and you will encounter errors while adding the service to the Silverlight Application.
If your service is good, it should look something like this when you view it in a browser.

You can click the link, and view the WSDL code for the service you added.
Now its time to add the service to the Silverlight Application. In order to do that, right-click the SL Project or the Service Reference folder and select Add Service Reference.
You should see the following dialog box.

Now click the Discover button to discover the services inside the Solution.
Note: The address in the address combo-box will be detected automatically if you view the Service in a web browser. If you find errors while discovering the solution, it means the port has not been opened for the web service. It will be resolved if you view the web service in the browser.
Having added the service, add a reference to your MainPage.xaml.cs code, and write the code to wire up the Service to the SL Application.
The basic steps to add a service to any project is :
– To instantiate the Service client you just created.
– Declare Event Handler(s) to the methods (or) Operational Contracts you created in the Service.
– Call the Async method for the event you just added.
– Define the event handler you declared previously.
The following code shows how you can wire up a service to an application (the SL Application in our case).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WCFIntroduction.ServiceReference1;
namespace WCFIntroduction
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void btn1_Click(object sender, EventArgs e)
{
WCFServiceClient client = new WCFServiceClient();
client.PrintTimeCompleted+=new EventHandler<PrintTimeCompletedEventArgs>(client_PrintTimeCompleted);
client.PrintTimeAsync(txtblock.Text);
}
private void client_PrintTimeCompleted(object sender, PrintTimeCompletedEventArgs e)
{
txtblock.Text = e.Result;
}
}
}
Having done this, we are all set to debug our project. After building and knowing that the code is error-free, its F5 time.
Debug and you should see something like this.
When you click the button, you should see this
You might encounter Communication Exception while debugging if you have your Silverlight Application as your Start-up project and not having it to Out-of-Browser.
In order to resolve this, set the ASP.NET Application as the Startup Project or set the Silverlight Application to work out-of-browser.
To make the SL Application work out-of-browser, right click the project, Select Properties and in the Silverlight tab, check the Enable running application out of browser.
This is the introduction to create a basic WCF Service and adding it to a Silverlight Project. Hope this post was useful.