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

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

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ASP.Net, web services--- asynchronous and synchronous and AJAX By Thakur Rashmi Singh.

ASP.Net, web services---asynchronous and synchronous

and AJAX

ByThakur Rashmi Singh

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

Presentation Flow

What is a webservice Making a web service Call What is synchronous call What is asynchronous call What is Ajax. Conclusion.

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

Web Services?

“The W3C defines a Web service as a software system designed to support interoperable Machine to Machine interaction over a network”

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”.

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

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.

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

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.

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

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.

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

Ways of calling a Web Service

There are 2 ways of calling a web serviceCalling it synchronouslyCalling it Asynchronously.

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

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:

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

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!!!

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

Solution???

The best way to make Web service calls is to make asynchronous Web service calls !

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

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.

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

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?

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

Solution…

Three ways to determine if the thread has finished its work or not.

Notification Polling Wait Handles

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

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.

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

Polling

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

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

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.

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

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.

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

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.

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

AJAX

AJAX has many acronyms. The most common one is Asynchronous Java and XML.

It is a web techinque of making Asynchronous web requests.

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

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

<asp:ScriptManager ID="_scriptManager" runat="server"> <Services>

<asp:ServiceReference Path="StockQuoteService.asmx" /> </Services>

</asp:ScriptManager>

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

You can now write your Javascript and call your webservicefunction 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 completedOnError – is a function which is called during exceptions.

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

Proxy is created in the client side by injecting a <script> tag.

<script src="StockQuoteService.asmx/js" type="text/javascript"></script>

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.

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

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

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

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…

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

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

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

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.

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

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.

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

References

http://aspalliance.com/337_Review_of_Making_Web_Service_Calls#Page4

http://msdn.microsoft.com/msdnmag/issues/07/01/ExtremeASPNET/default.aspx

http://www.developer.com/net/asp/article.php/10917_3657826_4

http://www.ondotnet.com/pub/a/dotnet/2005/08/01/async_webservices.html

http://msdn.microsoft.com/msdnmag/issues/01/08/Async/ http://msdn.microsoft.com/msdnmag/issues/03/06/NET/

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

QUESTIONS???