Posts Tagged ‘WCF service’

Consuming WCF service from Jscript using JQuery

I recently got a requirement to host a WCF service in Azure and consume it client side using Jscript.

In this article, I am providing details on how to consume WCF service from Jscript with the help of JQuery.

In below example, service is a simple “Products.svc” contain 1 method “GetProducts()” and returns List<Product> (“Product” is custom class)

WCF Service

  • Service Contract (IProductService.cs)

[ServiceContract(Namespace = “http://ABCproducts&#8221;,

Name = “IProductService”,

SessionMode = SessionMode.NotAllowed,

ProtectionLevel = ProtectionLevel.None)]

public interface IProductService{



Method = “POST”,

BodyStyle = WebMessageBodyStyle.Wrapped,

RequestFormat = WebMessageFormat.Json,

ResponseFormat = WebMessageFormat.Json)]

List<Product> GetProducts();



public class Product{

string id = string.Empty;

string name = string.Empty;


public string ProductId{

get { return id; }

set { id = value; }



public string ProductName{

get { return name; }

set { name = value; }



  • Service (ProductService.svc)

public List<Product> GetProducts(){

List<Product> products = new List<Product>();

Product P1 = new Product();

P1.Id = “1”;

P1.Name = “Mango”;

Product P2 = new Product();

P2.Id = “2”;

P2.Name = “Apple”;



return products;


  • Service Binding (Web.config)
    • Very important note is, the service endpoint has to be “webHttpBinding”; otherwise we get “Cannot process the message because the content type application\json was not the expected…” error.
    • webHttpBinding is the REST-style binding, where you can hit a service URL and get back a result in either XML or JSON from the service.
    webHttp Binding

    webHttp Binding

    • Add a “EndPointBehavior” with “webHttp” stack element.
webHttp Behavior

webHttp Behavior

    • Below is the service configuration mentioned in my web.config file


<service name=”ProductService”>

<endpoint address=”” behaviorConfiguration=”endPtBehaviorJSon

binding=”webHttpBinding” bindingConfiguration=”bindingWebHttp”

name=”wsBindingBFS” contract=”IProductService” />





<behavior name=”endPtBehaviorJSon“>

<webHttp />





  • Since we are using JQuery, refer “JQuery.1.4.1.min.js” file
  • In this example we are getting output in json format. (Observe “dataType property mentioned as “json”)
  • getProducts” is the function which communicate to service and gets “Product” collection as response in JSon format.
  • The response comes in special format (i.e., WCF Service method name+Result)  (i.e, GetProductsResult)

function getProducts() {

var serviceURL = “http://{Server Name}/ProductService.svc/GetProducts“;


type: “POST”,

contentType: “application/json; charset=utf-8”,

url: serviceURL,

processData: false,

dataType: “json”,

//If the call succeeds


function (response) {



//If the call fails

error: function (XMLHttpRequest, textStatus, errorThrown) {

alert(“Error while retrieval – ” + XMLHttpRequest.responseText);




function retrieveProducts(response) {

// Result collection come as Response.{MethodName}+”Result”

if (response && response.GetProductsResult) {

$.each(response.GetProductsResult, function (i) {


“Product ID: ” + this.ProductId +

” Product Name: ” + this.ProductName);




The maximum array length quota has been exceeded – Error in WCF service

June 9, 2012 1 comment


I was getting below exception when I call a WCF service from my plug – in

There was an error deserializing the object of type XYZ. The maximum array length quota (16384) has been exceeded while reading XML data

Below was my requirement,

  • I have to send emails with attachments from my plugin
  • So I built a custom WCF Email service
  • From the plug-in we are calling service method by passing object

I have no clue about the error, because I already given maximum reader quote values in my service configuration (Refer below)

Service Configuration:- 



<binding name=”basicHttp_EmailService” openTimeout=”00:30:00″

receiveTimeout=”00:30:00″ sendTimeout=”00:30:00″ maxBufferSize=”2147483647″

maxBufferPoolSize=”2147483647″ maxReceivedMessageSize=”2147483647″>


maxDepth=”2147483647” maxStringContentLength=”2147483647

maxArrayLength=”2147483647” maxBytesPerRead=”2147483647




<service name=”EmailService“>

After spending couple of hard hours I found the reason & fix for the issue


  • My service is simply ignoring my binding “basicHttp_EmailService” and it’s always taking default binding
  • Reason is
    • My configuration service name does not match with service name in Service Host
    • The name I given is “EmailService” where it has to be “XYZ. EmailService”

Fix :-

  • Right click your “.svc” file and choose “View Markup”
  • Copy the Service name and paste it in your service name
WCF Service Host

WCF Service Host

  • Now my Service configuration looks below

<service name=”XYZ.EmailService“>