Introduction
In this article we will see,
- How to create a WCF 4.0 Service
- How to host service in IIS 7.5
- How to consume that in Silverlight 4.0 client
- How to avoid cross domain problems.
For our walkthrough, I am going to create a simple Addition Calculator. User will interact with the Silverlight UI and on the user input a
WCF Service hosted in IIS will get called.
Expected Output

When the user will clicks on
the + button a WCF service hosted on IIS gets called. The value from two textboxes will be passed as an input parameter to the service and the
result will be displayed in the result text box.

How to create the WCF Service
Open Visual Studio, select new project and then from the WCF tab select WCF Service application to create a new WCF service.

Delete the default code created by
WCF from IService1 and Service1.svc.cs. Delete the data contract and modify the service contract as below. Just make one operation contract to
return a string.
Listing 1: IService1.cs
Now implement the service as below.
Listing 2: Service1.svc.cs
Since we are creating a WCF Service for Silverlight client, we need to modify the endpoint of the service with basicHttpBinding.
As we know Silverlight supports either basicHttpBinding or webHttpBinding. webHttpBinding is essentially for REST based services. We are
going to use basicHttpBinding to enable SOAP based WCF service for Silverlight client.
1. Declare the end point with basicHttpBinding.

2. Declare the metadata exchange end point.

3. Define the service behavior

So the service will look like

Listing 3: Web.Config
Hosting in IIS and solving the cross domain problem
Let's see how to host the WCF Service in IIS 4.0
Open the command prompt in administrator mode. To do this, click on the Start button
then type RUN in search and then in the Run window type inetmgr to open IIS.

Now you will have IIS opened like below.

Right click on Sites and then click Add Web
Site.

Now a window will
open.

Give a name of
your choice as the site name. I am naming it HostedWcfService.

Now click on select and choose ASP.Net v4.0 from the drop down.

Now in the physical path section, we need to give a physical path
for the service. So to get the physical path of the service, right click on the WCF Service project in Visual Studio and click open project in
windows explorer. Open the project in windows explorer and from the address bar copy the path and paste it below. Or, just click on the Browse
button and navigate to the project folder of the WCF Service.

Now at the Binding section select HTTP. Give any port number. I am selecting port 4567.

Do not give any name as the host
name. Leave it blank.

Check the checkbox "Start Web Site Immediately".

Now click OK on the window. Open the Visual Studio command prompt in administrator mode. Right click and select
"Run as administrator".


Type the command
aspnet_regiis.exe /iru.

You will get the message "Finished Installing ASP.Net 4.0". Now go to inetmgr, and you should be able to see the
site you just created in previous steps.

Publishing the WCF Service
Go back to WCF Service.Right click and select Publish.

Upon clicking on Publish a window will open.

Provide a Service URL. You are free to provide any Service URL here. Just make
sure the host name is getting resolved. Instead of localhost you can give an IP address or your machine name.

Supply the site name HostedWcfService. We created this site
in previous steps.

Leave the credentials group inactive.

Now click on Publish.

You will get the message "Publish Succeeded".
Browsing the service hosted in IIS
Open Inetmgr and right click on site HostedWcfService. Then from Manage Web Site select the Browse option.

When you click on Bowse Site, it
will get opened in the browser.

You will get the above error. Just in address bar append Service1.svc with the address to open the service in browser.
http://localhost:4567/Service1.svc
And in browser you will get the service up
and running.

So now you
successfully created and hosted the WCF 4.0 service in IIS 7.5
Solving the cross domain problem
Right click on WCF Service project and add new item. Select XML file from Data tab.

Give the XML file the name the clientaccesspolicy.xml.

Add the below markup in
ClientAcccessPolicy.xml

Now you have a ClientAccesspolicy.xml file in the WCF project.
Listing 4: ClientAccesspolicy.xml
Now open the WCF Service project in windows explorer. To do this right click on the project and select open folder in Window
explorer.

Copy
clientaccesspolicy.xml file and paste it in C:\inetpub\wwwroot. Make sure the clientaccesspolicy.xml file exists in the wwwroot folder.
Consuming the service in Silverlight 4.0
Create a Silverlight project by clicking on Project->new ->Silverlight -> Silverlight Application.

And create a web site to host the
Silverlight application.

Now design the page in XAML.

Listing 5: MainPage.xaml
Explanation of the XAML clode:
- Divided the main grid in two rows.
- In first row put a textbox to display the result.
- In second row put a stack panel with horizontal orientation.
- Put a canvas inside stack panel
- Put two text box and button in canvas.
Now right click on the Silverlight project and add a Service Reference.

Give the URL of the WCF service hosted in IIS in Address text box. And then
click GO.

Calling the Service
Add the namespace:
On the click event of the button, add the following code:
Explanation of the previous code:
- Silverlight calls WCF service asynchronously
- So the first completed event is getting called. If the operation contract name is
GetMessage then the event name will be
GetMessageCompleted.
- After event aysnc function is being called.
Listing 6: MainPage.Xaml.cs

Conclusion
In this article we saw how we can consume a WCF Service in a Silverlight application with cross domain. I hope this article was useful. Thanks
for reading and happy coding.
About Dhananjay Kumar
 |
Dhananjay Kumar is a developer who blogs at http://debugmode.net/. He is Microsoft MVP ,Telerik MVP and Mindcracker MVP. You can follow him on twitter @debug_mode
This author has published 8 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
Using WCF with SQL Azure and Telerik OpenAccess
read more
View and print Reporting Services Reports from Silverlight.
read more
Problems running Windows Communication Foundation (.svc) on an upgraded Windows 7 and IIS 7
read more
Silverlight Live Streaming service update
read more
Silverlight MVP
read more
Impressions from Basta Germany Developer Conference
read more
Exporting SWF & FLV format reports in SSRS 2005 and 2008
read more
Project Turing: Beginning RIA Svcs
read more
Create a SQL Azure CRUD Application with Telerik OpenAccess and the WCF Wizard
read more
HanselMinutes Interview on RIA Services
read more
|
|
Please login to rate or to leave a comment.