-
Book Overview & Buying
-
Table Of Contents
JavaScript for .NET Developers
By :
To consume the WCF service methods from JavaScript, we need to expose them as the RESTful service methods that accept and return the data in either JSON or XML formats. This helps developers to consume the WCF services as easily as the REST services, and use them with the jQuery $.ajax or $.getJSON (shorthand method of $.ajax) methods. To expose a WCF service as a REST service, we need to annotate the WCF service methods with the WebGet or WebInvoke attributes. The WebGet attribute is mostly used when making any HTTP GET request, whereas WebInvoke is used for all HTTP request methods.
The following code shows the representation of using the WebGet attribute on a WCF operation contract that returns the product based on productCode passed during the method call:
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "json/{productCode}")]
Product GetProduct(string productCode)...