Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

    Advertisements
  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

Posts Tagged ‘upload problem’

WCF Large Data Upload Problem and Resolution

Posted by Paul on April 11, 2012

While dealing with large data using WCF services you might have encountered the following error message.

image

Type: ProtocolException

Message: The remote server returned an unexpected response: (400) Bad Request.

Problem: The message size was exceeded and it resulted in reconstructing the message at server side.  So the incomplete message lead to Protocol Exception.

Solution

The solution is to increase the Received Message Size for the service.  Please do the following.

1. Add the following tags inside the service configuration.

  maxReceivedMessageSize=”2147483647″  maxBufferSize=”2147483647″

2. Build the service

3. Update the client consumer with the new service reference

Example

// Interface

namespace WCF_LargeUpload
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        int Save(DataSet ds);

        [OperationContract]
        DataSet Get();
    }
}

// Service

namespace WCF_LargeUpload
{
    public class Service1 : IService1
    {
        public int Save(System.Data.DataSet ds)
        {
            return ds.Tables[0].Rows.Count;
        }

        public System.Data.DataSet Get()
        {
            DataSet ds = new DataSet();
            ds.Tables.Add(new DataTable() { });
            ds.Tables[0].Columns.Add(new DataColumn() { ColumnName = “Id”, DataType = typeof(int) });
            ds.Tables[0].Columns.Add(new DataColumn() { ColumnName = “Name”, DataType = typeof(string) });

            for (int i = 1; i <= 1000; i++)
                ds.Tables[0].Rows.Add(1, “A lengthy Name for the Item”);

            return ds;
        }
    }
}

// Server Config

<?xml version=”1.0″?>
<configuration>

  <system.web>
    <compilation debug=”true” targetFramework=”4.0″ />
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name=”NewServiceBinding”   maxReceivedMessageSize=”2147483647″  maxBufferSize=”2147483647″>
          <readerQuotas maxDepth=”2147483647″ maxStringContentLength=”2147483647″ maxArrayLength=”2147483647″ maxBytesPerRead=”2147483647″ maxNameTableCharCount=”2147483647″/>
          <security mode=”None”>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration=”NewServiceBehavior” name=”WCF_LargeUpload.Service1″>
        <endpoint  address=”” binding=”basicHttpBinding” bindingConfiguration=”NewServiceBinding”
          contract =”WCF_LargeUpload.IService1″>
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>

        <behavior name=”NewServiceBehavior”>
          <serviceMetadata httpGetEnabled=”true” />
          <dataContractSerializer maxItemsInObjectGraph=”2147483647″ />
          <serviceDebug includeExceptionDetailInFaults=”true” />
          <serviceThrottling
               maxConcurrentCalls=”500″
               maxConcurrentSessions=”500″
               maxConcurrentInstances=”500″
                    />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled=”true” />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests=”true”/>
  </system.webServer>

</configuration>

// Client Console Application

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Service1Client client = new Service1Client();

            DataSet ds = new DataSet();
            ds.Tables.Add(new DataTable() { });
            ds.Tables[0].Columns.Add(new DataColumn() { ColumnName = “Id”, DataType = typeof(int) });
            ds.Tables[0].Columns.Add(new DataColumn() { ColumnName = “Name”, DataType = typeof(string) });

            for (int i = 1; i <= 1000; i++)
                ds.Tables[0].Rows.Add(1, “abcdefghijklmnopqurstuvwxyzabcdefghijklmnopqurstuvwxyzabcdefghijklmnopqurstuvwxyzabcdefghijklmnopqurstuvwxyz”);

            int result = client.Save(ds);

            Console.WriteLine(result);
           
            Console.ReadKey(false);
        }
    }
}

This post confirms my ownership of the site and that this site adheres to Google AdSense program policies and Terms and Conditions: ca-pub-9482913752970808

Advertisements

Posted in C# | Tagged: , , | Leave a Comment »