Software School Projects | Academic Students Projects | Source Codes | Tablets header
Please use our contact us form or send email to Support@srishtis.com.


Understanding ASP.NET AJAX Web Services.
Web Services are an integral part of the .NET framework that provide a cross-platform solution for exchanging data between distributed systems. Although Web Services are normally used to allow different operating systems, object models and programming languages to send and receive data, they can also be used to dynamically inject data into an ASP.NET AJAX page or send data from a page to a back-end system. All of this can be done without resorting to postback operations.
Calling Web Services with ASP.NET AJAX
Web Services are an integral part of the .NET framework that provide a cross-platform solution for exchanging data between distributed systems. Although Web Services are normally used to allow different operating systems, object models and programming languages to send and receive data, they can also be used to dynamically inject data into an ASP.NET AJAX page or send data from a page to a back-end system. All of this can be done without resorting to postback operations.While the ASP.NET AJAX UpdatePanel control provides a simple way to AJAX enable any ASP.NET page, there may be times when you need to dynamically access data on the server without using an UpdatePanel. In this article you'll see how to accomplish this by creating and consuming Web Services within ASP.NET AJAX pages. This article will focus on functionality available in the core ASP.NET AJAX Extensions as well as a Web Service enabled control in the ASP.NET AJAX Toolkit called the AutoCompleteExtender. Topics covered include defining AJAX-enabled Web Services, creating client proxies and calling Web Services with JavaScript. You'll also see how Web Service calls can be made directly to ASP.NET page methods.
Web Services Configuration
When a new Web Site project is created with Visual Studio 2008, the web.config file has a number of new additions that may be unfamiliar to users of previous versions of Visual Studio. Some of these modifications map the "asp" prefix to ASP.NET AJAX controls so they can be used in pages while others define required HttpHandlers and HttpModules. Listing 1 shows modifications made to the <httpHandlers> element in web.config that affects Web Service calls. The default HttpHandler used to process .asmx calls is removed and replaced with a ScriptHandlerFactory class located in the System.Web.Extensions.dll assembly. System.Web.Extensions.dll contains all of the core functionality used by ASP.NET AJAX.
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false"           type="System.Web.Script.Services.ScriptHandlerFactory,           System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,           PublicKeyToken=31bf3856ad364e35">
<authenticationService enabled="true" />
</httpHandlers>
This HttpHandler replacement is made in order to allow JavaScript Object Notation (JSON) calls to be made from ASP.NET AJAX pages to .NET Web Services using a JavaScript Web Service proxy. ASP.NET AJAX sends JSON messages to Web Services as opposed to the standard Simple Object Access Protocol (SOAP) calls typically associated with Web Services. This results in smaller request and response messages overall. It also allows for more efficient client-side processing of data since the ASP.NET AJAX JavaScript library is optimized to work with JSON objects. Listing 2 and Listing 3 show examples of Web Service request and response messages serialized to JSON format. The request message shown in Listing 2 passes a country parameter with a value of "Belgium" while the response message in Listing 3 passes an array of Customer objects and their associated properties
Listing 2. Web Service Request Message Serialized to JSON
{"country":"Belgium"}
Note: the operation name is defined as part of the URL to the web service; additionally, request messages are not always submitted via JSON. Web Services can utilize the ScriptMethod attribute with the UseHttpGet parameter set to true, which causes parameters to be passed via a the query string parameters.
Listing 3. Web Service Response Message Serialized to JSON
[{"__type":"Model.Customer","Country":"Belgium","CompanyName":"Maison      Dewey","CustomerID":"MAISD","ContactName":"Catherine      Dewey"},{"__type":"Model.Customer","Country":"Belgium","CompanyName":"Suprêmes      délices","CustomerID":"SUPRD","ContactName":"Pascale      Cartrain"}]
In the next section you'll see how to create Web Services capable of handling JSON request messages and responding with both simple and complex types.
Creating AJAX-Enabled Web Services
The ASP.NET AJAX framework provides several different ways to call Web Services. You can use the AutoCompleteExtender control (available in the ASP.NET AJAX Toolkit) or JavaScript. However, before calling a service you have to AJAX-enable it so that it can be called by client-script code. Whether or not you're new to ASP.NET Web Services, you'll find it straightforward to create and AJAX-enable services. The .NET framework has supported the creation of ASP.NET Web Services since its initial release in 2002 and the ASP.NET AJAX Extensions provide additional AJAX functionality that builds upon the .NET framework's default set of features. Visual Studio .NET 2008 Beta 2 has built-in support for creating .asmx Web Service files and automatically derives associated code beside classes from the System.Web.Services.WebService class. As you add methods into the class you must apply the WebMethod attribute in order for them to be called by Web Service consumers.
Listing 4 shows an example of applying the WebMethod attribute to a method named GetCustomersByCountry().
Listing 4. Using the WebMethod Attribute in a Web Service
[WebMethod] public Customer[] GetCustomersByCountry(string country)
{
      return Biz.BAL.GetCustomersByCountry(country);
}
The GetCustomersByCountry() method accepts a country parameter and returns a Customer object array. The country value passed into the method is forwarded to a business layer class which in turn calls a data layer class to retrieve the data from the database, fill the Customer object properties with data and return the array.
Using the ScriptService Attribute
While adding the WebMethod attribute allows the GetCustomersByCountry() method to be called by clients that send standard SOAP messages to the Web Service, it doesn't allow JSON calls to be made from ASP.NET AJAX applications out of the box. To allow JSON calls to be made you have to apply the ASP.NET AJAX Extension's ScriptService attribute to the Web Service class. This enables a Web Service to send response messages formatted using JSON and allows client-side script to call a service by sending JSON messages.
Listing 5 shows an example of applying the ScriptService attribute to a Web Service class named CustomersService.
Listing 5. Using the ScriptService Attribute to AJAX-enable a Web Service.
[System.Web.Script.Services.ScriptService]
[WebService(Namespace = "http://xmlforasp.net")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class CustomersService : System.Web.Services.WebService
{
      [WebMethod]  
     public Customer[] GetCustomersByCountry(string country)
      {  
          return Biz.BAL.GetCustomersByCountry(country);
     }
}
The ScriptService attribute acts as a marker that indicates it can be called from AJAX script code. It doesn't actually handle any of the JSON serialization or deserialization tasks that occur behind the scenes. The ScriptHandlerFactory (configured in web.config) and other related classes do the bulk of JSON processing.
Using the ScriptMethod Attribute
The ScriptService attribute is the only ASP.NET AJAX attribute that has to be defined in a .NET Web Service in order for it to be used by ASP.NET AJAX pages. However, another attribute named ScriptMethod can also be applied directly to Web Methods in a service. ScriptMethod defines three properties including UseHttpGet, ResponseFormat and XmlSerializeString. Changing the values of these properties can be useful in cases where the type of request accepted by a Web Method needs to be changed to GET, when a Web Method needs to return raw XML data in the form of an XmlDocument or XmlElement object or when data returned from a service should always be serialized as XML instead of JSON. The UseHttpGet property can be used when a Web Method should accept GET requests as opposed to POST requests. Requests are sent using a URL with Web Method input parameters converted to QueryString parameters. The UseHttpGet property defaults to false and should only be set to true when operations are known to be safe and when sensitive data is not passed to a Web Service. Listing 6 shows an example of using the ScriptMethod attribute with the UseHttpGet property.
Listing 6.Using the ScriptMethod attribute with the UseHttpGet property.
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public XmlElement GetRssFeed(string url)
{
      XmlDocument doc = new XmlDocument();
      doc.Load(url);
      return doc.DocumentElement;
}
The ResponseFormat property can also be used along with the XmlSerializeString property. The XmlSerializeString property has a default value of false which means that all return types except strings returned from a Web Method are serialized as XML when the ResponseFormat property is set to ResponseFormat.Xml. When XmlSerializeString is set to true, all types returned from a Web Method are serialized as XML including string types. If the ResponseFormat property has a value of ResponseFormat.Json the XmlSerializeString property is ignored.
Listing 8 shows an example of using the XmlSerializeString property to force strings to be serialized as XML.
Listing 8. Using the ScriptMethod attribute with the XmlSerializeString property
Code sample: Loading profile data at page load
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml,XmlSerializeString=true)]
public string GetXmlString(string input)
{  
     return input;
}
The value returned from calling the GetXmlString Web Method shown in Listing 8 is shown next:
<?xml version="1.0"?>
<string>Test</string>
Although the default JSON format minimizes the overall size of request and response messages and is more readily consumed by ASP.NET AJAX clients in a cross-browser manner, the ResponseFormat and XmlSerializeString properties can be utilized when client applications such as Internet Explorer 5 or higher expect XML data to be returned from a Web Method.
Job or extra money for students

Search Engine Rank of your blog or websites