3 - Data communication - ASP.NET Web Services (ASMX)
It is one of the easy ways. All you need to do is to implement a simple web method which will return requested data.
Please follow these steps to create and use the web service to get the requested data:
1) Right click on your web project folder and choose
Add New Item option. Select
Web Service as type of the new item and name it as
CMSWebService.asmx:
2) Following files will be automatically created by Visual Studio:
/CMSWebService.asmx
/App_Code/CMSWebService.cs
3) Define method to find users according to the search expression, modify CMSWebService.cs as follows. Notice that it calls internally our helper method from ServiceHelper class which was defined in the previous step:
4) Ensure your web service is enabled. Just type the full URL path to the CMSWebService.asmx file within your web application and then click on GetUsers method, following window will be displayed:
5) Ensure your web service is working. Just type some search expression to find requested users. You will be displayed with the results in XML format. Notice the names of the XML elements and attributes. It helps you to understand the definition of the helper methods in ServiceHelper class:
6) Let the Visual Studio to create proxy class for your web service - Right click on the SilverlightDemo project and choose Add Service Reference option:
7) Add Service Reference dialog is opened. Locate your new CMSWebService and specify namespace for its proxy class:
8) Web service was referenced and initialized:
- Reference to your web service was added to the SilverlightDemo project.
- Proxy class CSMWebService.CMSWebServiceSoapClient was created for easier access to the web service.
- Web service default configuration file (ServiceReferences.ClientConfig) was created, however you can configure service settings in code behind as well.
9) Locate Page.xaml.cs file in SilverlightDemo project and add you web service as the first data communication method. Implementation is quite easy because of the generated proxy class.
This article is part of the
Data communication in Silverlight 2.0 tutorial.