簡體   English   中英

大型 WCF Web 服務請求因 (400) HTTP 錯誤請求而失敗

[英]Large WCF web service request failing with (400) HTTP Bad Request

我遇到了這個明顯常見的問題,但一直無法解決。

如果我使用數組參數中相對較少的項目(我已經測試了 50 個)來調用我的 WCF Web 服務,那么一切都很好。

但是,如果我使用 500 個項目調用 Web 服務,則會收到錯誤請求錯誤。

有趣的是,我在服務器上運行了Wireshark ,看起來請求甚至沒有到達服務器 - 400 錯誤是在客戶端生成的。

例外是:

System.ServiceModel.ProtocolException:遠程服務器返回意外響應:(400) 錯誤請求。 ---> System.Net.WebException:遠程服務器返回錯誤:(400) 錯誤請求。

我的客戶端配置文件的system.serviceModel部分是:

 <system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="WSHttpBinding_IMyService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
                messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                allowCookies="false">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="2147483647"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00"
                    enabled="false" />
                <security mode="None">
                    <transport clientCredentialType="Windows" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="Windows" negotiateServiceCredential="true"
                        establishSecurityContext="true" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://serviceserver/MyService.svc"
            binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IMyService"
            contract="SmsSendingService.IMyService" name="WSHttpBinding_IMyService" />
    </client>
</system.serviceModel>

在服務器端,我的 web.config 文件具有以下system.serviceModel部分:

<system.serviceModel>
    <services>
        <service name="MyService.MyService" behaviorConfiguration="MyService.MyServiceBehaviour" >
            <endpoint address="" binding="wsHttpBinding" bindingConfiguration="MyService.MyServiceBinding" contract="MyService.IMyService">
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <bindings>
      <wsHttpBinding>
        <binding name="MyService.MyServiceBinding">
          <security mode="None"></security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <behaviors>
        <serviceBehaviors>
            <behavior name="MyService.MyServiceBehaviour">
                <!-- 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="true"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

看了一個相當大的數量回答這個問題,沒有成功

誰能幫我這個?

也嘗試在服務器上設置 maxReceivedMessageSize,例如設置為 4MB:

<binding name="MyService.MyServiceBinding" maxReceivedMessageSize="4194304">

默認值(我認為是 65535)如此之低的主要原因是為了降低拒絕服務 (DoS) 攻擊的風險。 您需要將其設置為大於服務器上的最大請求大小和客戶端上的最大響應大小。 如果您處於 Intranet 環境中,DoS 攻擊的風險可能很低,因此使用比您預期需要的值高得多的值可能是安全的。

順便說一下,解決連接到 WCF 服務的問題的一些技巧:

我也遇到了這個問題,但是上面沒有一個對我有用,因為我在長時間挖掘后使用自定義綁定(用於 BinaryXML)我在這里找到了答案: Sending large XML from Silverlight to WCF

在使用 customBinding 時,必須在 web.config 中 binding 元素下的 httpTransport 元素上設置 maxReceivedMessageSize:

<httpsTransport maxReceivedMessageSize="4194304" /> 

值得一提的是,使用 .NET 4.0 時的另一個注意事項是,如果在您的配置中找不到有效端點,則會自動創建和使用默認端點。

默認端點將使用所有默認值,因此如果您認為您有一個有效的服務配置,其中 maxReceivedMessageSize 等值較大,但配置有問題,您仍然會收到 400 Bad Request,因為默認端點將是創建和使用。

這是靜默完成的,因此很難被發現。 如果您在服務器上打開跟蹤但沒有其他指示(據我所知),您將看到有關此效果的消息(例如“未找到服務端點,創建默認端點”或類似信息)。

在 web.config 中的 .NET 4.0 服務器中,您還需要更改默認綁定。 設置以下 3 個參數:

<basicHttpBinding>  
    <!-- http://www.intertech.com/Blog/post/NET-40-WCF-Default-Bindings.aspx  
         Enable transfer of large strings with maxBufferSize, maxReceivedMessageSize and maxStringContentLength
    -->  
       <binding **maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"**>  
          <readerQuotas **maxStringContentLength="2147483647"**/>            
       </binding>
</basicHttpBinding>

您還可以打開 WCF 日志記錄以獲取有關原始錯誤的更多信息。 這幫助我解決了這個問題。

將以下內容添加到您的 web.config,它將日志保存到 C:\\log\\Traces.svclog

<system.diagnostics>
    <sources>
        <source name="System.ServiceModel"
                  switchValue="Information, ActivityTracing"
                  propagateActivity="true">
            <listeners>
                <add name="traceListener"
                     type="System.Diagnostics.XmlWriterTraceListener"
                     initializeData= "c:\log\Traces.svclog" />
            </listeners>
        </source>
    </sources>
</system.diagnostics>

調試客戶端可能很有用,關閉 Tools\\Options\\Debugging\\General\\'Enable Just My Code',單擊 Debug\\Exceptions\\'catch all first-chance exceptions' 以獲取托管 CLR 異常,然后查看是否存在在協議異常之前和消息到達線路之前客戶端上的異常。 (我的猜測是某種序列化失敗。)

只想指出

除了 MaxRecivedMessageSize 之外,ReaderQuotas 下還有一些屬性,您可能會遇到項目數限制而不是大小限制。 MSDN 鏈接在這里

我找到了錯誤請求 400 問題的答案。

這是默認的服務器綁定設置。 您需要添加到服務器和客戶端的默認設置。

<binding name="" openTimeout="00:10:00" closeTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" maxBufferSize="2147483647">
    <security mode="None"/>
    <readerQuotas maxStringContentLength="2147483647"/>
</binding>

就我而言,即使在嘗試了所有解決方案並將所有限制設置為最大值后,它也無法正常工作。 最后我發現IIS/網站上安裝了一個Microsoft IIS過濾模塊Url Scan 3.1 ,它有自己的限制,可以根據內容大小拒絕傳入請求並返回“404 Not found page”。

通過將MaxAllowedContentLength設置為所需的值,可以在%windir%\\System32\\inetsrv\\urlscan\\UrlScan.ini文件中更新它的限制。

例如。 以下將允許多達 300 mb 的請求

MaxAllowedContentLength=314572800

希望它會幫助某人!

暫無
暫無

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

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