簡體   English   中英

文件上傳,遠程服務器返回錯誤:(413) Request Entity Too Large

[英]File Upload, The remote server returned an error: (413) Request Entity Too Large

當我通過 WCF 服務上傳文件時,收到異常“遠程服務器返回錯誤:(413) 請求實體太大”

關於這個例外有很多問題和答案。 但是,它們都沒有解決我的問題。

我使用的是 Windows Web Server 2008 R2、IIS 7.5。 我的客戶端和 WCF 應用程序托管在 IIS 中。 我正在使用頻道來調用 WCF 服務。

        var binding = new BasicHttpBinding();

        binding.MaxReceivedMessageSize = Int32.MaxValue;
        binding.MaxBufferPoolSize = Int32.MaxValue;
        binding.MaxBufferSize = Int32.MaxValue;
        binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
        binding.ReaderQuotas.MaxArrayLength = Int32.MaxValue;
        binding.ReaderQuotas.MaxBytesPerRead = Int32.MaxValue;
        binding.ReaderQuotas.MaxDepth = Int32.MaxValue;
        binding.ReaderQuotas.MaxNameTableCharCount = Int32.MaxValue;

        var address = new EndpointAddress("WCF Service URL");

        return ChannelFactory<IFileService>.CreateChannel(binding, address);

WCF配置文件如下:

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="behavior1">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="MyProject.WCF.FileService" behaviorConfiguration="behavior1">
        <endpoint binding="basicHttpBinding" bindingName="binding1" contract="MyProject.WCF.Contracts.Service.IFileService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="binding1" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

另外,我在客戶端 web.config 文件中添加了以下配置:

<httpRuntime maxRequestLength="2097151" />

<serverRuntime uploadReadAheadSize="2147483647" />
<security>
    <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483648" />
    </requestFiltering>
</security> 

我更改了 C:\\Windows\\System32\\inetsrv\\config\\applicationHost.config,

<location path="My Site Path" overrideMode="Allow">
    <system.webServer>
        <httpLogging dontLog="true" />
        <serverRuntime uploadReadAheadSize="2147483647" />
    </system.webServer>
</location>

但是,它們都沒有解決我的問題。 Windows Server 2012沒有問題,應該是Windows Server 2008的問題。Windows Server 2008如何用WCF上傳大文件?

謝謝。

您可能需要更新 System.WebServer 部分中的 IIS 請求限制,默認值為 30MB。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <security>
            <requestFiltering>
                <requestLimits maxAllowedContentLength="30000001" />
            </requestFiltering>
        </security>
    </system.webServer>
</configuration>

更多信息在這里:

http://www.iis.net/configreference/system.webserver/security/requestfiltering/requestlimits

您可以使用 IIS 中的配置編輯器執行此操作。

要訪問它,請在 IIS 管理器中選擇網站,然后在管理部分下選擇配置編輯器。 深入到 requestLimits 部分,您可以根據下面的屏幕截圖更新那里的配置。 記得點擊“應用”。 這將更新應用程序根目錄中的 Web.Config。

根據您的部署過程,此更改可能會在下一個版本中丟失,因此值得考慮在源代碼管理中更新您的配置。

在此處輸入圖片說明

我必須將它添加到 system.web 部分中的 web.config(注意:不是 system.webserver)

<system.web> < httpRuntime maxRequestLength="100485760" requestValidationMode="2.0" maxQueryStringLength="18192" /> </system.web>

暫無
暫無

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

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