Marketplace for Content, Tests and Assessment
 
 
Home > Programming / tutorials > Web Services with ASP .NET- Part I

Web Services with ASP .NET- Part I

December 13th, 2009 Arunava Leave a comment Go to comments

Introduction

Web services are a new way of performing remote method calls over HTTP with the use of Simple Object Access Protocol (SOAP) . It becomes very easy for us to use all of this with the help of .Net framework. This tutorial covers building XML Web services and how we can consume XML Web service interfaces and integrate them into ASP.NET applications. But first we examine what is Web service some of the underlying technologies such as SOAP, WSDL and more.

Objective

The main objectives of this tutorial are
a.Give a simple idea about XML web service and the backend technologies.
b.How to build XML web service with the help of Visual Studio 2008
c.How to consume the web service within a web application built in .Net
We will cover the first two portions in Part-I of this tutorial and Part-II will cover the remaining portion.

What is Web Service?

In short we can define Web service as small units of code which is independent of operating system and programming languages and use XML based communicating protocol.

XML is considered ideal for data representation purpose because it enables developers to structure XML documents. But sending self structured XML documents between dissimilar systems cannot solve the purpose. The industry decided on using SOAP (Simple Object Access Protocol) to make the standard XML structure work. SOAP enables user to expose and consume complex data structures such as Datasets or Datatables. So actually ASP.NET Web services generally use SOAP over HTTP using the HTTP post protocol.

WSDL (Web Service Description Language) is another fully XML compliant syntax and specifies Web services by the available methods, the types used by these methods, the request and response message format sent by the methods via protocols like SOAP,HTTP-GET etc. We can say WSDL completely describes Web services, the available methods and the various ways of calling these methods.

Building a Simple XML Web Service

In this tutorial we use Visual Studio 2008 to build an XML Web service. Like our previous tutorial we will provide step by step procedures to create the application:

1.Create a new Web site by selecting File->New->Web Site from the IDE. When the New Web Site dialog opens , select the ASP.NET Web Service and name it ‘TestService’ as shown in Figure-1:

.Net chart tutorial

Figure-1: Select ASP.NET Web Service and name it ‘TestService’

2. In the solution explorer there is a single XML Web service named Service.asmx and its code-behind file is located in the App_Code folder (Service.cs).

.Net chart tutorial

Figure-2: Files in Solution Explorer

3.Open the Service.asmx file in Visual Studio and you can see the file contains only WebService page directive as shown in Figure-3:

.Net chart tutorial

Figure-3: Webservice directive in Service.asmx page

4. Now look at the WebService.cs file—the code behind file for the XML Web. By default a structure of code is already in place in this file. Now we will add a method which we will from our .Net web application in this Web service. Adding a method accessible through the Web service simply requires defining the method as public and giving it ‘WebMethod’ attribute.
Here we will include a function called ‘add’ which will take two integers as arguments and simply returns the added output as result. We will call this function from our .net application Figure-4 shows you the code for that:

.Net chart tutorial

Figure-4: ‘add’ function is added

5. Like the ‘WebService’ attribute ‘WebMethod’ can also contain some properties. Some of them are described in short below:
BufferResponse: When it is set to ‘TRUE’ ,the response from the XML web service is held in memory.
CacheDuration: Specifies the life time (in seconds) of the response in system’s cache
Description: Add a description for the WebMethod
EnableSession: ‘TRUE’ enables session state for a method. Default setting is ‘FALSE’

6. Running ‘Service.asmx’ in the browser pulls up the ASP.NET Web service test page. This visual interface is really important for the testing purpose. The page generated for the ‘TestService’ web service is shown in Figure-5:

.Net chart tutorial

Figure-5: Exposed web methods for ‘TestService’

The interface shows the name of the Web service in the blue bar with white color at the top of the page. By default the name of the class is used and in this case we have not renamed it. A bulleted list of links to all the Web service’s WebMethods is displayed.

7. A link to the Web Services Description Language (WSDL) document is also available with the link titled ‘Service Description’ in the above figure (Figure-5). Everything we need for the request and the response is described in the WSDL document. Figure-6 shows you the WSDL (just click on the ‘Service Description’ to get the WSDL) for the ‘TestService’:

.Net chart tutorial

Figure-6: ‘WSDL’ for ‘TestService’

8. User can test the WebMethod directly from the service.asmx page. Once you click on the ‘add’ method you will find the following screen (Figure-7):

.Net chart tutorial

Figure-7: Testing WebMethod directly from the page

As our webmethod ‘add’ requires two input parameters to get a response , we see the two textboxes included in the page. If the webmethod does not require any parameter then there will be only the ‘Invoke’ method.

9. Now put two integers in the two textboxes respectively (as parameter) and click on the ‘Invoke’ button to send SOAP request to the web server. Here we give a=8 b=7 and invoke the method and it will cause a new browser instance with the result to appear (shown in Figure-8)

.Net chart tutorial

Figure-8: Expected output

Now we are sure that our webmethod is working properly and we can consume it in an ASP.NET application. This is discussed in Web Services with ASP.NET–Part-II of this tutorial

Categories: Programming / tutorials Tags: ,
Get Adobe Flash playerPlugin by wpburn.com wordpress themes