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.
Comments