413上传大图像文件时,请求实体在wcf服务中太大

时间:2014-11-28 07:51:27

标签: wcf base64 rest-client

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

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

413请求实体太大

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

enter image description here

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

以上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>

但错误仍未得到重新发送。

任何人都可以帮帮我???

1 个答案:

答案 0 :(得分:2)

虽然您已定义basicHttpBinding大于默认值,但您尚未告知WCF服务实际使用该绑定。如果未定义任何服务端点,则默认的开箱即用绑定为basicHttpBinding,其中默认值。

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

<bindings>
  <basicHttpBinding>
    <binding 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>

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

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

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

整个<system.serviceModel>

选项1:定义了默认绑定(从绑定配置中省略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:通过端点&#39; bindingConfiguration属性使用指定的绑定配置显式端点:

<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>

这些选项中的任何一个都应解决此问题。如果他们没有,请尝试我在原始答案中提供的第三个选项。