错误:远程服务器返回意外响应:(413)请求实体太大

时间:2014-10-06 09:50:10

标签: c# asp.net wcf

我设计了一个简单的WCF服务,允许用户上传文件。我还创建了一个ASP.NET网站来采用该服务。但是,我一直收到错误消息"远程服务器返回了意外的响应:

  

(413)请求实体太大

我试图搜索MSDN和StackOverflow来寻求解决方案,但它们都不起作用。

我的WCF服务的web.config

<?xml version="1.0"?>
<configuration>
   <appSettings>
      <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
   </appSettings>
   <system.web>
       <compilation debug="true" targetFramework="4.5" />
       <httpRuntime targetFramework="4.5"/>
   </system.web>
   <system.serviceModel>
      <bindings>
         <basicHttpBinding>
            <binding name="bindingMetadata" maxBufferPoolSize="2147483647">
               <readerQuotas maxDepth="256" maxStringContentLength="2147483647" 
                      maxArrayLength="2147483647" maxBytesPerRead="2147483647" 
                      maxNameTableCharCount="65536" />
            </binding>
         </basicHttpBinding>
      </bindings>
      <services>
         <service name="ServiceStorage.ServiceStorage" 
                  behaviorConfiguration="behaviorMetadata">
            <endpoint
                address=""
                binding="basicHttpBinding"
                bindingConfiguration="bindingMetadata"
                contract="ServiceStorage.IService"/>
            <endpoint
                address="mex"
                binding="mexHttpBinding"
                contract="IMetadataExchange"/>
         </service>
      </services>
      <behaviors>
         <serviceBehaviors>
             <behavior name="behaviorMetadata">
                 <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                 <serviceDebug includeExceptionDetailInFaults="true"/>
                 <dataContractSerializer maxItemsInObjectGraph="2147483647" />
             </behavior>
         </serviceBehaviors>
         <endpointBehaviors>
             <behavior>
                  <dataContractSerializer maxItemsInObjectGraph="2147483647" />
             </behavior>
         </endpointBehaviors>
      </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"/>
    <security>
    <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483647" />
    </requestFiltering>
    </security>
</system.webServer>

</configuration>

ASP.NET网站的Web.config

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-TestServiceStorage-01f455a2-2fb1-42f0-9099-18bc95b05403;AttachDbFilename=|DataDirectory|\aspnet-TestServiceStorage-01f455a2-2fb1-42f0-9099-18bc95b05403.mdf;Integrated Security=SSPI" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5" maxRequestLength="2097150"/>
    <pages>
      <namespaces>
        <add namespace="System.Web.Optimization"/>
        <add namespace="Microsoft.AspNet.Identity"/>
      </namespaces>
      <controls>
        <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt"/>
      </controls>
    </pages>
    <membership>
      <providers>
        <!--
        ASP.NET Membership is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template
        -->
        <clear/>
      </providers>
    </membership>
    <profile>
      <providers>
        <!--
        ASP.NET Membership Profile is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template
        -->
        <clear/>
      </providers>
    </profile>
    <roleManager>
      <!--
          ASP.NET Membership Role is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template
        -->
      <providers>
        <clear/>
      </providers>
    </roleManager>
    <!--
            If you are deploying to a cloud environment that has multiple web server instances,
            you should change session state mode from "InProc" to "Custom". In addition,
            change the connection string named "DefaultConnection" to connect to an instance
            of SQL Server (including SQL Azure and SQL  Compact) instead of to SQL Server Express.
      -->
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection"/>
      </providers>
    </sessionState>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService" maxBufferSize="1073741824" maxReceivedMessageSize="1073741824" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:65001/ServiceStorage.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService"
        contract="ServiceStorage.IService" name="BasicHttpBinding_IService" />
    </client>
  </system.serviceModel>
</configuration>

有没有人有任何想法? 谢谢,

1 个答案:

答案 0 :(得分:0)

你试过this solution吗?

  

默认情况下,WCF将邮件限制为65KB以避免拒绝服务   用大邮件攻击。 [...]要解决这个问题,你必须这样做   重新配置您的服务以接受更大的消息。

     

您需要在绑定中设置maxReceivedMessageSize。你也可以   需要设置readerQuotas

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding maxReceivedMessageSize="10485760">
        <readerQuotas ... />
      </binding>
    </basicHttpBinding>
  </bindings>  
</system.serviceModel>