Web Services Development
In addition to creating web-applications, ASP.Net technology allows you to create and web-services (described in Section 2.4). It should be noted that at present the technology of working with Windows Communication Foundation (WCF) services is more perfect. However, for understanding web services, given the limited amount of benefits, this section explains only the creation of ASMX wcb-services.
To create a web service, use the ASP.Net Web Service project template, which is available in the ".Net Framework 3.5" platform. (Figure 4.52).
Fig. 4.52. Creating a web-cepeuca template
As a result of selecting this template, a set of files will be created to create a web-service named Service. In order to create a service with a different name, for example MathService, it is better to remove the existing service (Service.asmx and Service.cs files in the subdirectory App_Code), and then add files with different names to the project. To do this, you need to execute the command "Website => Add New Item", select the file template "Web Service" in the dialog box. and give it a new name, for example MathService. As a result, two MathService files (in the root directory) and MathService.cs (in the AppCode subdirectory) will be created.
ASMX web services are files with the extension * .asmx, which contain the WebService directive (and not the Rade, as in the web form). For example:
& lt;% @ WebService Language = "C #" CodeBehind = -/App_Code/MathService.cs Class = MathService ''% & gt;
The program code for the service is contained in the associated file specified in the CodeBehind property. In the example above, the service code is stored in the MathService.cs file (in the App Code subfolder).
A Web service is a normal class in C # (in this case, MathService), derived from the WebService base class (from the System.Web.Services namespace). When describing this class, the special attributes WebService and WebServiceBinding are used, indicating the common execution environment (CLR), how to work with objects of this class. The methods of the service class that will be available through the SOAP protocol must have the WebMethod attribute.
In the example shown in Fig. 4.53, a service containing two methods: Add (summation of two numbers) and Subtract (subtraction of two numbers).
Fig. 4.53. The code for weh-cepeuca
If the created web service is launched for execution (press F5), the browser will display a web page with the description of this service (Figure 4.54).
Fig. 4.54. Web-page with the description of the developed web-service
Clicking on the Service Description the browser will display the WSDL description of this web service, which will be used to create proxy classes in programs that use this service (Figure 4.55).
Fig. 4.55. Web page with a fragment of WSDL-description of web-service
Clicking on the name of one of the methods included in this service will display a form that allows you to enter the original data for the selected method and test its operation (Figure 4.56).
Fig. 4.56. Web page for testing the Add web service method
Fig. 4.57. Context command to add a web service link to the project
After selecting this command, a dialog box will appear in which you need to specify the URL of the web service (in this case it's localhost: 61269/MyWebSite/MathService.asmx) and specify the name of the space in which the proxy class will be created Figure 4.58.)
Fig. 4.58. The dialog box for adding a web service link to the project
An HTTP request will be sent to the specified address with the? WSDL parameter. In response to this request, the Visual Studio development environment will receive a WSDL description of the web service and on its basis create a proxy class MathServiceSoapCIient in the project (Figure 4.59).
Fig. 4.59. The MathServiceSoapCIient class, created from the WSDL web service description
Now to use this web service in the application, you need to create a MathServiceSoapCIient object and call one of its methods (for example, Subtract), Fig. 4.60.
Fig. 4.60. Using web-cervuca with a proxy class (class MathServiceSoapCIient)
A proxy object will create a SOAP request that includes an XML document to call the Subtracl () method, and send it to the web service. The resulting SOAP response will be converted to the result value of the called method.