5,424,116 members and growing! (17,472 online)
Email Password   helpLost your password?
Web Development » Web Services » General     Advanced

WebService Behavior

By Stevan Rodrigues

The WebService behavior enables methods that are implemented on Web Services to be called from client-side script in a web page. The main advantage of the WebService behavior is that it is now possible to update contents of the web page without reloading the whole page.
VB, Windows, .NET, Visual Studio, Dev

Posted: 19 Feb 2004
Updated: 4 Mar 2004
Views: 51,369
Bookmarked: 19 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
10 votes for this Article.
Popularity: 3.80 Rating: 3.80 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
2 votes, 20.0%
3
3 votes, 30.0%
4
5 votes, 50.0%
5

Introduction

The WebService behavior enables methods that are implemented on Web Services to be called from client-side script in a web page. The main advantage of the WebService behavior is that it is now possible to update contents of the web page without reloading the whole page. For example, in this article I have used the behavior to display continuously quotes at regular intervals without reloading the page. You can use this feature to display continuously featured products, latest news etc.

XML Web services

XML Web services are program components that allow you to build scalable, loosely coupled, platform-independent applications. XML Web services enable disparate applications to exchange messages using standard protocols such as HTTP, XML, XSD, SOAP, and Web Services Description Language (WSDL).

To study the WebService behavior I have written a simple FactorialService WebService and QuoteService WebService. FactorialService WebService contains the following WebMethod

  <WebMethod()> Public Function Factorial(
      ByVal Number As Integer) As String
    Try
     If Number = 0 Then
      Return 1
     Else
      Return Number * Factorial(Number - 1)
     End If
    Catch ex As Exception
     Return ex.Message
    End Try
  End Function

QuoteService WebService contains the following WebMethod.

  <WebMethod()> Public Function GetQuote() As String
    Select Case getRand.Next(4)
     Case 0
      Return "Where there is a will there is a way"
     Case 1
      Return "Necessity is the mother of invention"
     Case 2
      Return "Be the head and not the tail"
     Case 3
      Return "Rome was not built in a day"
    End Select
  End Function

Attaching the WebService Behavior

The WebService behavior is implemented with an HTML Component (HTC) file as an attached behavior. So it can be used in Microsoft Internet Explorer 5 and later versions.

To attach a WebService behavior to a page we need to follow the following steps,

  • First download the WebService HTC File file and put a copy in the same directory as the pages that use the behavior.
  • The next step in using the WebService behavior is to attach it to an HTML element using the STYLE attribute and set the id attribute so that this element can be easily referenced in script.
<div id="Service" style="behavior:url(webservice.htc)"></div>

In our code we have used the following div tag:

<div id="Service" style="BEHAVIOR:url(webservice.htc)" 
  onresult="Service_Result()"></div>

The event handler for the Onresult event is the default for the behavior. We will be shortly dealing with it.

Identifying Web Services

The useService method is used to identify the WebService. This method has two parameters i.e.. "ServiceURL" and "FriendlyName". The method maps the Web Service URL to a FriendlyName. The friendly name for the Web Service is later used directly in script to reference the Web Service URL.

The syntax of the useService method is as follows:

id.useService("ServiceURL","FriendlyName");

The id is the id attribute of the HTML tag to which the behavior is attached.

In our code we have used the following Javascript code:
  //---------------------------------------------------

  // Init is used to indentify the webservices

  //---------------------------------------------------

    function Init()
    {
    //useService is used to identify the FactorialService webservice

     Service.useService("WebServices/FactorialService.asmx?WSDL",
        "FactorialService");
    //Function is used to call the QuoteService webservice at 

     //regular intervals

     GetNewQuote();
     setInterval( "GetNewQuote()", 5000 ) 
    }
  //-----------------------------------------------------

  // GetNewQuote is used to indentify the webservices and 

  // call webmethods at the same time

  //-----------------------------------------------------

    function GetNewQuote()
    {
     //useService is used to identify the QuoteService webservice

     Service.useService("WebServices/QuoteService.asmx?WSDL","QuoteService");
     
     // callService method is used to invoke methods and 

     // QuoteService_Result is the callback event handler

     
     intCallID = Service.QuoteService.callService( QuoteService_Result, 
        "GetQuote" );
    }

Init function is called in the body tag as shown:

<body bgColor="#ffffff" text="#000000" link="#0000cc" vLink="#0000cc" 
  aLink="#ff0000" leftmargin="0" topmargin="0" marginwidth="0"
  marginheight="0" onload="Init()">

Calling WebMethods

The callService method is used to call WebMethods of the WebService. The WebService behavior supports both synchronous and asynchronous remote procedure calls to WebServices. The asynchronous mode of remote method invocation is used as default.

The syntax of the callService method is as follows:

iCallID = id.FriendlyName.callService([CallbackHandler,] 
  "MethodName", Param1, Param2, ...);

The first parameter of the callService method is a callback handler function to process the results received from the method call which is optional. If callback handler is absent, then the event handler for the onresult event is used. The onresult event is fired by the WebService behavior as soon as the result of the remote call is completed. MethodName is the name of the WebMethod while Param1, Param2, ... are the parameters to be passed to the WebMethod.

