【问题标题】:The remote server returned an unexpected response: (413) Request Entity Too Large.?远程服务器返回意外响应:(413) Request Entity Too Large.?
【发布时间】:2015-06-17 11:27:51
【问题描述】:

我将一些值从 .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>

【问题讨论】:

    标签: c# web-services wcf


    【解决方案1】:

    试试这个,

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

    我希望这对你有帮助。 :)

    【讨论】:

    • 我会试试这个。谢谢。
    • 为什么这个答案对 OP 有帮助?即,您发布的绑定配置与一个 OP 有什么不同,为什么差异很重要? (注意:我知道为什么 - 但这是,IMO,一个糟糕的答案,因为它没有解释任何东西)。
    • 非常适合我今天早上需要的东西。
    【解决方案2】:

    它可能不起作用的一个原因是您定义的绑定没有被您的端点使用,因为您没有通过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 名称的服务(在该配置文件中) .

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

    【讨论】:

    • 我会试试这个。谢谢。
    【解决方案3】:

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

    如果你发送的数据真的很大,那么你必须改用HTTP POST

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

    另外,你需要编辑你的 web.config 文件,你可以find here.

    【讨论】:

    • 即使在我将它放在我的 Web 服务方法上之后,我也会遇到同样的错误。
    • 编辑 web.config 后又出现同样的错误?如果是,请显示调用您的网络服务的代码。
    • 我的 web.config 与您建议的 web.config 类似。
    • 调用 Web 服务的代码在哪里?请出示一下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    相关资源
    最近更新 更多