Advertisment

Web Services in ASP.NET

author-image
CIOL Bureau
Updated On
New Update

By: Vinod Unny

Advertisment

Web services are a new feature of ASP.NET

that allow you to perform remote data operations and recovery by using a

standardized data exchange format - XML. So, what is the use of this? Well,

there are many technologies currently available on the Internet, such as

COM/COM+/DCOM, CORBA and RMI/IIOP, and getting them to talk to each other can be

difficult. 

Web services allow you to interact with other information providers without

worrying about what they are running either at the backend or even their

frontend. Take for example a company stock ticker that you may wish to have, say

on your website or Intranet. The data could possibly be coming from a major news

site like MSN or NASDAQ. The way you would currently achieve this is either by

buying access into their database or by 'scraping' their home page HTML for

the relevant data and converting into your format. Suppose they go ahead and

change their Web design-all the scraping code you would have written would be

rendered useless. 

Running the

ASP.NET Web Service show you the method created

Advertisment

Even if you buy access into their system, they may be running a technology

that is incompatible or too hard to work with your own. So what do you do?



This is where Web Services help out. By allowing data interchange in the

standard XML

format, anybody can pick up the data and use it. Let us see how to create and

use Web services quickly in ASP.NET. We'll be using the free Asp.NET Web

Matrix tool available from www.asp.net for creating quick templates. 

Let's create a simple Web Service first. Open Web Matri    x

and select Simple Web Service as the template. Name the file ASTRO.ASMX, class

as astro and namespace as PCQ. You will be presented with a template of a Web

service that looks like this.

Enter a data

in the automatically provided text box to get your sun sing 

Advertisment

<%@ WebService language=”VB” class=”astro”

%>

Imports System



Imports System.Web.Services


Imports System.Xml.Serialization

Public Class astro

Advertisment

Public Function Add(a As Integer, b

As Integer) As Integer 



Return a + b


End Function

End Class

Add the line 'Imports Microsoft.VisualBasic' just below the last

'Imports' line. Then paste the following Web service Method into the class

below the 'end function' line.

Advertisment

Public Function getStar(ByVal dDate

As Date) As String



Dim iMonth, iDay As Integer

iMonth = Month(dDate)



iDay = Day(dDate)

If (iMonth = 3 And iDay >= 20) Or (iMonth = 4 And

iDay <= 20) Then getStar = “Aries”

Advertisment

End If



If (iMonth = 4 And iDay >= 21) Or (iMonth = 5 And iDay <= 21) Then getStar
= “Taurus”

The WSDL

script for the web service

End If



If (iMonth = 5 And iDay >= 22) Or (iMonth = 6 And iDay <= 21) Then getStar
= “Gemini”

Advertisment

End If



If (iMonth = 6 And iDay >= 22) Or (iMonth = 7 And iDay <= 21) Then getStar
= “Cancer”

End If



If (iMonth = 7 And iDay >= 22) Or (iMonth = 8 And iDay <= 22) Then getStar
= “Leo”

End If



If (iMonth = 8 And iDay >= 23) Or (iMonth = 9 And iDay <= 23) Then getStar
= “Virgo”

End If



If (iMonth = 9 And iDay >= 24) Or (iMonth = 10 And iDay <= 23) Then
getStar = “Libra”

End If



If (iMonth = 10 And iDay >= 24) Or (iMonth = 11 And iDay <= 22) Then
getStar = “Scorpio”

End If



Type ID Text 
TextBox txtDate    
Button btnStar Get

Star Sign 
Label lblStar    

If (iMonth = 11 And iDay >= 23) Or (iMonth = 12 And

iDay <= 21) Then getStar = “Saggitarius”

End If



If (iMonth = 12 And iDay >= 22) Or (iMonth = 1 And iDay <= 20) Then
getStar = “Capricon”

End If



If (iMonth = 1 And iDay >= 21) Or (iMonth = 2 And

iDay <= 18) Then getStar = “Aquarius”

End If



If (iMonth = 2 And iDay >= 19) Or (iMonth = 3 And iDay <= 19) Then getStar
= “Pisces”

End If



End Function

A sample

application using the web service 

This simple Web service will return the star sign for a given date. Save the

file in Web Matrix as astro.asmx. Now run the Web Service by browsing to http://localhost/pcq/astro.asmx.

You can test the Web service out by clicking the method you see and entering a

date in the resulting textbox. You will see the output in XML for the same.

Another interesting link on the initial page is 'Service Description'.

Clicking on this will show you an extremely complicated looking XML output. This

is the Web Services Description Language or WSDL for the object you just

created. This is used as reference documentation (much like a TypeLib) for

registering a Web Service for use with other applications.



Now to use this create a normal ASP.NET file in Web Matrix. Go to the
Tools>Web Service Proxy Generator and enter the following information:

WSDL URL: http://localhost/pcq/newfile.asmx?WSDL

NameSpace: PCQ



Output Directory: C:\Inetpub\wwwroot\PCQ


Source File: AstroProxy.vb


Assembly: AstroProxy.dll

Click Generate to create the local proxy object for the Web Service. Also

create the following controls on the page. (see table)

Double-click the button element. This will take you to the btnStar_Click

event handler. Enter code to make the subroutine look like the one in Listing 6.

Private Sub btnStar_Click(ByVal sender As Object, ByVal e As System.EventArgs)

Handles btnStar.Click



Dim wsAstro As New localhost.Astro()


lblStar.Text = wsAstro.getStar(txtDate.Text)


End Sub

The code is simple enough to understand. All that happens here is that a new

variable, wsAstro, is declared to be of the type localhost.Astro. This datatype

is actually the full namespace of the Astro class that you created in the

WebService.



The next line simple sets the text of the lblStar label to the value that the
Web Service's method-getStar, returns. The method takes it's parameter from

the date that is entered in the textbox.

Run the form by going to localhost/ pcq/test.aspx (assuming you saved it as

such) and enter your birth date and press the button. You will immediately see

the message telling you your star sign appear. 

This Webservice can now be used by anyone-whether using Java, CORBA, Linux,

PHP

or any other Web Technology you can name. This is the power of this technology

and you will be providing a valuable Web service, by using this technology for

your data-rich site.

Source: PCQuest

tech-news