Thursday, February 25, 2010

Ajax WCF Service using GET /POST Method

The WebGetAttribute attribute is applied to the Add operation to ensure that the service responds to HTTP GET requests. The code uses GET for simplicity (you can construct an HTTP GET request from any Web browser). You can also use GET to enable caching. We can also add the WebInvokeAttribute attribute to explicitly specify HTTP POST, or not specify an attribute, which defaults to HTTP POST.
[ServiceContract(Namespace = "MyAjaxService")]
public interface ICal
{ [OperationContract]
[WebGet]
double Add(double x1, double x2); }

You can create an AJAX endpoint on the service by using the webHTTPBinding standard binding and the enableWebScript behavior in the service configuration file.
The enableWebScript behavior sets the default data format for the service to JSON instead of XML as shown below.
to change the Serializer
[OperationContract]
[WebInvoke(ResponseFormat=WebMessageFormat.Xml)]
//WebMessageFormat.JSon for Json Serializer
Public string HelloWCF()
{ Return “Hello WCF” }

For client follow any one of the technic
Add an ASP.NET AJAX Endpoint with out using Configuration
or
Use configuration to add an AJX Endpoint.

No comments:

Post a Comment