By default,all web methods declared on a web service are synchronous on the server side.However,the call from the browser via XML HTTP is asynchronous, but the actual execution of the web method at the server is synchronous.
This means that from the moment a request comes in to the moment the response is generated from that web method call, it occupies a thread from the ASP.NET worker pool. If it takes a relatively long period of time for a request to complete, then the thread that is processing the request will be in use until the method call is done. Unfortunately, most lengthy calls are due to something like a long data base query or perhaps a call to another web service. For instance, if you make a database call, the current thread waits for the database call to complete.
The thread simply has to wait around doing nothing until it hears back from its query.Similar issues arise when a thread waits for a call to a TCP socket or a backend web service to complete.
When you write a typical ASP.NET web service using web methods, the compiler compiles your code to create the assembly that will be called when requests for its web methods are received.When your application is first launched, the ASMX handler reflects over the assembly to determine which web methods are exposed.
For normal synchronous requests, it is simply a matter of finding which methods have a [Web Method] attribute associated with them.
To make asynchronous web methods, you need to ensure the following rules are met:
If the ASMX handler finds two methods that meet all these requirements, then it will expose the XXX method in its WSDL as if it were a normal web method.
Example shows a typical synchronous web method and Example shows how it is made asynchronous by introducing a Begin and End pair.
Example of a synchronous web method
The ASMX handler will expose a web method named Sleep from the pair of web methods.The method will accept the parameters defined before the AsyncCallback parameter in the signature for BeginXXX as input and return with the EndXXX function.
After the ASMX handler reflects on the compiled assembly and detects an asynchronous web method, it must handle requests for that method differently than it handles synchronous requests. Instead of calling the Sleep method synchronously andproducing responses from the return value,it calls the BeginSleep method. It deserializes the incoming request into the parameters to be passed to the function—as it does for synchronous requests—but it also passes the pointer to an internal callback function as the extra AsyncCallback parameter to the BeginSleep method.
After the ASMX handler calls the BeginSleep function, it will return the thread to the process thread pool so it can handle another request.The HttpContext for the request will not be released yet.The ASMX handler will wait until the callback function that it passed to the BeginSleep function is called to finish processing the request.
Once the callback function is called, a thread from the thread pool is taken out to execute the remaining work.The ASMX handler will call the EndSleep function so that it can complete any processing it needs to perform and return the data to be rendered as a response.Once the response is sent, the HttpContext is released (see Figure).
How the asynchronous web method works
The asynchronous web method concept is hard to grasp.It does not match with anything that we do in regular development. There are some fundamental differences and limitations to consider:
So,in Example,neither the simple sleep function nor any of the methods that we have used in our proxy web service can be real asynchronous functions(see Chapter).We need to rewrite them to support the asynchronous call nature. Before we do so,remember one principle—you can only benefit from the asynchronous method when the BeginXXX web method ends up calling a BeginYYY method on some other component, and your EndXXX method calls that component’s EndYYY method.Otherwise, there’s no benefit in making web methods asynchronous
Example shows the code for a simple stock quote proxy web service.The proxy web service’s Begin Get Stock method ends up calling the Begin GetS tock method on a component that fetches the stock data from external source. When data arrives,the component calls back via the Async Call back cb.The ASMX handler passes down this call back to the web method.So,when it is called, ASP NET’s ASMX handler receives the call back, and it restores the Http Context,calls End Get Stock, and renders the response.
Example of a stock quote proxy web service
Example of a stock quote proxy web service (continued)
The problem is ASP.NET’s ASMX handler has the capability to call asynchronous web methods and return threads to the ASP.NET thread pool, but ASP.NET AJAX Framework’s ASMX handler does not have that capability. It supports only synchronous calls.So, we need to rewrite the ASMX handler of ASP.NET AJAX to support asynchronous web method execution and then bypass ASP.NET AJAX’s ASMX handler when web methods are called via XML HTTP.In the next section, you will seem how the ASP.NET AJAX Frame work’s ASMX handler works and how you can rewrite such a handler your self and introduce new features to it.
ASP.NET Related Interview Questions
|VB.NET Interview Questions||C#. NET Interview Questions|
|ASP.NET Interview Questions||ADO.Net Interview Questions|
|Windows Presentation Foundation(WPF) Interview Questions||Windows CE .NET Interview Questions|
|Dot Net Framework Interview Questions||Asp Dot Net Mvc 4 Interview Questions|
|Asp Dot Net Mvc Interview Questions|
Introducing Web Portals And Dropthings.com
Architecting The Web Portal And Widgets
Building The Web Layer Using Asp.net Ajax
Building The Data And Business Layers Using .net 3.5
Building Client-side Widgets
Optimizing Asp.net Ajax
Creating Asynchronous, Transactional, Cache-friendly Web Services
Improving Server-side Performance And Scalability
Improving Client-side Performance
Solving Common Deployment, Hosting, And Production Challenges
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.