2014-07-24 102 views
2

當我通過WCF服務上傳文件時,收到異常「遠程服務器返回錯誤:(413)請求實體太大」文件上傳,遠程服務器返回錯誤:(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上傳大文件?

謝謝。

回答

1

您可能需要更新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。

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

enter image description here

+0

我注意到你談到更新客戶端的web.config中,客戶端程序Web.Config發出請求,而服務器是請求過濾發生,所以更改客戶端的web.config的請求限制將不起作用。 –

+0

另外,我會考慮使用WCF流,它可以防止文件存儲在RAM中。我使用它來傳輸大文件而沒有這些問題。 –

+0

我更改了客戶端web.config和WCF應用程序web.config。但是沒有效果。 – selami

相關問題