Presentation is loading. Please wait.

Presentation is loading. Please wait.

ASP.Net, web services--- asynchronous and synchronous and AJAX By Thakur Rashmi Singh.

Similar presentations


Presentation on theme: "ASP.Net, web services--- asynchronous and synchronous and AJAX By Thakur Rashmi Singh."— Presentation transcript:

1 ASP.Net, web services--- asynchronous and synchronous and AJAX By Thakur Rashmi Singh

2 Presentation Flow  What is a webservice  Making a web service Call  What is synchronous call  What is asynchronous call  What is Ajax.  Conclusion.

3 Web Services?  “The W3C defines a Web service as a software system designed to support interoperable Machine to Machine interaction over a network”W3C What does that mean…  They allow programs written in different languages on different platforms to Communicate with each other in a standards- based way.  Provide an Service Oriented Architecture “SOA”.

4 Making Web Service Calls …  2 ways of adding the reference From cmd  by giving the command wsdl URL From the “Add Web Reference”.  Either methods a proxy class is generated.  When you choose the Add Web Reference option in Microsoft Visual Studio®.NET, Visual Studio.NET generates a proxy class.  Web Services are called through these proxy classes.

5 Proxy Class?  Generated by WSDL to describe what methods are available in the web service without showing the logic behind them.  proxy class takes care of the details of generating, transmitting and parsing the XML required to actually calling the remote Web service.

6  The generated proxy class provides three methods for each Web service operation. One method named the same (used for synchronous calling) pair of methods prefixed with Begin and End to call the Web service asynchronously.

7 Ways of calling a Web Service  There are 2 ways of calling a web service Calling it synchronously Calling it Asynchronously.

8 Synchronous Calling…  Calling the Web service synchronously is a matter of creating an instance of the proxy and calling the method named the same as the Web service operation. Eg:

9 Performance……?  Web services are slow ! Reason?  latency involved when making calls across a network, especially over a relatively low- speed wireless connection.  Communicating large amounts of data may also cause the slowness. Result? Calling synchronously, the user interface of your application might freeze!!!

10 Solution???  The best way to make Web service calls is to make asynchronous Web service calls !

11 Asynchronous Calls…  When a synchronous call is made, the calling thread is blocked until the method completes whereas an asynchronous call is made on a different thread in a Thread pool, and this allows the original thread to continue its work while the asynchronous call is in progress.  Thread Pool? When a managed application is executed, the.NET runtime offers a pool of threads that will be created the first time the code accesses it. No overhead of creating and destroying separate threads for each task.

12  Once the thread finishes it job we need to retrieve the results from the thread  Before retrieving the results of an Asynchronous Web service call, the application must first determine that the call has completed How do we know at what time the thread will complete the job?

13 Solution… Three ways to determine if the thread has finished its work or not.  Notification  Polling  Wait Handles

14 Notification  In this case, there is a function which can be called to notify once the thread completes it job. protected void Button1_Click(object sender, EventArgs e) { localhost.Service s = new localhost.Service(); s.BeginDelayedResponse(10000, Callback, s); } private void Callback(IAsyncResult result) { localhost.Service s = (localhost.Service)result.AsyncState; string re = s.EndDelayedResponse(result); Label2.Text = re; }  Only option which can exit the current working thread.

15 Polling  In some rare occasions, an application may wish to continue processing while periodically checking on the Web service call.

16  If EndDelayedResponse is called before the request has completed, it will simply block until the request does complete.  But this method if not used carefully can end up using a lot of your machine's CPU cycles.

17 Wait Handles  Can be used when you need to make asynchronous calls, but you do not want to release the thread you are currently executing in.  It blocks the current thread till the web service completes.

18  It can be WaitOne, WaitAll, WaitAny  WaitOne – wait for one webservice to complete  WaitAll – wait for all web services mentioned in the parameters array to complete  WaitAny – if any of the Web services mentioned in the parameters array completes then continue.  All can have timeout as a parameter.

19 AJAX  AJAX has many acronyms. The most common one is Asynchronous Java and XML.  It is a web techinque of making Asynchronous web requests.

20 Calling Web Services using AJAX  ASP.NET 2.0 AJAX Extensions enable the exact same experience of seamless proxy generation for Web services for client-side JavaScript that will run in the browser  You can invoke methods of the service from JavaScript by adding a ServiceReference

21  You can now write your Javascript and call your webservice function OnLookup() { var stb = document.getElementById("_symbolTextBox"); StockQuoteService.GetStockQuote( stb.value, OnLookupComplete, OnError); } Where OnLookupComplete is another function which will be called after the webservice has completed OnError – is a function which is called during exceptions.

22  Proxy is created in the client side by injecting a tag.  This proxy is invoked whenever a web service is requested.  This way of client side proxy generation and server side javscript support can be used intuitively to make asyn web calls.

23 If So easy then why not always use AJAX???

24 NoOoO….  Although it looks simple enough AJAX has one main concern…  The above technique won't work for external web services.  ASP.NET AJAX internally relies on the XML HTTP object  The XML HTTP object, for security reasons cannot communicate outside the originating web site. So no External Web Services…

25  Although no direct solution for this has been found.  But a “bridge” technique can be used instead. Add a web reference to the external web service in your web site. Create a new web service in your web site. In the newly created web service, provide wrapper web methods that call the external web methods via web reference. Call this newly added web service from the client application

26 Interesting feature of ASP.NET AJAX  AJAX Extensions provide two pre-built services ProfileService AuthenicationService  With these two client-side proxy classes you can set and retrieve profile values for individual clients, as well as perform authentication (via the default Membership provider) and grant authentication cookies completely within client script.

27 Conclusion  Whenever executing a lengthy process where performance is an issue, Asynchronous calls are much better.  Also AJAX’s flashy looks and responsive UI and the ability to create client side proxies and using wrappers to call external web servies surely have the most compelling features so far.

28 References  http://aspalliance.com/337_Review_of_Making_Web_Serv ice_Calls#Page4 http://aspalliance.com/337_Review_of_Making_Web_Serv ice_Calls#Page4  http://msdn.microsoft.com/msdnmag/issues/07/01/Extre meASPNET/default.aspx http://msdn.microsoft.com/msdnmag/issues/07/01/Extre meASPNET/default.aspx  http://www.developer.com/net/asp/article.php/10917_36 57826_4 http://www.developer.com/net/asp/article.php/10917_36 57826_4  http://www.ondotnet.com/pub/a/dotnet/2005/08/01/asyn c_webservices.html http://www.ondotnet.com/pub/a/dotnet/2005/08/01/asyn c_webservices.html  http://msdn.microsoft.com/msdnmag/issues/01/08/Async / http://msdn.microsoft.com/msdnmag/issues/01/08/Async /  http://msdn.microsoft.com/msdnmag/issues/03/06/NET/ http://msdn.microsoft.com/msdnmag/issues/03/06/NET/

29 QUESTIONS???


Download ppt "ASP.Net, web services--- asynchronous and synchronous and AJAX By Thakur Rashmi Singh."

Similar presentations


Ads by Google