In our code, when you click the Callback button, CallBackButton_Click event is fired which calls the Factorial method of the WebService. Factorial_Result serves as the callback handler.

 
  function CallBackButton_Click()
  {
   intCallID = Service.FactorialService.callService( 
      Factorial_Result, "Factorial", txtFactorial.value );
  }

  function Factorial_Result( result )
  {
    if ( result.error )
     {
      lblFactorial.innerText = result.errorDetail.string;
     } 
     else
     {
     lblFactorial.innerText = "The factorial of " + 
      txtFactorial.value + " is " + result.value;
     }
  
  }

When the Normal button, NormalButton_Click event is fired which calls the Factorial method of the WebService.

  function NormalButton_Click()
  {
     intCallID = Service.FactorialService.callService( 
      "Factorial", txtFactorial.value );
  }

In the absence of the callback handler, onresult event handler of the html tag is fired which in turn calls the Service_Result method.

  function Service_Result()
  {
   if ( event.result.error )
   {
    lblFactorial.innerText = event.result.errorDetail.string;
   } 
   else
   {
    lblFactorial.innerText = "The factorial of " + 
      txtFactorial.value + " is " + event.result.value; 
   }
  }

For the QuoteService, GetNewQuote is used to identify the WebService and call WebMethods simultaneously. QuoteService_Result is the callback event handler for the QuoteService.

  //--------------------------------------------------

  // GetNewQuote is used to indentify the webservices and 

   // call webmethods at the same time

  //---------------------------------------------------

      function GetNewQuote()
      {
       //useService is used to identify the QuoteService webservice

       Service.useService("WebServices/QuoteService.asmx?WSDL",
                "QuoteService");
       
       // callService method is used to invoke methods and 

         // QuoteService_Result is the callback event handler

       
       intCallID = Service.QuoteService.callService( 
              QuoteService_Result, "GetQuote" );
      }
  //------------------------------------------------------

  // QuoteService_Result is the callback event handler 

   // for the QuoteService

  //-------------------------------------------------------

      function QuoteService_Result(result)
      {
       if (result.error) 
       {
       divQuote.innerText = result.errorDetail.string;
       }
       else
       {
       divQuote.innerText = result.value;
       }
      }

The Result Object

Whenever callService is invoked, an object containing results data is generated. When the eventhandler is present, the result object is referenced as the name of the parameter being passed to the user-defined callback function. In the absence of the eventhandler, the result object is referenced as event.result when the event handler is absent or as the name of the parameter being passed to the user-defined callback function.

Here is the list of properties of event.result we have used in our code..

event.result.error Returns a boolean value
event.result.errorDetail.string Returns the detailed information of the error
event.result.value Returns result of the webmethod

When the eventhandler is present, the result object is referenced as the name of the parameter being passed to the user-defined callback function. Here is the list of properties it exposes.

result.error Returns a boolean value
result.errorDetail.string Returns the detailed information of the error
result.value Returns result of the webmethod

When an error or problem occurs for a method call, the error property is set to true and the errorDetail is generated at runtime. The errorDetail object exposes several properties, which can be quite helpful in diagnosing the error.

It is always good programming practice to verify a function call. Hence in our examples, the error property is checked to determine if the remote method invocation was processed successfully.

Limitations

The following are the limitations of the behavior

  1. The behavior accesses Web Services in the same domain as the web page.
  2. There is no direct support for complex types such as DataSets, DataTables, collections or custom objects, etc. It supports all the base .NET types and arrays of the the base .NET types

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Stevan Rodrigues


I am a Microsoft Certified Solutions Developer in .Net Architecture (MCSD.Net Early Achiever – one among the first 2500 worldwide), Microsoft Certified Application Developer in .Net – MCAD.Net (Charter Member - one among the first 5000 developers worldwide) with Microsoft certifications (MCP) in Visual basic .Net, ASP.net, XML web services, Visual Studio.Net, COM+, C# and SQL Server 2000. I have successfully completed my professional training in .Net from ExecuTrain Institute – Authorized Microsoft Technical Center, Dubai, U.A.E.

I am a Science postgraduate majoring in Mathematics serving the IT industry in Dubai, U.A.E for quite sometime.


Occupation: Web Developer
Location: United Arab Emirates United Arab Emirates

Other popular Web Services articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
Subject  Author Date 
Generalcreating a windows based application using webservicesmembersrichandk0:52 5 Feb '07  
Generaltimer to trigger the refresh of webpagememberkd834110:31 10 Jun '05  
GeneralLooking for a web service for real-time forex infomemberilan_levy22:53 22 Feb '05  
Generalcustom parameterssussAnonymous7:24 24 Jul '04  
Generalweb service returning datasetmemberPradeep K V22:21 21 Apr '04  
GeneralRe: web service returning datasetmemberStevan Rodrigues22:50 21 Apr '04  
GeneralRe: web service returning datasetmemberChris Norton11:56 11 Mar '05  
GeneralRe: web service returning datasetmemberStevan Rodrigues20:44 27 Mar '05  
GeneralNeed Urgent HelpmemberV. Anandan15:19 24 Feb '04  
GeneralRe: Need Urgent HelpmemberAlastair Todd3:03 21 Apr '04  
GeneralRe: Need Urgent HelpsussAnonymous3:46 4 May '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Mar 2004
Editor: Nishant Sivakumar
Copyright 2004 by Stevan Rodrigues
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project