This is a mirror of official site: http://jasper-net.blogspot.com/

Steps to Call WCF Service using jQuery

| Thursday, December 2, 2010
Introduction

In this post is related to No. of step one should follow to call WCF Service in your client code and No. of thing require to take care when you call WCF service.
Before start reading and following this read about how to create WCF service. Create, Host(Self Hosting, IIS hosting) and Consume WCF servcie

Step 1
Once you done with creation of WCF service you need to specify the attribute on server type class for ASP.NET  Compatibility mode. so that WCF service works as normal ASMX services and support all  ASP.NET existing feature. By setting compatibility mode WCF service require to host on IIS and get communicate with client application using HTTP protocol. More about this in detail : WCF Web HTTP Programming Object Model
Following line of code set ASP.NET Compatibility mode

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service : IService
{
   //  .....your code
}

Service.Cs

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service : IService
{
   public string GetData(int value)
   {
       return string.Format("You entered: {0}", value);
   }


   public string[] GetUser(string Id)
   { return new User().GetUser(Convert.ToInt32(Id)); }


Step 2
Need to specify attribute at operation level in Service contract file for each method/operation. So to do this its require decorate method with WebInvoke which mark a service operation as one that responds to HTTP requests other than GET. So your operational level code in contract file is

[OperationContract]
   [OperationContract]
   [WebInvoke(Method = "POST",  BodyStyle = WebMessageBodyStyle.Wrapped,  ResponseFormat = WebMessageFormat.Json)]
   string[] GetUser(string Id);

as you can see in code here sub-attribute having value to support call via jQuery i.e Method=post so data get posted to service via post method. ResponseFormat = WebMessaeFormat Json indicate data return as json format.

IService.cs

[ServiceContract]
public interface IService
{
   [OperationContract]
   [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
   string GetData(int value);


Read more: Codeproject

Posted via email from .NET Info

0 comments: