|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThe 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 servicesXML 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 <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
<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 BehaviorThe 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,
<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 ServicesThe 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" );
}
<body bgColor="#ffffff" text="#000000" link="#0000cc" vLink="#0000cc"
aLink="#ff0000" leftmargin="0" topmargin="0" marginwidth="0"
marginheight="0" onload="Init()">
Calling WebMethodsThe 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,
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, 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 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 //--------------------------------------------------
// 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 ObjectWhenever Here is the list of properties of
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.
When an error or problem occurs for a method call, the error property is set to true and the 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. LimitationsThe following are the limitations of the behavior
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||