The maximum string content length quota (8192) has been exceeded while reading XML data. This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader.

Recently i got error on my WCF service. which is something like “The maximum string content length quota (8192) has been exceeded while reading XML data. This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader.”

The error says clearly that it has not able to read more than 8192 string length. WCF service by default set 8192 string length for security to prevent not submit more data. But, in my case i need more data to send. so, i have set following things in my bindings and all done.




maxStringContentLength is set length and solve the problem.

Hope it will help you other also..

error 413 Request Entity Too Large in wcf request

Hello All,

Today i am facing one error in my WCF request. The error is “413 Request Entity Too Large”. My page request is larger than normal size and i encounter this error.

To overcome from above error use following code it is work for me.



  
	
	
  


IIS has not any problem but WCF has this error. It works in earlier version of Visual Studio but this error shows in Visual Studio 2010. WCF limits message to 65KB. It will set this limit to provide security from unknown attacks. Set maxReceivedMessageSize will solve your problem.

Hope it will help you too!!!

Create Simple WCF Service

Hello All,

Today, I want to tell you how to Create Simple WCF Service.

To create WCF service right click on project and add new item -> WCF service. It will create .svc file. Remember to declare Factory tag in .svc file. When you creating service if your web.config is read only it will ask to make it writable. It will not write anything in web.config.

Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"

Now, go to your .cs file. It has created two files one for sampleClassFunction and another is interface file. We can code in both way write in sampleClassFunction or in interface. I prefer to write in class direct. So, i have remove interface from my class. In cs class file add following code

[AspNetCompatibilityRequirements
    (RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceContract(Namespace = "")]
public class sampleClassFunctions
{
    [OperationContract]
    public string GetFirstString(string sample)
    {
        return sample;
    }
}

Now, declare ScriptManager in .aspx.

    
        
            
        
    

To call this service from your code use following code(I am calling it from my js).

function sample()
{
    function getFirstStringCallback(data)
    {
         alert(data);
    }

    sampleClassFunctions.GetFirstString("My First WCF Service", getFirstStringCallback);
}

That’s it. Enjoy your WCF service….

Call cross-domain service from js

Hello All,

Today, i want to share how to call cross-domain service from js.

We may call service from js with ajax call. Following function will return json data from service. Make sure your service also return json data too.

$.getData = function(url, options) {
            $.ajax({
                url: url,
                type: "POST",
                data: JSON.stringify(options),
                dataType: "json",
                async: false,
                success: function(msg) {
                    returnData(options.method, jQuery.parseJSON(msg.result));
                }
            });
        };

Now, to call service write as follow

function getDataFromService() {
        $.getData("service_url/WebServices.ashx", { method: 'method_name', params: { /* if any */ }
        });
    };

To use parameter in your service write as “param_name1″ : param_value, “param_name2″ : param_value…

That’s it. enjoy your service call. Your browser will block cross-domain service so, make sure you will remove that option from browser settings.

Generate service log for WCF service from web.config

Sometime our service not returning correct error. So, find exact problem of our code we can use diagnostics to generate service log. With help of service log we can see what is the problem in service.

To generate service log add code to web.config as follows.


    
      
    


    
    
      
    
    
      
        
          
        
      
      
        
          
        
      
    
  

with above change you can generate service log. Write your_path where you want to generate log file (e.g. c:\servicetrace.svclog).
To open service log find SvcTraceViewer.exe in Program Files\Microsoft SDKs\Windows\v7.0A\Bin (as per your windows version).

Return json string from WCF service

Today, i have one requirement to return json string from my wcf service. So, i search many things and i found the simple things which i want to share with you.

Write WebInvoke part in your function of WCF service.

[OperationContract]
[WebInvoke(Method = "GET",
    BodyStyle = WebMessageBodyStyle.WrappedRequest,
    ResponseFormat = WebMessageFormat.Json
    )]
public void GetDisplayList()
{
    StringBuilder jsonData = new StringBuilder();
    
    // Your code goes here......

    jsonData = StringValidate(jsonData);

    HttpContext.Current.Response.ContentType = "application/json; charset=utf-8";
    HttpContext.Current.Response.Write(jsonData.ToString());
    HttpContext.Current.Response.End();
}

To generate correct json add one more function.

    public string StringValidate(string value)
    {
        value = value.Replace("\r", "");
        value = value.Replace("\n", "");
        value = value.Replace("\"", "");
        value = value.Replace("'", "");
        value = value.Replace("\t", "");
        value = value.Replace("\b", "");
        value = value.Replace("\f", "");
        value = value.Replace("\\", "");
        value = value.Replace(":", "");
        //value = value.Replace("\\/", "");
        value = value.Replace("\\u", "");

        return value;
    }

I am calling my Service from javascript to bind grid.

function populateGrid()
{
    $('#grid').setGridParam({ url: 'services/yoursericename.svc/GetDisplayList'});
}

That’s all you will get result from your wcf service.

Difference between Web service and WCF

WCF offers much more flexibility and portability to develop a service when comparing to web service. See the following points that represent the difference between WCF and Web Service.

Web Service

WCF

[WebService] attribute has to be added to the class [ServiceContraact] attribute has to be added to the class
[WebMethod] attribute represents the method exposed to client [OperationContract] attribute represents the method exposed to client
hosted in IIS hosted in IIS, WAS(windows activation service,) Self-hosting, Windows service
System.Xml.serialization name space is used for serialization System.Runtime.Serialization namespace is used for serialization
accessed through HTTP accessed through HTTP, TCP, MSMQ,P2P, Named pipes
One-way, Request- Response are the different operations supported in web service One-Way, Request-Response, Duplex are different type of operations supported in WCF
Security Security, Reliable messaging, Transactions

Communication between javascript and .Net CLR

Hi All,

I am presenting article on communication between javascript and .Net CLR. There are number of methods for the same. In this article, we will see how to communicate javascript and .Net CLR using webservice.

I have used javascript object for communication with server side method because this is a good practice to use javascript object instead of list of argument. And you can also see how javascript object will be used in communication with server side method.

Below code representing that how can we pass the information from client side to server side?

function Employee(firstName, lastName, age)
{
   Employee.prototype.firstName = firstName;
   Employee.prototype.lastName = lastName;
   Employee.prototype.age = age;
}

function insertEmployeeClick()
{
   var firstName = document.getElementById("txtFirstName").value;
   var lastName = document.getElementById("txtLastName").value;
   var age = document.getElementById("txtAge").value;

   if (firstName == "") { alert("First Name    Required");document.getElementById("txtFirstName").focus();return;}
   if (lastName == "") { alert("Last Name Required"); document.getElementById("txtLastName").focus(); return; }
   if (age == "") { alert("Age Required"); document.getElementById("txtAge").focus(); return; }

   var employee = new Employee(firstName, lastName, age);
   JSObject.InsertEmployee(employee, onInsertCallBack);
}

function onInsertCallBack(result)
{
   document.getElementById("txtFirstName").value = "";
   document.getElementById("txtLastName").value = "";
   document.getElementById("txtAge").value = "";

   if (result) { alert("Employee added successfully"); }
   else { alert("Database insert fail"); }
}

I have also attached the sample code. You can download it from here.
JSObjectAndWebService