Increase Message Size Quota In WCF And Web Service

Hello Everyone!
In this article, we will learn how to increase the size of the message quota in WCF. We are using web services or WCF for security purposes.

Problem: Many times, we have seen this kind of exception: 
"Additional information: The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element".

Reason of this problem: By default, the client machine supports only 65536.

Last month, while working on web services, I needed to transfer a huge amount of data between server to client machines. When I returned to the top  twenty or forty rows, it was working fine. But, when the number of rows exceeded forty, the above exception occured.
Example code
  1. DataTable dt = new DataTable();  
  2.        DataColumn Name = new DataColumn("Name");  
  3.        DataColumn Age = new DataColumn("Age");  
  4.        DataColumn City = new DataColumn("City");  
  6.        [WebMethod]  
  7.        public DataTable ExceptionModule()  
  8.        {  
  9.            dt.Columns.Add(Name);  
  10.            dt.Columns.Add(Age);  
  11.            dt.Columns.Add(City);  
  12.            for (int i = 0; i < 1000; i++)  
  13.            {  
  14.                DataRow row = dt.NewRow();  
  15.                row["Name"] = "Test" + i;  
  16.                row["Age"] = "Age" + i;  
  17.                row["City"] = "Gurgoan" + i;  
  19.            }  
  20.            return dt;  
  21.        }  
When this code publishes on server and consumes this service, one exception occurs.  
  1. System.InvalidOperationException: Cannot serialize the DataTabl. DataTable name is not set.  
Note: when consuming this service, values return in XML format. In XML,  we need root name, but we can't define the root name in the above code. So, I declare the name of table. There is no need to change in the code for removing this error. 
  1. DataTable TempDataTable = new DataTable("Employee");  // we add only employee name in table. 
  2.        DataColumn Name = new DataColumn("Name");  
  3.       DataColumn Age = new DataColumn("Age");  
  4.        DataColumn City = new DataColumn("City");  
  6.        [WebMethod]  
  7.        public DataTable HelloWorld()  
  8.        {  
  9.            TempDataTable.Columns.Add(Name);  
  11.            TempDataTable.Columns.Add(Age);  
  12.            TempDataTable.Columns.Add(City);  
  13.            for (int i = 0; i < 1000; i++)  
  14.            {   DataRow row = TempDataTable.NewRow();  
  15.                row["Name"] = "ravi"+i;  
  16.                row["Age"] = "500"+i;  
  17.               row["City"] = "Gurgaon"+i;  
  18.                TempDataTable.Rows.Add(row);  
  20.            }  
  21.            return TempDataTable;  
  22.        }   
Again, build the service and check again. I hope you will successfully get all the data.

When invoked, the web services look like the above images.

I deploy the web services on the Server and consume in console application. My service deployed successfully but I don't know if some error occurs while consuming it.

Using Consume.ServiceReference1,
Make the object of web services. 
  1. ServiceReference1.HomeSoapClient ExceptionCreate = new HomeSoapClient();   
Call the web service function. 
  1. DataTable dt= ExceptionCreate.HelloWorld();   
Oops!  some exception occurred. 
Reason of exception: Web services supported message of length 65536, by default. If you want to change the web services message length, change in client service.
Change in client app config or web config.
  1. <system.serviceModel>  
  2.      <bindings>  
  3.          <basicHttpBinding>  
  4.              <binding name="HomeSoap" maxReceivedMessageSize="2147483647" />  
  5.          </basicHttpBinding>  
  6.      </bindings>  
  7.      <client>  
  8.          <endpoint address="http://localhost:62596/home.asmx" binding="basicHttpBinding"  
  9.              bindingConfiguration="HomeSoap" contract="ServiceReference1.HomeSoap"  
  10.              name="HomeSoap" />  
  11.      </client>  
  12.  </system.serviceModel>  
I add maxReceiveMessageSize="2147483647".

Consume the web service again. I hope the project runs successfully.

Up Next
    Ebook Download
    View all
    View all