Creating a Web Services Using ASP.Net
Author: Faraz
In this article you will learn the simple steps to create a web services using ASP.Net 1.x.
Web services are based on the exchange of SOAP (Simple Object Access Protocol) documents. SOAP documents follow XML formatting, meaning that they are plain text and can be readily exchange over HTTP.
Whenever a call is made to a Web Service, the request is wrapped up as a SOAP message and sent over the Internet to the web server. The web server has the mechanism that takes the request and passes it to the software that powers the web services. The software then prepares the response, wraps it up as another SOAP message, and returns it to the client that made the call, again via the web server.
Web Services are not a proprietary Microsoft innovation. All the major platform vendors are realizing Web Services implementations based on a combination of HTTP and SOAP. Because the underlying standard followed on every platform is the same, a Web Service running on a UNIX computer can be accessed by a machine using Windows 2000, and vice versa. This is great for us because it means that, even though we’re developing our system on the Windows platform, our customers will be able to access them regardless of which particular computer and operating system they may be using.
Steps involve in creating a Web Services:
- Open the Visual Studio.Net and selects the new project option from the startup page.
- From the Template list, select ASP.Net Web Services. Enter the name and click OK.
- In the Solution Explorer you will see the Service1.asmx file. Note that this is the default Web Services file that gets created for us, and that will contain our Web Service’s main code.
- Selects the “click here to switch to code view” option from the Service1.asmx file. This will open the Service1.asmx.vb code file.
- You will see the following lines on the page:
Note: You will see the HelloWorld() method in comments. Remove the comments. Notice that the class is inherited from System.Web.Services.WebService. This is the class that automatically creates the user interface we saw in Internet Explorer that allowed us to invoke the methods.
Note: You can add as many methods as much you want by using <WebMethod()> Public Function FuncName() As String
- Run the application, you will see two methods, “Hello World” and “CreateWS”.
- Click on any one, this will take us to a page for testing the method. Beneath the invoke button, you will notice some notes about SOAP, HTTP GET and HTTP POST.
- Now, click on the Invoke button and a new Internet Explorer window will appear containing the XML document.
Concepts:
When the page request is sent to IIS, the .asmx extension tells IIS to pass it to ASP.Net for processing. ASP.Net looks at the page to determine the .vb file that contains the code that powers the Web Service.
At this point, Service1 must create a page to present details of the Web Service it represents. It knows that haven’t asked to actually run the Web Service at this point, so it looks through the class for all methods that have the WebMethod attribute set, and renders them on the page as the list of available methods. When we click on the method, the .asmx page is again requested and IIS passes the request to ASP.Net for processing. This time, the URL used will be:
http://localhost/CreatingWS/Service1.asmx?op=CreateWS
The op parameter at the end of the URL tells Service1 to provide further details of the method. This is duly does, along with a button labeled Invoke. If our method had parameters, a form would also be produced for us to enter the values to use as parameters when we test the method.
The Invoke button brings up a new browser window with this URL:
http://localhost/CreatingWS/Service1.asmx/CreateWS
This URL tells the Web Service to actually execute the CreateWS method and package the return value as a SOAP document to be returned to the caller. The returned document contains only a single entry, which is the string that the CreateWS implementation of the Service1 procedure.
Summary:
- Create the new project select the ASP.Net Web Services from the template list.
- Click on the View Code and add the WebMethod attribute. Notice that on the Service1.asmx.vb page you can add more then one methods and each method has different invocations.
- Run the application and see the results.