wcf - 413 Request Entity Too Large in wcf 服务上传大图片文件时

标签 wcf base64 rest-client

我创建了 wcf 服务,我在其中接收 Base64 字符串并将 base64 字符串转换为图像以存储在我的项目中。

但是当我像这样从休息客户端调用我的 wcf 服务方法时,我收到这样的错误:

413 请求实体太大

有时当我从 rest 客户端调用我的 wcf 方法时没有任何反应。

enter image description here

以上是我如何调用我的 wcf 服务方法。

base 64 字符串之上是一个大小为 177Kb 的图像。

但是当我传递大小为 2 kb 或 3 kb 的小型 base64 图像字符串时,我的 wcf 服务方法正在调用。

这是我的 web.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <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>
  <connectionStrings>
  </connectionStrings>

  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.serviceModel>
    <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>
    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

我在互联网上搜索了很多并添加了这个:

<bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
          <readerQuotas maxDepth="200" maxStringContentLength="8388608" maxArrayLength="16384" maxBytesPerRead="2147483647" maxNameTableCharCount="16384" />
        </binding>
      </basicHttpBinding>
    </bindings>

但错误仍未解决。

谁能帮帮我???

最佳答案

虽然您已经定义了一个 basicHttpBinding大于默认值,您没有告诉 WCF 服务实际使用该绑定(bind)。当没有定义服务端点时,默认的开箱即用绑定(bind)是 basicHttpBinding使用默认值。

您可以使用 basicHttpBinding 将您定义的绑定(bind)设置为任何服务的默认绑定(bind)配置通过省略 name 使用该配置文件绑定(bind)定义中的属性,如下所示:

<bindings>
  <basicHttpBinding>
    <binding maxBufferPoolSize="2147483647" 
             maxReceivedMessageSize="2147483647" 
             maxBufferSize="2147483647">
      <readerQuotas maxDepth="200" 
                    maxStringContentLength="8388608" 
                    maxArrayLength="16384" 
                    maxBytesPerRead="2147483647" 
                    maxNameTableCharCount="16384" />
    </binding>
  </basicHttpBinding>
</bindings>

或者您可以将定义的绑定(bind)分配给一个显式端点,它看起来像这样:

<services>
  <service name="MyService">
    <endpoint address="" binding="basicHttpBinding"
              bindingConfiguration="BasicHttpBinding_IService"
              contract="MyNamespace.IServiceContract" />
    </endpoint>
  </service>

第三个要检查的地方是 IIS 最大请求长度:

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="2147483647" />
  </system.web>
</configuration>

如果您要上传非常大的文件,您可能还需要查看 chunkingstreaming .

整个<system.serviceModel>

选项 1:定义默认绑定(bind)(从绑定(bind)配置中省略 name 属性)

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding maxBufferPoolSize="2147483647" 
               maxReceivedMessageSize="2147483647" 
               maxBufferSize="2147483647">
        <readerQuotas maxDepth="200" 
                      maxStringContentLength="8388608" 
                      maxArrayLength="16384" 
                      maxBytesPerRead="2147483647" 
                      maxNameTableCharCount="16384" />
      </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>

选项 2: 通过端点的 bindingConfiguration 使用指定绑定(bind)配置的显式端点属性:

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="BasicHttpBinding_IService"
               maxBufferPoolSize="2147483647" 
               maxReceivedMessageSize="2147483647" 
               maxBufferSize="2147483647">
        <readerQuotas maxDepth="200" 
                      maxStringContentLength="8388608" 
                      maxArrayLength="16384" 
                      maxBytesPerRead="2147483647" 
                      maxNameTableCharCount="16384" />
      </binding>
    </basicHttpBinding>
  </bindings>
  <services>
    <service name="MyService">
      <endpoint address="" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService"
                contract="MyNamespace.IServiceContract" />
      </endpoint>
    </service>
  </services>
  <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>

这些选项中的任何一个都应该可以解决问题。如果他们不这样做,请尝试我在原始答案中给出的第三个选项。

关于wcf - 413 Request Entity Too Large in wcf 服务上传大图片文件时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27184156/

相关文章:

ios - 使用内容类型应用程序/JSON iOS 未获得 JSON 响应

tomcat - 当我将它移动到 Tomcat 时,我的 JRuby rest-client 调用不起作用

java - 使用 restclient 或 postman 发送 gzip 数据

c# - 在 WCF 中捕获第三方 DLL 事件

WCF 通过 MSMQ 绑定(bind)。如何检测邮件何时移至毒物队列?

javascript - 如何将从 jquery mobile 表单输入的查询 SQL 发布到 WCF 服务并在 WCF 服务上运行该查询?

java - 用Java高效序列化数据并用Javascript读取

c++ - C++ 模幂运算的问题

wcf - 从 jQuery 调用启用 AJAX 的 WCF 服务

android - encodeToString base64 安卓问题