传递大字节 [] 时 WCF 服务错误 413“实体太大”

Posted

技术标签:

【中文标题】传递大字节 [] 时 WCF 服务错误 413“实体太大”【英文标题】:WCF Service Error 413 "Entity Too Large" when pass big byte[] 【发布时间】:2014-01-19 18:53:20 【问题描述】:

尝试将大字节 [] 传递给 wcf 服务时出现此错误 我的 IService 代码:

[OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, UriTemplate = "SaveFile")]
        SaveFileResult SaveFile(byte[] FileBytes, string FileName, decimal Filesize, string IntegrationSystem, string TellerIndentity, string SendingOfficeCode, string SendingArea);

服务代码:

public SaveFileResult SaveFile(byte[] FileBytes, string FileName, decimal Filesize, string IntegrationSystem, string TellerIndentity, string SendingOfficeCode, string SendingArea)
        
            FileFactory _FileFactory = new FileFactory();
            string _strExt = Path.GetExtension(FileName);
            IFileDealer _IFileDealer = _FileFactory.GetFileDealer(_strExt);
            SaveFileResult _Result=_IFileDealer.SaveFile(FileBytes, FileName, Filesize, IntegrationSystem, TellerIndentity, SendingOfficeCode, SendingArea);
            return _Result;
        

wcf 服务配置:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="CenterPostEntities" connectionString="metadata=res://*/DatabaseDesign.GRemModel.csdl|res://*/DatabaseDesign.GRemModel.ssdl|res://*/DatabaseDesign.GRemModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=TEST-SH\SQL2005;initial catalog=CenterPost;user id=sa;password=itsc;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <appSettings>
    <add key="PermittedFastUploadSize"  value="1000000"/>
    <add  key ="GRemLog" value="d:\GRemLog"/>
    <add key="FileUploadPath" value="d:\FileUpload"/>
  </appSettings>
    <system.web>
    <compilation debug="true" targetFramework="4.0" />
      <httpRuntime maxRequestLength="2097151" />
  </system.web>
  <system.serviceModel>
    <!--======================-->
    <bindings>
      <basicHttpBinding>
        <binding name="basicHttpBindingSettings"  openTimeout="00:01:00" receiveTimeout="05:00:00" sendTimeout="05:00:00" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647777" messageEncoding="Text">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          <!-- 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>
    <!--======================-->
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
   <serverRuntime enabled="true" uploadReadAheadSize="2147483647"  />
  </system.webServer>

请任何人帮我解决这个问题 我在 iis7 上增加了 uploadReadAheadSize 但仍然无法正常工作 我想知道为什么这段代码不起作用

【问题讨论】:

看看Request Entity Too Large和这个Request Entity Too Large 你可以从服务器添加端点配置吗? 我添加了它但仍然无法正常工作 【参考方案1】:

WCF 服务未使用您在配置文件中定义的绑定(“basicHttpBindingSettings”),因为您没有定义使用它的端点。在 WCF 4.0+ 中,如果配置文件中没有定义端点,则将创建一个默认端点(并且开箱即用,绑定将为 basicHttpBinding 和默认值)。

您有两种方法可以解决此问题。

首先,您可以通过省略 name 属性将绑定定义设为默认值,如下所示:

<bindings>
  <basicHttpBinding>
    <binding openTimeout="00:01:00" receiveTimeout="05:00:00" 
             sendTimeout="05:00:00" maxReceivedMessageSize="2147483647"
             maxBufferPoolSize="2147483647777" messageEncoding="Text">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                    maxArrayLength="2147483647" maxBytesPerRead="2147483647"
                    maxNameTableCharCount="2147483647" />
    </binding>
  </basicHttpBinding>
</bindings>

这将成为basicHttpBinding 的默认配置。

第二个选项是定义一个显式端点并通过bindingConfiguration 属性将绑定配置分配给它,如下所示:

<services>
  <service name="<service name>">
    <endpoint address="" binding="basicHttpBinding"
              bindingConfiguration="basicHttpBindingSettings"
              contract="<fully qualified contract name>" />
  </service>
</services>

有关默认端点和绑定的更多信息,请参阅A Developer's Introduction to Windows Communication Foundation 4。

【讨论】:

以上是关于传递大字节 [] 时 WCF 服务错误 413“实体太大”的主要内容,如果未能解决你的问题,请参考以下文章

使用 WCF 服务传输大文件 - 检索错误 413

(413) 请求实体太大 - 在 Azure 上上传 Wcf 时

WCF:远程服务器返回错误:(413)请求实体太大[重复]

具有 30mb 输入字符串的 WCF 服务 - 远程服务器返回错误:(413)请求实体太大

WCF错误:413 Request Entity Too Large

WCF(413)请求实体太大