简体   繁体   中英

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

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>

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. :)

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM