簡體   English   中英

遠程服務器返回意外響應:(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>

嘗試這個,

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

我希望這可以幫助你。 :)

它可能無法工作的一個原因是您已定義綁定未被端點使用,因為您沒有通過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。

這是因為您的服務是HTTP GET類型,其長度有限。

如果發送的數據非常大,則必須使用HTTP POST

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

此外,您需要編輯web.config文件,如此處所示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM