In my previous blog, I created a WCF service and hosted it in IIS. In this blog I will create a Silverlight client application to call that service, and host the Silverlight client in a ASP.Net web application.
1. Create a Silverlight Application
Right click the solution in the Solution Explorer, Add -> New Project. In the window below, select "Silverlight" as Project Type, select "Silverlight Application" Template, input project Name and Location, then click "OK".

In the window below, select "


Now there are 3 projects under the solution as shown below.
2. Add Service References
Right click WcfSilverlightClientApp project and click "Add Service Reference..." menu item, it will open the "Add Service Reference" window shoiwn as below.

select "Service1.svc", then click "OK" button. You will see a service reference named "ServiceReference1" under the "Service References", and "ServiceReferences.ClientConfig" file in the Solution Explorer.
3. Write Silverlight Client Code
MainPage.xaml
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">
MainPage.xaml.cs
using System;
using System.ServiceModel;
using System.Windows;
using System.Windows.Controls;
using WcfSilverlightClientApp.ServiceReference1;namespace WcfSilverlightClientApp
{ public partial class MainPage : UserControl
{ Service1Client proxy;public MainPage()
{ InitializeComponent();
EndpointAddress address = new EndpointAddress("http://localhost:2561/Service1.svc");
BasicHttpBinding binding = new BasicHttpBinding();proxy = new Service1Client(binding, address);
proxy.DummyOperationCompleted += new EventHandler(proxy_DummyOperationCompleted); }
void proxy_DummyOperationCompleted(object sender, DummyOperationCompletedEventArgs e)
{ if (e.Error == null) { result.Text = e.Result;
} else { result.Text = e.Error.StackTrace; } }private void Button_Click(object sender, RoutedEventArgs e) { proxy.DummyOperationAsync(name.Text); } } }
4. Change WCF service configuration
You know, Silverlight only supports BasicHttpBinding with WCF service endpoints. So, we have to modify the web.config under ServiceHost project.
Web.config
add the following xml under the
and change the
bindingConfiguration="myBasicHttpBinding">
After WCF service side config finished, it is time to update service reference for the Silverlight Application. Right click the "ServiceReference1" under "WcfSilverlightClientApp" project in the Solution Explorer, then click menuitem "Update Service Reference". Now, open ServiceReferences.ClientConfig, it looks like below:
ServiceReferences.ClientConfig
maxReceivedMessageSize="2147483647">
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
name="BasicHttpBinding_IService1" />
5. Look at Silverlight host page
When we choose to host the Silverlight application under a web site, The Visual Studio 2008 will create an aspx page or an Html page to host the Silverlight client. Let's take a look at its detail now.
6. Run from VS2008
Now we are ready to run this sample project. In the Solution Explorer, right click "WcfSilverlightClientApp.Web", click menuitem "Set as Startup Project", then right click "WcfSilverlightClientAppTestPage.html", click menuitem "Set As Start Page". Click "Start Debugging" on the Toolbar on the top of the window. The Page looks like below:

Input any string as your name, then click "Call Service" button, it throw an unhandled error as below:

To address this issue, You need to add "clientaccesspolicy.xml" and "crossdomain.xml" under the "ServiceHost" project.
clientaccesspolicy.xmlcrossdomain.xml
Let's run the project again and see what will happen. This time it runs successfully. The result is shown as below.

You can doanload the complete sample code here:
[@more@]来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/13651903/viewspace-1034049/ ,如需转载,请注明出处,否则将追究法律责任。