4 - Data communication - Windows Communication Foundation (WCF)
WCF "web services" are part of a much broader spectrum of remote communication enabled through WCF. You will get a much higher degree of flexibility doing things in WCF. ASMX is older than WCF, and anything ASMX can do so can WCF (and more).
Please follow these steps to create and use the WCF "web service" (it will be quite similar to the ASMX):
1) Right click on your web project folder and choose
Add New Item option. Select
WCF Service as type of the new item and name it as
CMSService.svc:
2) Visual Studio adds the necessary files and also the service configuration to the web.config file.
New files:
/CMSService.svc
/App_Code/CMSService.cs
/App_Code/ICMSService.cs
New section in web.config:
![](/getattachment/d747b8ad-35ee-4633-993f-0d1b49cdca76/01a_NewWebConfigSection.png)
3) Silverlight 2.0 supports only basicHttpBinding thus our WCF service can only use basicHttpBinding. As you can see WCF uses wsHttpBinding by default so you will need to change it from wsHttpBinding to basicHttpBinding:
![Change WCF binding](/getattachment/404f145a-43d8-4852-90c4-1a2a8f0fdc22/01b_webconfig_chanbebinding.png)
4) Modify the interface of your service in ICMSService.cs file to offer the same functionality as ASMX does. It means define method which enables you to search for the users according to the specified expression:
![Web service interface](/getattachment/44bf6f91-5191-4596-a782-9165de5e8973/02_WebService_Interface.png)
5) Now implement the modified interface in your web service to reach the same results as in ASMX:
![Web service implementation](/getattachment/f1bcb446-b366-41a5-b7e0-b0240660e9df/02_WebService_Method.png)
6) Your service is created and you will need to reference it in order to be able to call it in your code. Right click on the SilverlightDemo project and choose Add Service Reference option:
![Add service reference](/getattachment/3dd336a8-eec3-46da-8dba-448491140446/03_AddServiceReference.png)
7) Locate your new CMSService and specify namespace for its proxy class:
![Locate your new WCF service](/getattachment/cf9469fe-da5a-494f-8b10-ad88ebf3824d/04_DiscoverService.png)
8) Now you are ready to use created WCF service in your SilverlightDemo application. To do so, please update your Page.xaml.cs file with the following block of code. Again, Visusal Studio has created proxy class for your web service which make the work much easier:
This article is part of the
Data communication in Silverlight 2.0 tutorial.