The process is simple. You should know two things,what has to be provided as service.
Where it would be consumed.
Here Windows Communication Foundation lets you create services.
Silverlight application is the client which will consume it.
Steps for creating WCF service:
1) Select a Silverlight application hosted in Web application project.
2) Add to it a WCF service.
Here MyHello is a simple method to return a message on input of a string.
|Code in IService1.cs
|Code in Service1.svc.cs
5) Once the service is created build the project and the view the .svc file in browser.
Steps for consuming the service.
6) In our Silverlight application,in our xaml page we will add a textbox to enter a name , a button to call WCF service and a textblock to display the output.
<Grid x:Name="LayoutRoot" Background="White">
7) Your silverlight application has to first discover the service created. So add a service reference to your silverlight application project.
The project structure.
8) Finally the last step. Create a proxy which is an object of ServiceClient to access the method. The methods are loaded asynchronously hence you may not see the name of the method,but two additional methods,one to load method asynchronously and the other to do something once its completed.
|Code in MainPage.xaml.cs
private void btn_CallService_Click(object sender, RoutedEventArgs e)
//Hooking up the event to display the entered text.
//Passing the text input
void proxy_MyHelloCompleted(object sender, ServiceReference1.MyHelloCompletedEventArgs e)
9) Its done. Now when you run your application,on click event of button the WCF service will call the function asynchronously to take in the entered text and display the output in textblock.
Note: In Silverlight 4 application when using a WCF service I did not have to change the bindings from wsHttpBinding to basicHttpBinding.
Else we had to do it in earlier versions of Silverlight applications.