Modifying the ASP.NET AJAX Framework to Handle Web Service Calls - ASP.NET

When you make a web service call from the browser via the ASP.NET AJAX Frame work,it uses XML HTTP to make a call to a server-side web service. Usually all calls to ASMX files are handled by ASP.NET’s ASMX handler. But when you add ASP.NET AJAX to your web application, you need to make some changes in the web. config where you explicitly remove the default ASMX handler and add the ASP.NET AJAX Frame work’s own Script Handler as the ASMX handler (see Example).

ASP.NET AJAX handles all calls to ASMX

You also add a Script Module in the HTTP modules pipeline. It intercepts each and every HTTP request and checks whether the call is to an ASPX page and is calling a page method. It intercepts only page method calls,not web service calls.So, you don’t need to bypass it.

Script Handler is a regular HTTP handler that finds out which web service and web method is called by parsing the URL.It then executes the web method by reflecting on the web service type.The steps involved in calling a web method are as follows:

  1. Confirm it’s an Ajax web method call by checking Content-Type to see whether it has application/json.If not, raise an exception.
  2. Find out which .asmx is called by parsing a requested URL and getting the assembly, which has the compiled code for the. asmx.
  3. Reflect on the assembly and find the web service class and method that represents the web method being called.
  4. Deserialize input parameters into the proper data type.In case of HTTP POST,deserialize the JSON graph.
  5. See the parameters in the method and map each parameter to objects that have been deserialized from JSON.
  6. Initialize the cache policy.
  7. Invoke the method via reflection and pass the parameter values that match from JSON.
  8. Get the return value.Serialize the return value into JSON/XML.
  9. Emit the JSON/XML as the response.

To add asynchronous web method call support, you need to first change the way it reflects on the assembly and locates the web method. It needs to call the Begin and End pair, instead of the real web method.You also need to make the handler implement the IHttp Async Handler interface and execute the Begin and End pair in Begin Process Request and End Process Request.But there’s no step that facilitates .NET 2.0 transactions.The only way to implement them is to use System.Enter prise Services transactions or use your own .NET 2.0 Transaction Scope class inside your web method code..NET 2.0 introduced the new System.Transaction name space, which has a much better way to handle transactions. It would be great if you could add a [Transaction] attribute in your web methods so they could work within a transaction managed by the Script Handler.But Script Handler does not deal with .NET 2.0 transactions.

Initializing the Cache Policy

In the ASP.NET AJAX Framework,initialization of cache policy comes before invoking the web method.Example shows the ASP.NET AJAX 1.0 code for the Initialize Cache Policy function that sets the cache policy before invoking the web method.

Script Handler’s Initialize Cache Policy function initializes the cache settings before the web method is called

ScriptHandler’s InitializeCachePolicy function initializes the cache settings before the web method is called (continued)

If you do not have cache duration set in the [Web Method] attribute, it will set the Max Age to zero.Once Max Age is set to zero, it can no longer be increased; there fore, you can not increase Max Age from your web method code dynamically and thus make the browser cache the response.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

ASP.NET Topics