This post contains attachments v20020214233758.zip 
Summary
This article gives an idea on how to create a Web Service, how to consume the Web Service and its interfaces.
In this article, I will discuss
- How to build an XML Web Service using the ASP.NET FrameWork?
- How to create a proxy of the XML Web Service?
- How to create an instance of the proxy and accessed its exposed interfaces?
Creation of Web Service using ASP.NET
In ASP.NET, the XML Web Service files have .asmx extension. Here I will try to add, subtract, multiply and divide two integers. This example is to show how really, we can implement the concept of web services and to analyze its advantages.
Refer to the code of SimpleCalculator.asmx file.
In this example, we created a class SimpleCalculator using C# language as XML Web Service which exposes Add, Subtract, Multiply and Division methods. The interfaces returns the result of type integer. You can extend this example to different types like float,etc. The first line in the example is a Web Service directive, which is used to identify the language and class of the Web Service. Any method that is to be exposed as a method of an XML Web Service must have WebMethodAttribute applied to it and it must be declared using the public keyword. The WebService, which is System.Web.Services.WebServiceAttribute class is an optional attribute that can be applied to a public class.
This XML Web Service can be browsed through Internet Explorer browser. To see how the asmx file looks like please downlaod the attachment where I posted my web service code, which shows how .asmx file exposes its intefaces.
You can look at WSDL(Web Services Description Language) by typing SimpleCalculator.asmx?wsdl To see disco file, type SimpleCalculator.asmx?disco DISCO is Microsoft publishing technology into .NET. UDDI(Universal Description, Discovery, and Integration) is an industry wide publishing technology.
To test the exposed intefaces, click on one of the methods. Then we will see two text boxes with Invoke button. When we give input to these boxes, the Invoke button returns the result in the form of an XML document.
Creation of Proxy for Web Service
To consume the Web Service, we need to create a proxy for the Web Service. Then create the instance of the proxy and invoke its methods.
How to create a proxy for Web Service?
To create the proxy, you can use Web Services Description Lanugage Tool (WSDL.exe) to generate a Web Service client proxy class. The following is the command line to create the proxy class:
If you develop .asmx file using C# language then
wsdl /l:cs /o:SimpleCalculator.cs http://pandu/SimpleCalculator.asmx?wsdl /n:CalculatorApplication
If you develop .asmx file using VB language then
wsdl /l:vb /o:SimpleCalculator.vb http://pandu/SimpleCalculator.asmx?wsdl /n:CalculatorApplication
Then compile the resultant file to create dll. The following is the command line to create dll.
For C#: csc /out:SimpleCalculator.dll /t:library /r:system.web.dll, system.dll, system.xml.dll, system.web.service.dll, system.data.dll SimpleCalculator.cs
For VB: vbc /out:SimpleCalculator.dll /t:library /r:system.web.dll, system.dll, system.xml.dll, system.web.service.dll, system.data.dll SimpleCalculator.vb
Create an instance of Proxy Client in .aspx file(Web Form)
Refer to the CalTest.aspx file in code section.
In this .aspx file, I used two text box controls, one dropdown control and a button. These are server side controls. This .aspx file is actually a Web Form. The following is the figure of web form I used to test the Web Service.
I test the above given code with Beta2 .NET SDK version on Windows 2000 Professional OS.
If you have any questions or issues, please do feel free to contact me.
|