SlideShare a Scribd company logo
1 of 25
Version 1.0.12 (January 2007)
Dino Esposito
http://weblogs.asp.net/despos
ASP.NET AJAX Extensions
Calling Remote Services from ASP.NET AJAX
Solid Quality ASP.NET AJAX
What You’re Going to See
• Defining remote services
• Implementing remote services
• REST vs. SOAP
• Considerations
Solid Quality ASP.NET AJAX
Need for services
• AJAX’s big win is invoking server code from the client page
– How do you expose server code to the client?
• Learn from the past
– Remote Scripting back in 1997
– Client engine nearly identical (was a Java applet)
– Server infrastructure based on classic ASP
– Public, fixed-name, contract-bound object exposed by the page and
invoked by the client code
– Contract implementation to determine subsequent server behavior
– Data exchanged through strings
– Client sets method to call and passes/receives values
Solid Quality ASP.NET AJAX
Define a contract
• Give your service an explicit contract
• Not strictly required by ASP.NET AJAX v1.0
• Implement the contract in a service class
– SOA architecture
Solid Quality ASP.NET AJAX
Expose the server API
• Build a RESTful service
– Build a service that documents itself through headers
and URLs, not SOAP
• Translated to the ASP.NET AJAX’s jargon
– Build an ASP.NET Web service with a special attribute
– Or
– Use the ASP.NET page as a special Web service
Solid Quality ASP.NET AJAX
Common misconception
• ASP.NET AJAX Lets You Call Web Services
– Untrue and incorrect
– but it may happen …
Solid Quality ASP.NET AJAX
REST vs. SOAP
• REpresentational State Transfer
– It’s all about the URL (and headers)
– Resource = URL
• Simple Object Access Protocol
– One URL fits all
– Use a XML schema to describe the call
– Use WSDL to describe the API
• SOAP-based services require the client know about
SOAP
Solid Quality ASP.NET AJAX
ASP.NET AJAX Perspective
• Expose the server API as REST services
• You write ASP.NET Web services with an extra
attribute
– Expose the service the REST way
– JSON feeds consumed
– No SOAP at all (simpler, faster)
– Same Origin Policy
• Call made to the local server
Solid Quality ASP.NET AJAX
JavaScript Proxy
• Mirrors all WebMethods of the Web services
• Extra members
– Properties: path, timeout, defaultUserContext,
defaultSucceededCallback, defaultFailedCallback
– Static (private) instance of the class
– Static members for the client to call (no proxy instantiation)
IntroAjax.WebServices.TimeService.prototype =
{
GetTime : function(succeededCallback, failedCallback, userContext)
{
return this._invoke(IntroAjax.WebServices.TimeService.get_path(),
'GetTime', false, {},
succeededCallback, failedCallback, userContext);
},
GetTimeAsFormat : function(timeFormat, succeededCallback, failedCallback, userContext)
{
return this._invoke(IntroAjax.WebServices.TimeService.get_path(),
'GetTimeAsFormat', false, {format: timeFormat},
succeededCallback, failedCallback, userContext);
}
}
Solid Quality ASP.NET AJAX
Calling Web Methods
• Use static methods on the proxy and add callbacks
<script language="javascript" type="text/javascript">
function getTime() {
var timeFormat = "ddd, dd MMMM yyyy [hh:mm:ss]";
IntroAjax.WebServices.TimeService.GetTimeFormat(timeFormat, onMethodComplete);
}
function onMethodComplete(results) {
$get("Label1").innerHTML = results;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"><title>The Time Web service</title></head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebServices/TimeService.asmx" />
</Services>
</asp:ScriptManager>
<h2> What time is it on the server? </h2>
<input type="button" value="Get Time" onclick="getTime()" />
<asp:Label runat="server" ID="Label1" />
</form>
</body>
</html>
Solid Quality ASP.NET AJAX
DEMO
• Invoking a Web service from an AJAX page
Solid Quality ASP.NET AJAX
JSON Serialization
• Page/server communication is based on strings
– Data is automatically serialized using JSON
• JavaScript Object Notation (simpler than XML)
– Text format to represent an object—actually, an (associative) array
– Needs a “proxy” to represent a server type
• Server and client infrastructure to proxy data
• Sample call:
– IntroAjax.TimeService.GetTimeFormat("ddd, dd MMMM yyyy [hh:mm:ss]", ...)
{"format":"ddd, dd MMMM yyyy [hh:mm:ss]"}
"Thu, 25 January 2007 [07:10:31]"
Request body
Response Content
Solid Quality ASP.NET AJAX
Using Complex Types
• JSON can be used to serialize complex types
– .NET types JSON-serialized using server class JavaScriptSerializer
– String processed on the client to an instance of a proxy class that
mirrors the original type
– And vice-versa
{"id":"ALFKI"} {
"__type":"IntroAjax.Customer",
"ID":"ALFKI",
"CompanyName":"Alfreds Futterkiste",
"ContactName":"Maria Anders",
"ContactTitle":"Sales Representative",
"Street":"Obere Str. 57",
"PostalCode":"12209",
"City":"Berlin",
"Country":"Germany",
"Phone":"030-0074321“
}
Response contentRequest body
IntroAjax.WebServices.MyDataService.LookupCustomer("ALFKI", onSearchComplete);
Solid Quality ASP.NET AJAX
Client Callbacks
• Methods on the proxy class have an extended prototype
– Params array, success and error callbacks, user context info
• Callbacks prototype
– function methodCompleted(results, context, methodName)
– User context is any information developers want to pass from the
client and reuse in the callback
IntroAjax.WebServices.TimeService.prototype = {
GetTimeAsFormat : function(timeFormat, succeededCallback, failedCallback, userContext) {
return this._invoke(IntroAjax.WebServices.TimeService.get_path(), // service path
'GetTimeAsFormat', // method name
false, // use GET verb
{format: timeFormat}, // parameters
succeededCallback, // completed callback
failedCallback, // error callback
userContext); // context extra information
}
}
Method prototype
Solid Quality ASP.NET AJAX
Exceptions and Timeouts
• Web service requests
– Set Content-Type to “application/json; charset=utf-8”
– In case of error, the response contains a jsonerror header
• Process results of the call using onSucceeded callback
• Exceptions on the server caught and exposed to client code
via onFailed callback
– Default failure callback defined: pops up a message box
• Timeout supported
– Set it through client timeout property
// Method can't take longer than 3 secs
function runOutOfTime() {
IntroAjax.WebServices.MyDataService.set_timeout(3000);
IntroAjax.WebServices.MyDataService.VeryLengthyTask(methodCompleted, methodError, "runOutOfTime()");
}
Solid Quality ASP.NET AJAX
Page Methods
• Page methods are similar to Web service method except
that they are defined in code-behind class
• Add a method to the page’s class and decorate it with the
WebMethod attribute
– Also works if method is defined inline in a text/C# block
• The page acts as a Web service
• Set EnablePageMethods to true in the script manager
• Public methods of the class wrapped up in a proxy
– Injected inline in the client page
– No registration required
Solid Quality ASP.NET AJAX
Defining a Page Method
• Public and static method
– Add the WebMethod attribute
– No access to controls in the page
– Just a way to expose pieces of “business” logic
– Parameters and return values JSON-serialized
– Necessary type definition injected in the client page
public partial class Samples_Ch07_CallPageMethod : System.Web.UI.Page
{
:
[WebMethod]
public static DateTime GetTime()
{
return DateTime.Now;
}
}
Sample page method
Can be any .NET type, including
custom types
Solid Quality ASP.NET AJAX
Invoking a Page Method
• Script manager commands the creation of a proxy class
– Named PageMethods defined inline
– As many methods as there are static WebMethods in the class
– Same prototype as for Web service methods
<script type="text/javascript">
function getTime()
{
PageMethods.GetTime(onMethodComplete);
}
function onMethodComplete(results, context, methodName)
{
// Format the date-time object to a more readable string
var displayString = results.format("ddd, dd MMMM yyyy");
$get("Label1").innerHTML = displayString;
}
</script>
<asp:ScriptManager runat="server"
ID="ScriptManager1"
EnablePageMethods="true" />
<h2>What time is it on the server?</h2>
<input type="button" value="Get Time"
onclick="getTime()" />
Solid Quality ASP.NET AJAX
DEMO
• Invoking a page method
• Accessing session state from the client
Solid Quality ASP.NET AJAX
Web services vs. Page methods
• Web service methods
– Callable from any pages and reusable
– Publicly exposed over the Web and publicly callable
• Page methods
– Have client callable server code without developing a Web service
– No page context associated with the call
• Can’t control who’s calling
– Do not use for critical BLL
– Only for UI-level logic
Solid Quality ASP.NET AJAX
Bridging External Services
• Web services you can call must be local to the application
– What if you want to call an external service?
– Different application OR different machine/network
• Client AJAX calls into an external service (i.e., Amazon)
– Can’t make assumptions on the Web server
• Client AJAX calls into an ASP.NET service
– Some browsers prohibit cross-site calls from the client
• The bottom line
– Client calls its own server
– Server places a server-to-server call to the Web service
Solid Quality ASP.NET AJAX
Server-to-Server
• Manual implementation
– Invoke a server method that, in turn, call the Web service
– Updatable panel or page method
• ASP.NET AJAX assisted implementation
– The bridge technology
– Enables developers to create programmatic gateways to Web
services on the Internet
– Chief enabling technology for building mash-ups
Solid Quality ASP.NET AJAX
DEMO
• Stock-watching demo (using Yahoo)
– UpdatePanel with a grid
– Timer control to refresh periodically
– A homemade service class to get stock quotes
– UI code to bind the grid
Solid Quality ASP.NET AJAX
Summary
• In ASP.NET AJAX, Web services are a way to execute server-
side code in response to a client action
• Local Web services or page methods
• JSON serialization
• JavaScript callbacks to merge results with the page
• Server-to-server calls to external services
Solid Quality ASP.NET AJAX
References
• For more information, refer to
– Introducing Microsoft ASP.NET 2.0 AJAX Extensions
• Microsoft Press, June 2007

More Related Content

What's hot

Developing and Hosting SOAP Based Services
Developing and Hosting SOAP Based ServicesDeveloping and Hosting SOAP Based Services
Developing and Hosting SOAP Based ServicesStephenKardian
 
Testing web services
Testing web servicesTesting web services
Testing web servicesTaras Lytvyn
 
PHP and Web Services
PHP and Web ServicesPHP and Web Services
PHP and Web ServicesBruno Pedro
 
Webservices Overview : XML RPC, SOAP and REST
Webservices Overview : XML RPC, SOAP and RESTWebservices Overview : XML RPC, SOAP and REST
Webservices Overview : XML RPC, SOAP and RESTPradeep Kumar
 
Web service的自动化测试 soap ui的介绍
Web service的自动化测试 soap ui的介绍Web service的自动化测试 soap ui的介绍
Web service的自动化测试 soap ui的介绍bqconf
 
Client Side MVC with Backbone and Rails
Client Side MVC with Backbone and RailsClient Side MVC with Backbone and Rails
Client Side MVC with Backbone and RailsTom Z Zeng
 
Overview of Rest Service and ASP.NET WEB API
Overview of Rest Service and ASP.NET WEB APIOverview of Rest Service and ASP.NET WEB API
Overview of Rest Service and ASP.NET WEB APIPankaj Bajaj
 
An Overview of Web Services: SOAP and REST
An Overview of Web Services: SOAP and REST An Overview of Web Services: SOAP and REST
An Overview of Web Services: SOAP and REST Ram Awadh Prasad, PMP
 
PLAT-8 Spring Web Scripts and Spring Surf
PLAT-8 Spring Web Scripts and Spring SurfPLAT-8 Spring Web Scripts and Spring Surf
PLAT-8 Spring Web Scripts and Spring SurfAlfresco Software
 
Web Service Testing By Sheshadri Mishra
Web Service Testing By Sheshadri MishraWeb Service Testing By Sheshadri Mishra
Web Service Testing By Sheshadri MishraSheshadri Mishra
 
REST services and IBM Domino/XWork - DanNotes 19-20. november 2014
REST services and IBM Domino/XWork - DanNotes 19-20. november 2014REST services and IBM Domino/XWork - DanNotes 19-20. november 2014
REST services and IBM Domino/XWork - DanNotes 19-20. november 2014John Dalsgaard
 
A 20 minute introduction to AngularJS for XPage developers
A 20 minute introduction to AngularJS for XPage developersA 20 minute introduction to AngularJS for XPage developers
A 20 minute introduction to AngularJS for XPage developersMark Leusink
 
Web flowpresentation
Web flowpresentationWeb flowpresentation
Web flowpresentationRoman Brovko
 
REST API Recommendations
REST API RecommendationsREST API Recommendations
REST API RecommendationsJeelani Shaik
 
Jim Webber A Couple Of Ways To Skin An Internet Scale Catx
Jim Webber A Couple Of Ways To Skin An Internet Scale CatxJim Webber A Couple Of Ways To Skin An Internet Scale Catx
Jim Webber A Couple Of Ways To Skin An Internet Scale Catxdeimos
 
Microservice Websites (microXchg 2017)
Microservice Websites (microXchg 2017)Microservice Websites (microXchg 2017)
Microservice Websites (microXchg 2017)Gustaf Nilsson Kotte
 

What's hot (20)

Developing and Hosting SOAP Based Services
Developing and Hosting SOAP Based ServicesDeveloping and Hosting SOAP Based Services
Developing and Hosting SOAP Based Services
 
Testing web services
Testing web servicesTesting web services
Testing web services
 
PHP and Web Services
PHP and Web ServicesPHP and Web Services
PHP and Web Services
 
Webservices Overview : XML RPC, SOAP and REST
Webservices Overview : XML RPC, SOAP and RESTWebservices Overview : XML RPC, SOAP and REST
Webservices Overview : XML RPC, SOAP and REST
 
Web service的自动化测试 soap ui的介绍
Web service的自动化测试 soap ui的介绍Web service的自动化测试 soap ui的介绍
Web service的自动化测试 soap ui的介绍
 
Client Side MVC with Backbone and Rails
Client Side MVC with Backbone and RailsClient Side MVC with Backbone and Rails
Client Side MVC with Backbone and Rails
 
Overview of Rest Service and ASP.NET WEB API
Overview of Rest Service and ASP.NET WEB APIOverview of Rest Service and ASP.NET WEB API
Overview of Rest Service and ASP.NET WEB API
 
MEAN.js Workshop
MEAN.js WorkshopMEAN.js Workshop
MEAN.js Workshop
 
An Overview of Web Services: SOAP and REST
An Overview of Web Services: SOAP and REST An Overview of Web Services: SOAP and REST
An Overview of Web Services: SOAP and REST
 
PLAT-8 Spring Web Scripts and Spring Surf
PLAT-8 Spring Web Scripts and Spring SurfPLAT-8 Spring Web Scripts and Spring Surf
PLAT-8 Spring Web Scripts and Spring Surf
 
Web Service Testing By Sheshadri Mishra
Web Service Testing By Sheshadri MishraWeb Service Testing By Sheshadri Mishra
Web Service Testing By Sheshadri Mishra
 
REST services and IBM Domino/XWork - DanNotes 19-20. november 2014
REST services and IBM Domino/XWork - DanNotes 19-20. november 2014REST services and IBM Domino/XWork - DanNotes 19-20. november 2014
REST services and IBM Domino/XWork - DanNotes 19-20. november 2014
 
Web API Basics
Web API BasicsWeb API Basics
Web API Basics
 
A 20 minute introduction to AngularJS for XPage developers
A 20 minute introduction to AngularJS for XPage developersA 20 minute introduction to AngularJS for XPage developers
A 20 minute introduction to AngularJS for XPage developers
 
L13 Presentation Layer Design
L13 Presentation Layer DesignL13 Presentation Layer Design
L13 Presentation Layer Design
 
Web flowpresentation
Web flowpresentationWeb flowpresentation
Web flowpresentation
 
REST API Recommendations
REST API RecommendationsREST API Recommendations
REST API Recommendations
 
Jim Webber A Couple Of Ways To Skin An Internet Scale Catx
Jim Webber A Couple Of Ways To Skin An Internet Scale CatxJim Webber A Couple Of Ways To Skin An Internet Scale Catx
Jim Webber A Couple Of Ways To Skin An Internet Scale Catx
 
Javascript for Wep Apps
Javascript for Wep AppsJavascript for Wep Apps
Javascript for Wep Apps
 
Microservice Websites (microXchg 2017)
Microservice Websites (microXchg 2017)Microservice Websites (microXchg 2017)
Microservice Websites (microXchg 2017)
 

Viewers also liked

Viewers also liked (6)

RA.50012 Informe TICS
RA.50012 Informe TICSRA.50012 Informe TICS
RA.50012 Informe TICS
 
Get it right #sw grenoble 2013
Get it right   #sw grenoble 2013Get it right   #sw grenoble 2013
Get it right #sw grenoble 2013
 
Lectura 4 diferencia entre cambios quimicos y fisicos
Lectura 4 diferencia entre cambios quimicos y fisicosLectura 4 diferencia entre cambios quimicos y fisicos
Lectura 4 diferencia entre cambios quimicos y fisicos
 
E rate presentation
E rate presentationE rate presentation
E rate presentation
 
5248
52485248
5248
 
word
wordword
word
 

Similar to Espositoajaxremote 1210732828647866-8

Esposito Ajax Remote
Esposito Ajax RemoteEsposito Ajax Remote
Esposito Ajax Remoteask bills
 
web services-May 25.ppt
web services-May 25.pptweb services-May 25.ppt
web services-May 25.pptShivaangiKrish
 
Web services - A Practical Approach
Web services - A Practical ApproachWeb services - A Practical Approach
Web services - A Practical ApproachMadhaiyan Muthu
 
SharePoint Client Object Model (CSOM)
SharePoint Client Object Model (CSOM)SharePoint Client Object Model (CSOM)
SharePoint Client Object Model (CSOM)Kashif Imran
 
SRV418 Deep Dive on Accelerating Content, APIs, and Applications with Amazon ...
SRV418 Deep Dive on Accelerating Content, APIs, and Applications with Amazon ...SRV418 Deep Dive on Accelerating Content, APIs, and Applications with Amazon ...
SRV418 Deep Dive on Accelerating Content, APIs, and Applications with Amazon ...Amazon Web Services
 
Consume wsa
Consume wsaConsume wsa
Consume wsamahe797
 
SFDC Inbound Integrations
SFDC Inbound IntegrationsSFDC Inbound Integrations
SFDC Inbound IntegrationsSujit Kumar
 
Using Java to implement SOAP Web Services: JAX-WS
Using Java to implement SOAP Web Services: JAX-WS�Using Java to implement SOAP Web Services: JAX-WS�
Using Java to implement SOAP Web Services: JAX-WSKatrien Verbert
 
API Testing. Streamline your testing process.
API Testing. Streamline your testing process.API Testing. Streamline your testing process.
API Testing. Streamline your testing process.Andrey Oleynik
 
REST to JavaScript for Better Client-side Development
REST to JavaScript for Better Client-side DevelopmentREST to JavaScript for Better Client-side Development
REST to JavaScript for Better Client-side DevelopmentHyunghun Cho
 
Codestrong 2012 breakout session building your own custom cloud services
Codestrong 2012 breakout session   building your own custom cloud servicesCodestrong 2012 breakout session   building your own custom cloud services
Codestrong 2012 breakout session building your own custom cloud servicesAxway Appcelerator
 
StackMate - CloudFormation for CloudStack
StackMate - CloudFormation for CloudStackStackMate - CloudFormation for CloudStack
StackMate - CloudFormation for CloudStackChiradeep Vittal
 
SharePoint Saturday The Conference DC - How the client object model saved the...
SharePoint Saturday The Conference DC - How the client object model saved the...SharePoint Saturday The Conference DC - How the client object model saved the...
SharePoint Saturday The Conference DC - How the client object model saved the...Liam Cleary [MVP]
 
Java Web services
Java Web servicesJava Web services
Java Web servicesSujit Kumar
 

Similar to Espositoajaxremote 1210732828647866-8 (20)

Ntg web services
Ntg   web servicesNtg   web services
Ntg web services
 
Esposito Ajax Remote
Esposito Ajax RemoteEsposito Ajax Remote
Esposito Ajax Remote
 
web services-May 25.ppt
web services-May 25.pptweb services-May 25.ppt
web services-May 25.ppt
 
06 web api
06 web api06 web api
06 web api
 
Web services - A Practical Approach
Web services - A Practical ApproachWeb services - A Practical Approach
Web services - A Practical Approach
 
SharePoint Client Object Model (CSOM)
SharePoint Client Object Model (CSOM)SharePoint Client Object Model (CSOM)
SharePoint Client Object Model (CSOM)
 
SRV418 Deep Dive on Accelerating Content, APIs, and Applications with Amazon ...
SRV418 Deep Dive on Accelerating Content, APIs, and Applications with Amazon ...SRV418 Deep Dive on Accelerating Content, APIs, and Applications with Amazon ...
SRV418 Deep Dive on Accelerating Content, APIs, and Applications with Amazon ...
 
Consume wsa
Consume wsaConsume wsa
Consume wsa
 
Web services intro.
Web services intro.Web services intro.
Web services intro.
 
SFDC Inbound Integrations
SFDC Inbound IntegrationsSFDC Inbound Integrations
SFDC Inbound Integrations
 
Using Java to implement SOAP Web Services: JAX-WS
Using Java to implement SOAP Web Services: JAX-WS�Using Java to implement SOAP Web Services: JAX-WS�
Using Java to implement SOAP Web Services: JAX-WS
 
API Testing. Streamline your testing process.
API Testing. Streamline your testing process.API Testing. Streamline your testing process.
API Testing. Streamline your testing process.
 
REST to JavaScript for Better Client-side Development
REST to JavaScript for Better Client-side DevelopmentREST to JavaScript for Better Client-side Development
REST to JavaScript for Better Client-side Development
 
Codestrong 2012 breakout session building your own custom cloud services
Codestrong 2012 breakout session   building your own custom cloud servicesCodestrong 2012 breakout session   building your own custom cloud services
Codestrong 2012 breakout session building your own custom cloud services
 
ASP
ASPASP
ASP
 
Test
TestTest
Test
 
StackMate - CloudFormation for CloudStack
StackMate - CloudFormation for CloudStackStackMate - CloudFormation for CloudStack
StackMate - CloudFormation for CloudStack
 
Introduction ASP
Introduction ASPIntroduction ASP
Introduction ASP
 
SharePoint Saturday The Conference DC - How the client object model saved the...
SharePoint Saturday The Conference DC - How the client object model saved the...SharePoint Saturday The Conference DC - How the client object model saved the...
SharePoint Saturday The Conference DC - How the client object model saved the...
 
Java Web services
Java Web servicesJava Web services
Java Web services
 

More from Sopheak Sem

Job annocement field supervisor v-1
Job annocement field supervisor v-1Job annocement field supervisor v-1
Job annocement field supervisor v-1Sopheak Sem
 
Chc ja pc shp_tmor puk_02022015
Chc ja pc shp_tmor puk_02022015Chc ja pc shp_tmor puk_02022015
Chc ja pc shp_tmor puk_02022015Sopheak Sem
 
Chc ja-sccs 12.12.2014
Chc ja-sccs 12.12.2014Chc ja-sccs 12.12.2014
Chc ja-sccs 12.12.2014Sopheak Sem
 
Chc ja-m &e officer-12.12.2014
Chc ja-m &e officer-12.12.2014Chc ja-m &e officer-12.12.2014
Chc ja-m &e officer-12.12.2014Sopheak Sem
 
Chc ja-mdr-tb nurses.12.12.2014
Chc ja-mdr-tb nurses.12.12.2014Chc ja-mdr-tb nurses.12.12.2014
Chc ja-mdr-tb nurses.12.12.2014Sopheak Sem
 
Outline of consultative workshop on integrated promotion v.1.1 14_oct2014
Outline of consultative workshop on integrated promotion v.1.1 14_oct2014Outline of consultative workshop on integrated promotion v.1.1 14_oct2014
Outline of consultative workshop on integrated promotion v.1.1 14_oct2014Sopheak Sem
 
Announcement on advaced tot bcc
Announcement on advaced tot bccAnnouncement on advaced tot bcc
Announcement on advaced tot bccSopheak Sem
 
Application form advanced tot bcc
Application form advanced tot bccApplication form advanced tot bcc
Application form advanced tot bccSopheak Sem
 
Outline of sales promotion workshop v 1 2_12aug14
Outline of sales promotion workshop v 1 2_12aug14Outline of sales promotion workshop v 1 2_12aug14
Outline of sales promotion workshop v 1 2_12aug14Sopheak Sem
 
Analysis of-gender-and-emerging-issues-with-focus-on-ageing-population-help a...
Analysis of-gender-and-emerging-issues-with-focus-on-ageing-population-help a...Analysis of-gender-and-emerging-issues-with-focus-on-ageing-population-help a...
Analysis of-gender-and-emerging-issues-with-focus-on-ageing-population-help a...Sopheak Sem
 
Ageing&migrationin cambodia26dec2013 (1)
Ageing&migrationin cambodia26dec2013 (1)Ageing&migrationin cambodia26dec2013 (1)
Ageing&migrationin cambodia26dec2013 (1)Sopheak Sem
 
Cambodia's older people's association guidelines (1)
Cambodia's older people's association guidelines (1)Cambodia's older people's association guidelines (1)
Cambodia's older people's association guidelines (1)Sopheak Sem
 
Support for the elderly in cambodia letter
Support for the elderly in cambodia letterSupport for the elderly in cambodia letter
Support for the elderly in cambodia letterSopheak Sem
 
Isps course announcement 2014 15_giz
Isps course announcement 2014 15_gizIsps course announcement 2014 15_giz
Isps course announcement 2014 15_gizSopheak Sem
 
To r lgcr 14-007
To r lgcr 14-007To r lgcr 14-007
To r lgcr 14-007Sopheak Sem
 
Application form of chpm
Application form of chpmApplication form of chpm
Application form of chpmSopheak Sem
 
Announcement on chpm
Announcement on chpmAnnouncement on chpm
Announcement on chpmSopheak Sem
 
Concept note heifer
Concept note heiferConcept note heifer
Concept note heiferSopheak Sem
 

More from Sopheak Sem (20)

Job annocement field supervisor v-1
Job annocement field supervisor v-1Job annocement field supervisor v-1
Job annocement field supervisor v-1
 
Chc ja pc shp_tmor puk_02022015
Chc ja pc shp_tmor puk_02022015Chc ja pc shp_tmor puk_02022015
Chc ja pc shp_tmor puk_02022015
 
Chc ja-sccs 12.12.2014
Chc ja-sccs 12.12.2014Chc ja-sccs 12.12.2014
Chc ja-sccs 12.12.2014
 
Chc ja-m &e officer-12.12.2014
Chc ja-m &e officer-12.12.2014Chc ja-m &e officer-12.12.2014
Chc ja-m &e officer-12.12.2014
 
Chc ja-mdr-tb nurses.12.12.2014
Chc ja-mdr-tb nurses.12.12.2014Chc ja-mdr-tb nurses.12.12.2014
Chc ja-mdr-tb nurses.12.12.2014
 
Outline of consultative workshop on integrated promotion v.1.1 14_oct2014
Outline of consultative workshop on integrated promotion v.1.1 14_oct2014Outline of consultative workshop on integrated promotion v.1.1 14_oct2014
Outline of consultative workshop on integrated promotion v.1.1 14_oct2014
 
Announcement on advaced tot bcc
Announcement on advaced tot bccAnnouncement on advaced tot bcc
Announcement on advaced tot bcc
 
Application form advanced tot bcc
Application form advanced tot bccApplication form advanced tot bcc
Application form advanced tot bcc
 
Outline of sales promotion workshop v 1 2_12aug14
Outline of sales promotion workshop v 1 2_12aug14Outline of sales promotion workshop v 1 2_12aug14
Outline of sales promotion workshop v 1 2_12aug14
 
Analysis of-gender-and-emerging-issues-with-focus-on-ageing-population-help a...
Analysis of-gender-and-emerging-issues-with-focus-on-ageing-population-help a...Analysis of-gender-and-emerging-issues-with-focus-on-ageing-population-help a...
Analysis of-gender-and-emerging-issues-with-focus-on-ageing-population-help a...
 
Ageing&migrationin cambodia26dec2013 (1)
Ageing&migrationin cambodia26dec2013 (1)Ageing&migrationin cambodia26dec2013 (1)
Ageing&migrationin cambodia26dec2013 (1)
 
Cambodia's older people's association guidelines (1)
Cambodia's older people's association guidelines (1)Cambodia's older people's association guidelines (1)
Cambodia's older people's association guidelines (1)
 
Support for the elderly in cambodia letter
Support for the elderly in cambodia letterSupport for the elderly in cambodia letter
Support for the elderly in cambodia letter
 
Isps course announcement 2014 15_giz
Isps course announcement 2014 15_gizIsps course announcement 2014 15_giz
Isps course announcement 2014 15_giz
 
To r lgcr 14-007
To r lgcr 14-007To r lgcr 14-007
To r lgcr 14-007
 
Application form of chpm
Application form of chpmApplication form of chpm
Application form of chpm
 
Announcement on chpm
Announcement on chpmAnnouncement on chpm
Announcement on chpm
 
Bfh
BfhBfh
Bfh
 
Afh
AfhAfh
Afh
 
Concept note heifer
Concept note heiferConcept note heifer
Concept note heifer
 

Recently uploaded

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 

Recently uploaded (20)

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 

Espositoajaxremote 1210732828647866-8

  • 1. Version 1.0.12 (January 2007) Dino Esposito http://weblogs.asp.net/despos ASP.NET AJAX Extensions Calling Remote Services from ASP.NET AJAX
  • 2. Solid Quality ASP.NET AJAX What You’re Going to See • Defining remote services • Implementing remote services • REST vs. SOAP • Considerations
  • 3. Solid Quality ASP.NET AJAX Need for services • AJAX’s big win is invoking server code from the client page – How do you expose server code to the client? • Learn from the past – Remote Scripting back in 1997 – Client engine nearly identical (was a Java applet) – Server infrastructure based on classic ASP – Public, fixed-name, contract-bound object exposed by the page and invoked by the client code – Contract implementation to determine subsequent server behavior – Data exchanged through strings – Client sets method to call and passes/receives values
  • 4. Solid Quality ASP.NET AJAX Define a contract • Give your service an explicit contract • Not strictly required by ASP.NET AJAX v1.0 • Implement the contract in a service class – SOA architecture
  • 5. Solid Quality ASP.NET AJAX Expose the server API • Build a RESTful service – Build a service that documents itself through headers and URLs, not SOAP • Translated to the ASP.NET AJAX’s jargon – Build an ASP.NET Web service with a special attribute – Or – Use the ASP.NET page as a special Web service
  • 6. Solid Quality ASP.NET AJAX Common misconception • ASP.NET AJAX Lets You Call Web Services – Untrue and incorrect – but it may happen …
  • 7. Solid Quality ASP.NET AJAX REST vs. SOAP • REpresentational State Transfer – It’s all about the URL (and headers) – Resource = URL • Simple Object Access Protocol – One URL fits all – Use a XML schema to describe the call – Use WSDL to describe the API • SOAP-based services require the client know about SOAP
  • 8. Solid Quality ASP.NET AJAX ASP.NET AJAX Perspective • Expose the server API as REST services • You write ASP.NET Web services with an extra attribute – Expose the service the REST way – JSON feeds consumed – No SOAP at all (simpler, faster) – Same Origin Policy • Call made to the local server
  • 9. Solid Quality ASP.NET AJAX JavaScript Proxy • Mirrors all WebMethods of the Web services • Extra members – Properties: path, timeout, defaultUserContext, defaultSucceededCallback, defaultFailedCallback – Static (private) instance of the class – Static members for the client to call (no proxy instantiation) IntroAjax.WebServices.TimeService.prototype = { GetTime : function(succeededCallback, failedCallback, userContext) { return this._invoke(IntroAjax.WebServices.TimeService.get_path(), 'GetTime', false, {}, succeededCallback, failedCallback, userContext); }, GetTimeAsFormat : function(timeFormat, succeededCallback, failedCallback, userContext) { return this._invoke(IntroAjax.WebServices.TimeService.get_path(), 'GetTimeAsFormat', false, {format: timeFormat}, succeededCallback, failedCallback, userContext); } }
  • 10. Solid Quality ASP.NET AJAX Calling Web Methods • Use static methods on the proxy and add callbacks <script language="javascript" type="text/javascript"> function getTime() { var timeFormat = "ddd, dd MMMM yyyy [hh:mm:ss]"; IntroAjax.WebServices.TimeService.GetTimeFormat(timeFormat, onMethodComplete); } function onMethodComplete(results) { $get("Label1").innerHTML = results; } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"><title>The Time Web service</title></head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> <Services> <asp:ServiceReference Path="~/WebServices/TimeService.asmx" /> </Services> </asp:ScriptManager> <h2> What time is it on the server? </h2> <input type="button" value="Get Time" onclick="getTime()" /> <asp:Label runat="server" ID="Label1" /> </form> </body> </html>
  • 11. Solid Quality ASP.NET AJAX DEMO • Invoking a Web service from an AJAX page
  • 12. Solid Quality ASP.NET AJAX JSON Serialization • Page/server communication is based on strings – Data is automatically serialized using JSON • JavaScript Object Notation (simpler than XML) – Text format to represent an object—actually, an (associative) array – Needs a “proxy” to represent a server type • Server and client infrastructure to proxy data • Sample call: – IntroAjax.TimeService.GetTimeFormat("ddd, dd MMMM yyyy [hh:mm:ss]", ...) {"format":"ddd, dd MMMM yyyy [hh:mm:ss]"} "Thu, 25 January 2007 [07:10:31]" Request body Response Content
  • 13. Solid Quality ASP.NET AJAX Using Complex Types • JSON can be used to serialize complex types – .NET types JSON-serialized using server class JavaScriptSerializer – String processed on the client to an instance of a proxy class that mirrors the original type – And vice-versa {"id":"ALFKI"} { "__type":"IntroAjax.Customer", "ID":"ALFKI", "CompanyName":"Alfreds Futterkiste", "ContactName":"Maria Anders", "ContactTitle":"Sales Representative", "Street":"Obere Str. 57", "PostalCode":"12209", "City":"Berlin", "Country":"Germany", "Phone":"030-0074321“ } Response contentRequest body IntroAjax.WebServices.MyDataService.LookupCustomer("ALFKI", onSearchComplete);
  • 14. Solid Quality ASP.NET AJAX Client Callbacks • Methods on the proxy class have an extended prototype – Params array, success and error callbacks, user context info • Callbacks prototype – function methodCompleted(results, context, methodName) – User context is any information developers want to pass from the client and reuse in the callback IntroAjax.WebServices.TimeService.prototype = { GetTimeAsFormat : function(timeFormat, succeededCallback, failedCallback, userContext) { return this._invoke(IntroAjax.WebServices.TimeService.get_path(), // service path 'GetTimeAsFormat', // method name false, // use GET verb {format: timeFormat}, // parameters succeededCallback, // completed callback failedCallback, // error callback userContext); // context extra information } } Method prototype
  • 15. Solid Quality ASP.NET AJAX Exceptions and Timeouts • Web service requests – Set Content-Type to “application/json; charset=utf-8” – In case of error, the response contains a jsonerror header • Process results of the call using onSucceeded callback • Exceptions on the server caught and exposed to client code via onFailed callback – Default failure callback defined: pops up a message box • Timeout supported – Set it through client timeout property // Method can't take longer than 3 secs function runOutOfTime() { IntroAjax.WebServices.MyDataService.set_timeout(3000); IntroAjax.WebServices.MyDataService.VeryLengthyTask(methodCompleted, methodError, "runOutOfTime()"); }
  • 16. Solid Quality ASP.NET AJAX Page Methods • Page methods are similar to Web service method except that they are defined in code-behind class • Add a method to the page’s class and decorate it with the WebMethod attribute – Also works if method is defined inline in a text/C# block • The page acts as a Web service • Set EnablePageMethods to true in the script manager • Public methods of the class wrapped up in a proxy – Injected inline in the client page – No registration required
  • 17. Solid Quality ASP.NET AJAX Defining a Page Method • Public and static method – Add the WebMethod attribute – No access to controls in the page – Just a way to expose pieces of “business” logic – Parameters and return values JSON-serialized – Necessary type definition injected in the client page public partial class Samples_Ch07_CallPageMethod : System.Web.UI.Page { : [WebMethod] public static DateTime GetTime() { return DateTime.Now; } } Sample page method Can be any .NET type, including custom types
  • 18. Solid Quality ASP.NET AJAX Invoking a Page Method • Script manager commands the creation of a proxy class – Named PageMethods defined inline – As many methods as there are static WebMethods in the class – Same prototype as for Web service methods <script type="text/javascript"> function getTime() { PageMethods.GetTime(onMethodComplete); } function onMethodComplete(results, context, methodName) { // Format the date-time object to a more readable string var displayString = results.format("ddd, dd MMMM yyyy"); $get("Label1").innerHTML = displayString; } </script> <asp:ScriptManager runat="server" ID="ScriptManager1" EnablePageMethods="true" /> <h2>What time is it on the server?</h2> <input type="button" value="Get Time" onclick="getTime()" />
  • 19. Solid Quality ASP.NET AJAX DEMO • Invoking a page method • Accessing session state from the client
  • 20. Solid Quality ASP.NET AJAX Web services vs. Page methods • Web service methods – Callable from any pages and reusable – Publicly exposed over the Web and publicly callable • Page methods – Have client callable server code without developing a Web service – No page context associated with the call • Can’t control who’s calling – Do not use for critical BLL – Only for UI-level logic
  • 21. Solid Quality ASP.NET AJAX Bridging External Services • Web services you can call must be local to the application – What if you want to call an external service? – Different application OR different machine/network • Client AJAX calls into an external service (i.e., Amazon) – Can’t make assumptions on the Web server • Client AJAX calls into an ASP.NET service – Some browsers prohibit cross-site calls from the client • The bottom line – Client calls its own server – Server places a server-to-server call to the Web service
  • 22. Solid Quality ASP.NET AJAX Server-to-Server • Manual implementation – Invoke a server method that, in turn, call the Web service – Updatable panel or page method • ASP.NET AJAX assisted implementation – The bridge technology – Enables developers to create programmatic gateways to Web services on the Internet – Chief enabling technology for building mash-ups
  • 23. Solid Quality ASP.NET AJAX DEMO • Stock-watching demo (using Yahoo) – UpdatePanel with a grid – Timer control to refresh periodically – A homemade service class to get stock quotes – UI code to bind the grid
  • 24. Solid Quality ASP.NET AJAX Summary • In ASP.NET AJAX, Web services are a way to execute server- side code in response to a client action • Local Web services or page methods • JSON serialization • JavaScript callbacks to merge results with the page • Server-to-server calls to external services
  • 25. Solid Quality ASP.NET AJAX References • For more information, refer to – Introducing Microsoft ASP.NET 2.0 AJAX Extensions • Microsoft Press, June 2007