远程服务器返回意外响应:(413) 请求实体太大。?

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

我将一些值以字符串格式从 .net 应用程序传递到我的 WCF 服务。传递的字符串格式将采用这种结构,

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

每个订单项将以“|”分隔特点。我传递了将近 1000 件物品。它按预期工作,但是当我传递 1500 个项目时,出现此错误。

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

请帮我解决这个错误。

这是服务中的方法

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

这是我的 WCF 服务 web.config

<?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>

这是因为您的服务类型 HTTP GET 长度有限。

如果您发送的数据量很大,那么您必须使用HTTP POST代替。

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

此外,您需要编辑 web.config 文件,您可以 find here.

它可能不起作用的一个原因是您定义的绑定未被端点使用,因为您没有通过 endpoint 元素中的 bindingConfiguration 属性指定它。这导致 WCF 使用 basicHttpBinding 的默认值(较低),而不是您的值。

试试这个:

<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>

请注意上面示例中 bindingConfiguration 属性的使用。

另请注意,如果您在 IIS 中托管服务,则不需要 baseAddress 元素,因为基址将是 .svc 文件的位置。

已添加

相同的原则适用于您的端点行为 - 它也未分配给端点 - 您需要为此使用 behaviorConfiguration 属性:

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

服务行为配置部分没有指定 name 属性,因此它被视为默认服务行为并应用于所有未明确设置的服务(在该配置文件中) behaviorConfiguration 姓名。

空白名称 = default 也适用于绑定配置和端点行为配置,IIRC。

试试这个,

<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>

而不是代码中的这些行,

<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>

希望对您有所帮助。 :)