This post assumes you have installed the necessary pre-requisites for Silverlight 2 beta 1 and are using Visual Studio 2008.
Setup your solution
Open Visual Studio 2008
Click File > New Project
Select the Silverlight 2 project type
Select the Silverlight Application template
Provide an application name and click OK
You will be prompted to select whether to host your Silverlight application in a Web Site or Web Application Project. We’ll select Web Application Project
Add a WCF Service to the Web Application project
See my blog post on How to create and connect to a WCF Service in VS08 for instructions on setting up a WCF Service. Ignore the project setup …when you get to the section titled “Create the service proxy”, make sure to add the service reference to the Silverlight Application project.
You now have a Web Application, setup to host your Silverlight 2 application, which contains a basic WCF Service, and an empty Silverlight 2 application that is setup to connect to your WCF Service.
Now we’ll create a basic interface for your Silverlight Application.
Create the Silverlight interface
Use the following xaml markup for your interface:
<Grid x:Name="GrdService">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="200" />
</Grid.ColumnDefinitions>
<WatermarkedTextBox x:Name="TxtName" Grid.Column="0"
Watermark="Enter name here..."
HorizontalAlignment="Center"
Width="190"
VerticalAlignment="Center"
Height="30"
Margin="5" />
<Button x:Name="BtnCall" Grid.Column="1"
Content="WFC Call"
HorizontalAlignment="Left" />
<Border Grid.Column="2"
BorderThickness="1"
BorderBrush="Black"
Margin="5"
HorizontalAlignment="Center"
Width="190">
<TextBlock x:Name="TxtResponse"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
</Border>
</Grid>
Add the Width and Height properties to the UserControl tag of the Page.xaml file. Set the Width to 500 and Height to 40
Add a Click event handler to the Button control by selecting the Click property. As you fill in the property you will be prompted with an option to select , doing this will automatically create a handler in the Page.xaml.cs code behind.
Connect to the service
Open the Page.xaml.cs file
Locate the button event handler you just created end enter the code below:
private void BtnCall_Click(object sender, RoutedEventArgs e){
// substitute the path to your service here
string wcfUrl =
"http://localhost:[port]/[WebProjectName]/[ServiceName].svc";
BasicHttpBinding bind = new BasicHttpBinding();
EndpointAddress endpoint = new EndpointAddress(wcfUrl);
HelloService.HelloClient wcfClient =
new HelloService.HelloClient(bind, endpoint);
// creates a delegate, called when the asynchronous web method call
// completes
wcfClient.SayHelloCompleted +=
new EventHandler<HelloService.SayHelloCompletedEventArgs>(
wcfClient_SayHelloCompleted);
// call the web method asynchronously
wcfClient.SayHelloAsync(this.TxtName.Text);
}
If you didn’t automatically create the SayHelloCompleted delegate create as follows:
void wcfClient_SayHelloCompleted(object sender, HelloService.SayHelloCompletedEventArgs e)
{
if (e.Error == null)
this.TxtResponse.Text = e.Result;
}
There is one more thing to change before this will work. Open the web.config in the Web Application project and locate the tag with the name of your service. In the tag modify the binding property to use “basicHttpBinding”. This is required for Silverlight connectivity.
Ex. binding="basicHttpBinding"
You have successfully connected to a WCF Service from a Silverlight 2 beta 1 application. One thing to note, if you want to connect to a WCF Service that is hosted in a different domain than your Silverlight application you will need to follow the instructions located here to setup a client access policy.
This post is intended for those interested in setting up a simple WCF Service in Visual Studio 2008. There are a few ways to do this, however, to show the setup and connectivity we'll use an ASP.NET Web Application.
Create the Project
Open Visual Studio 2008
Click File > New Project
Select the "Web" Project Type on the left
Select the ASP.NET Web Application template
Name it what you want and click OK
Create the Service
Right click the project and click Add New Item...
Select the WCF Service template
Name it what you want
Select Visual C# for language and click OK
Add your service method(s)
Open the C# Interface file for your service (ex. IHelloWorld.cs)
Add your method declaration with an OperationContract attribute
[OperationContract]
string SayHello(string n);
Open the C# Class file for your service (ex. HelloWorld.cs)
Add your method definition
public string SayHello(string n)
{
return "Hello " + n;
}
Build the project
You now have a WCF Service that you can connect to and a method to execute. Let's walk through connecting to the service.
Create the service proxy
Right click the project and select Add Service Reference...
Enter the service address (ex. http://localhost:[port]/[WebSiteName]/[ServiceName].svc) and click Go
Enter the Namespace for your service reference and click OK
Create a simple interface
Open the Default.aspx designer
Drag a TextBox control onto the designer (TextBox1)
Drag a LinkButton control onto the designer
Drag a Literal control onto the designer (LitResponse)
Double-click the LinkButton to create a click event handler
Connect to the service
Enter the following code for the button click handler
HelloService.HelloClient h = new HelloService.HelloClient();
this.LitResponse.Text = h.SayHello(this.TextBox1.Text);
h.Close();
...where HelloService is the service namespace I provided when setting up the proxy and Hello is my service name. The client class is created automatically when adding the service reference.
hey whats up? read more
on Connect to a WCF Service from a Silverlight 2 application