Web Services Using JavaScript and .NET


Test the Web service here: http://www.xemantex.com/consumeWebSvcDateTime.aspx

I wrote this article because there are very few resources that elaborate on client-side consuming a Web service. This type of consuming is very handy when one has to make Web service calls to a remote Web server directly from the client. One can think of many instances where one would want to make Web service calls from the client. Look at this Web site, http://www.xemantex.com, for example, that uses the double-click on the client to call a Web service. Double-click on any word to get the word’s meanings. On the other hand, calling Web services from the client has an added advantage of refreshing only that part of the Web page that accesses the Web service. A Web service call does not re-render the entire content on the page. Thus, one can attain a partial refresh of the Web page, providing a good improvement in the browsing efficiency for the end user.

A Web service is a function, method, or sub routine that is accessible not only to one computer (the owner or host) but also to any computer on the Internet. In a sense, a Web service is a collection of universally available functions.

Why Web Services?

The increasing usage of Web services is attributed to the fact that they enable easy sharing of an application across the Internet. You can find a single Web site serving for diverse functions such as stock quotes, weather information, and airline tickets. It is quite possible that the Web site directly provides for all these services, but in most cases each of the services would be powered by a third party that specializes in it. For example, http://www.weather.com is a good source for weather information.

Similarly, http://www.google.com is excellent resource for searching the Web. Instead of creating tools that cater to weather and/or Web search tools, a Web site owner or a solution provider may obtain services from Weather.com or Google.com for those respective needs. This approach not only saves time but also enables the site to provide reliable, accurate, and consistent information to the end user. Services like these that are made available to other computers anywhere in the world via the Internet are called Web services.

.NET has supplemented the easy creation and usage of Web services. Web services in .NET utilize open standards such as HTTP, SOAP, XML, and WSDL that allow for fast creation, extension, and deployment. Creating a Web service using .NET is a snap. You will look into creating and consuming a Web service in the following paragraphs.

Code Sample

For the sake of simplicity, you can create a Web service that returns a Web server’s current date and time. Because this is a Web service, this will return the date and time of the geographical location where its server is located. If the Web server is in Chicago, USA, you will get the local time at Chicago. Similarly, if the Web server is in New Delhi, India, you will see its local time.

Test this Web service here: http://www.xemantex.com/consumeWebSvcDateTime.aspx.

There are two steps to creating a Web service using .NET (note that creating and consuming are two different things for a Web service).

  1. Create an .asmx file (call it dateTimeWebSvc.asmx).
  2. Write the required functions to accomplish business processes.

You will look into Step 2 before going to Step 1. Following is the code for a function to return the current date and time as a string:

<WebMethod()> Public Function currDateTime() As String
   Dim dateTimeStr As String
   Dim tZn         As TimeZone              'get the time zone
   Dim tZnNameStr  As String                'time zone name
                                            '(CST, EST, etc)
   tZn = System.TimeZone.CurrentTimeZone    'get name of time zone
   If tZn.IsDaylightSavingTime(Now) Then
      tZnNameStr = tZn.DaylightName
      tZnNameStr = tZn.StandardName
   End If
   dateTimeStr = "Current date and time at this web server's _
                  geographical location " & _
   "is- " & Now.ToString & " " & tZnNameStr
   Return dateTimeStr
End Function

More by Author

Must Read