1

I pass some values to my WCF service from the .net application in string format. Passing string format will be in this structure,

ItemName~ItemDescription~ItemPrice|ItemName~ItemDescription~ItemPrice|...

Every line item will be separated by '|' character. I was passing nearly 1000 items. It was working as expected, but when I came to pass 1500 items, this error occurs.

The remote server returned an unexpected response: (413) Request Entity Too Large.

Please help me in fixing this error.

This is the method in service

private void InsertGpLineItems(string lineItems)
{
    //Here I will process the insertion of line items to the GP system.
}

This is web.config at my WCF service.

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="connectionString" value="data source=localhost; initial catalog=TWO; integrated security=SSPI"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <pages validateRequest="false" />
    <httpRuntime requestValidationMode="2.0" />
  </system.web>
  <system.diagnostics>    
    <sources>
      <source name="System.ServiceModel.MessageLogging"
              switchValue="Information, ActivityTracing, Error">
        <listeners>
          <add name="messages"
               type="System.Diagnostics.XmlWriterTraceListener"
               initializeData="messages.svclog" />
        </listeners>
      </source>
    </sources> 
  </system.diagnostics>
  <system.serviceModel>
    <diagnostics>
      <messageLogging logEntireMessage="true"
                      logMalformedMessages="true"
                      logMessagesAtServiceLevel="true"
                      logMessagesAtTransportLevel="true"
                      maxMessagesToLog="3000"
                      maxSizeOfMessageToLog="2000"/>
    </diagnostics>
    <services>
      <service name="Service1.IService1">
        <endpoint address="" binding="basicHttpBinding"
                  contract="Service1.IService1">
        </endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:50935/Service1.svc"/>
          </baseAddresses>
        </host>
        <!--<endpoint address="http://localhost:50935/Service1.svc" binding="basicHttpBinding"></endpoint>-->
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="SampleBinding" 
                 messageEncoding="Text"
                 closeTimeout="00:02:00"
                 openTimeout="00:02:00"
                 receiveTimeout="00:20:00"
                 sendTimeout="00:02:00"
                 allowCookies="false"
                 bypassProxyOnLocal="false"
                 hostNameComparisonMode="StrongWildcard"
                 maxBufferPoolSize="2147483647" 
                 maxBufferSize="2147483647"
                 maxReceivedMessageSize="2147483647"
                 textEncoding="utf-8"
                 transferMode="Buffered"
                 useDefaultWebProxy="true">          
          <readerQuotas maxDepth="2000000"
                        maxStringContentLength="2147483647"
                        maxArrayLength="2147483647"
                        maxBytesPerRead="2147483647"
                        maxNameTableCharCount="2147483647"  />
          <security mode="Transport">
            <transport clientCredentialType="None" 
                       proxyCredentialType="None" 
                       realm="">
            </transport>
          </security>
        </binding>        
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="behaviorGPLineItemsService">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>
Tim
  • 28,212
  • 8
  • 63
  • 76
good-to-know
  • 742
  • 3
  • 15
  • 32

3 Answers3

2

Try this,

<bindings>
  <basicHttpBinding>
    <binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
      <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    </binding>
  </basicHttpBinding>
</bindings>

Instead of these lines in your code,

<bindings>
  <basicHttpBinding>
    <binding name="SampleBinding" 
             messageEncoding="Text"
             closeTimeout="00:02:00"
             openTimeout="00:02:00"
             receiveTimeout="00:20:00"
             sendTimeout="00:02:00"
             allowCookies="false"
             bypassProxyOnLocal="false"
             hostNameComparisonMode="StrongWildcard"
             maxBufferPoolSize="2147483647" 
             maxBufferSize="2147483647"
             maxReceivedMessageSize="2147483647"
             textEncoding="utf-8"
             transferMode="Buffered"
             useDefaultWebProxy="true">          
      <readerQuotas maxDepth="2000000"
                    maxStringContentLength="2147483647"
                    maxArrayLength="2147483647"
                    maxBytesPerRead="2147483647"
                    maxNameTableCharCount="2147483647"  />
      <security mode="Transport">
        <transport clientCredentialType="None" 
                   proxyCredentialType="None" 
                   realm="">
        </transport>
      </security>
    </binding>        
  </basicHttpBinding>
</bindings>

I hope this helps you. :)

  • I'll try this. Thanks. – good-to-know Jun 18 '15 at 04:27
  • 4
    Why does this answer help the OP? I.e., what's different between the binding configuration you posted and the one OP had, and why does the difference matter? (Note: I know why - but this is, IMO, a poor answer because it doesn't *explain* anything). – Tim Jun 18 '15 at 07:34
  • Perfect for what I need this morning. – Xavier J Sep 23 '16 at 18:17
1

One reason it may not be working is that you've defined binding is not being used by your endpoint because you don't specify it via the bindingConfiguration attribute in the endpoint element. This results in WCF using the default values for basicHttpBinding (which are lower), rather than your values.

Try this:

<service name="Service1.IService1">
  <endpoint address="" 
            binding="basicHttpBinding"
            bindingConfiguration="SampleBinding"
            contract="Service1.IService1">
  </endpoint>
  <host>
    <baseAddresses>
      <add baseAddress="http://localhost:50935/Service1.svc"/>
    </baseAddresses>
  </host>
  <!--<endpoint address="http://localhost:50935/Service1.svc" binding="basicHttpBinding"></endpoint>-->
</service>

Note the use of the bindingConfiguration attribute in the sample above.

Also note that if you're hosting the service in IIS, you don't need the baseAddress element, as the base address will be the location of the .svc file.

ADDED

The same principle applies to your endpoint behavior - its not being assigned to the endpoint either - you need to use the behaviorConfiguration attribute for this:

<service name="Service1.IService1">
  <endpoint address=""
            behaviorConfiguration="behaviorGPLineItemsService"
            binding="basicHttpBinding"
            bindingConfiguration="SampleBinding"
            contract="Service1.IService" />

The service behavior configuration section doesn't have a name attribute specified, so it is treated as the default service behavior and is applied to all services (in that config file) that do not explicitly set a behaviorConfiguration name.

The blank name = default applies to binding configurations and endpoint behavior configurations as well, IIRC.

Tim
  • 28,212
  • 8
  • 63
  • 76
0

This is because your service is of type HTTP GET which is limited length.

If your data sent is really large, then you must use HTTP POST instead.

[WebInvoke(Method = "POST")]
private void InsertGpLineItems(string lineItems)

Also, you need to edit you web.config file, as you can find here.

Community
  • 1
  • 1
  • Even after I put this on my Web Service method, I am getting the same error. – good-to-know Jun 17 '15 at 11:52
  • check this http://stackoverflow.com/questions/20575946/the-remote-server-returned-an-unexpected-response-413-request-entity-too-larg and this http://stackoverflow.com/questions/16120758/remote-server-returned-an-unexpected-response-413-requested-entity-too-large –  Jun 17 '15 at 11:55
  • Same error again after editing web.config? If yes, please show the code that invokes your web service. –  Jun 17 '15 at 12:05
  • My web.config is similar to that web.config what you suggested. – good-to-know Jun 17 '15 at 12:20
  • Where is the code that invokes your web service? Please show it –  Jun 17 '15 at 12:21