首页 > 解决方案 > 远程服务器返回错误:(413) Request Entity too Large WCF

问题描述

我有一个 Windows 应用程序使用 WCF 服务将图像发送到服务器。当我运行代码并上传图像时,WCF 返回“远程服务器返回错误:(413) 请求实体太大。”

我从链接尝试了几种方法,但没有解决。

以下是我的 WCF 配置。

<configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation targetFramework="4.5.2" />
    <httpRuntime maxRequestLength="2147483647" />
  </system.web>
  <system.serviceModel>
    <bindings>
       <basicHttpBinding>
        <binding name="secureHttpBinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
          <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="Transport" />
        </binding>

        </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>

以下是来自 Windows 应用的 app.config。

    <system.serviceModel>
        <bindings>
      <basicHttpBinding>

        <binding name="BasicHttpBinding_ILicencePhoto" maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
          maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="Transport" />
        </binding>

       <binding name="BasicHttpBinding_IUserLogging" >
          <security mode="Transport" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>

          <endpoint address="https://xxxxx/wcf/image.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ILicencePhoto"
        contract="LicencePhoto.ILicencePhoto" name="BasicHttpBinding_ILicencePhoto" />       

 <endpoint address="https://xxxxx/wcf/user.svc" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IUserLogging" contract="UserLogging.IUserLogging"
        name="BasicHttpBinding_IUserLogging" />
    </client>
  </system.serviceModel>

我可以通过调用 userWCF 登录。问题是在将用户照片上传到 WCF 服务时。用户照片文件大小最大仅为 500KB 左右。

谢谢。尼尼昂

标签: wcf-binding

解决方案


主要问题是我们没有在服务器端应用配置。由于服务器端使用 ProtocolMapping 特性使服务工作在 HTTPS 协议上,我们应该修改并应用 BasicHttpsBinding 而不是 BasicHttpBinding 上的配置。
请参考以下配置(服务器端)。

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="secureHttpBinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
          <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="Transport" />
        </binding>
      </basicHttpBinding>
      <basicHttpsBinding>
        <binding name="httpsBinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
          <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="Transport" />
        </binding>
      </basicHttpsBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" bindingConfiguration="httpsBinding"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

如果问题仍然存在,请随时告诉我。


推荐阅读