C# WCF 4.5:发生意外错误:远程服务器返回意外响应:(413)请求实体太大

C# WCF 4.5:发生意外错误:远程服务器返回意外响应:(413)请求实体太大,c#,wcf,file-upload,.net-4.5,C#,Wcf,File Upload,.net 4.5,我知道这个问题已经被问到了极点,但答案仍然难以捉摸。我已经读过很多关于建议解决方案的帖子,但是这个错误仍然困扰着我。任何帮助都将不胜感激 我有一个托管在IIS上的.Net 4.5 WCF服务,它由另一个IIS框上的MVC5网站使用。大部分情况下,通信工作正常,但我需要允许网站将文件上载到WCF服务,并且所有呼叫都被炸断,出现以下错误: 发生意外错误:远程服务器返回意外响应:(413)请求实体太大 web服务中调用的方法将单个POCO作为参数。POCO包含许多属性,包括用于上载文件内容的字节数组。

我知道这个问题已经被问到了极点,但答案仍然难以捉摸。我已经读过很多关于建议解决方案的帖子,但是这个错误仍然困扰着我。任何帮助都将不胜感激

我有一个托管在IIS上的.Net 4.5 WCF服务,它由另一个IIS框上的MVC5网站使用。大部分情况下,通信工作正常,但我需要允许网站将文件上载到WCF服务,并且所有呼叫都被炸断,出现以下错误:

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

web服务中调用的方法将单个POCO作为参数。POCO包含许多属性,包括用于上载文件内容的字节数组。例如:

public class ProofOfAddressRequest : RequestBase
{
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string Region { get; set; }
    public string City { get; set; }
    public string PostCode { get; set; }
    public string Country { get; set; }
    public string FileName { get; set; }
    public byte[] FileBytes { get; set; }
}

public ProofOfAddressResponse SubmitProofOfAddress(ProofOfAddressRequest data)
{
    // TODO... Save the record
}
网站的web.config当前如下所示:

<configuration>
  <configSections/>

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IUserService" maxBufferPoolSize="2097152" maxBufferSize="2097152" maxReceivedMessageSize="2097152">
          <readerQuotas maxDepth="32" maxStringContentLength="2097152" maxArrayLength="2097152" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
          <security mode="None"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:58354/Services/UserService.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IUserService"
        contract="UserService.IUserService" name="BasicHttpBinding_IUserService" />
    </client>
  </system.serviceModel>

  <system.web>
    <customErrors mode="Off" />
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" maxRequestLength="4096"/>
  </system.web>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

</configuration>

web服务的web.config是:

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

  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" maxRequestLength="4096"/>
  </system.web>

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding" maxBufferPoolSize="2097152" maxBufferSize="2097152" maxReceivedMessageSize="2097152">
          <readerQuotas maxDepth="32" maxStringContentLength="2097152" maxArrayLength="2097152" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
          <security mode="None"/>
        </binding>
      </basicHttpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceWithMetadata">
          <!-- 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="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpBinding" scheme="http" />
      <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.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

我想了解的是如何配置配置文件以允许最大上传大小(比如2MB)。如果可以以二进制格式而不是Base64(或其他任何格式)传递数据,这也有利于降低服务器之间的带宽要求

在我的开发环境中,站点之间的通信目前是通过HTTP进行的,但我还需要为测试/生产配置HTTPS。额外配置上指针的额外点数


提前感谢您在这方面提供的任何帮助。这已经花费了我相当多的时间,我还没有找到解决方案。

找到了我的答案-我最初在这里发布了一个链接,但其中一个MOD决定删除答案(谢谢!),因此我将发布整个解决方案:


为了允许将超过默认40KB的有效负载(如文件或大型数据阵列)发送到WCF服务方法,所需的更改主要在WCF项目的web.config文件中。在客户端的配置中还需要进行一个小的调整,因此我们将首先了解这一点:

<configuration>
  ...
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <!-- Large Message Upload (Begin) -->
        <binding name="BasicHttpBinding_IUserService" maxBufferPoolSize="2097152" maxReceivedMessageSize="2097152" />
        <!-- Large Message Upload (End) -->
        <binding name="BasicHttpBinding_ISystemService" />
        ...
      </basicHttpBinding>
    </bindings>
    ...
  </system.serviceModel>
</configuration>

...
...
...
当您第一次向客户端的项目添加服务引用时,VisualStudio会自动在配置文件中创建默认绑定和端点节点。您需要为服务的绑定节点添加两个新属性:maxBufferPoolSize和maxReceivedMessageSize。这两个属性的值都应设置为所需的文件上载大小限制(以字节为单位)。您还需要记下下下一位的名称

这是客户端配置的if,依此类推到WCF服务。以下是我的摘录:

<configuration>
   ...
  <system.serviceModel>

    <bindings>
      <basicHttpBinding>
        <!-- Large Message Upload (Begin) -->
        <binding name="BasicHttpBinding_IUserService"
                 maxBufferSize="2097152"
                 maxBufferPoolSize="2097152"
                 maxReceivedMessageSize="2097152"
                 closeTimeout="00:50:00" 
                 openTimeout="00:50:00" 
                 sendTimeout="00:50:00" 
                 receiveTimeout="00:50:00">
          <readerQuotas maxDepth="32" maxStringContentLength="100000"
                        maxArrayLength="2097152" maxBytesPerRead="4096"
                        maxNameTableCharCount="16384" />
          <security mode="None" />
        </binding>
        <!-- Large Message Upload (End) -->
      </basicHttpBinding>
    </bindings>

    <services>
      <!-- Large Message Upload (Begin) -->
      <service name="BlexEngine.Services.UserService"
               behaviorConfiguration="ServiceWithMetadata">
        <endpoint name="Default"
                  binding="basicHttpBinding"
                  bindingConfiguration="BasicHttpBinding_IUserService"
                  contract="BlexEngine.Services.IUserService" />
      </service>
      <!-- Large Message Upload (End) -->
    </services>

    <behaviors>
      <serviceBehaviors>
        <!-- Large Message Upload (Begin) -->
        <behavior name="ServiceWithMetadata">
          <!-- Large Message Upload (End) -->

          <!-- 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="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    ...
  </system.serviceModel>
  ...
</configuration>

...
...
...
如您所见,需要进行三项更改:

1) 专门为要启用大文件上载的服务创建绑定(我的项目目前发布五个服务,但只有一个需要更大的文件上载限制)。确保name属性的值与我们在客户机的web.config文件中注意到的绑定的名称匹配。复制上述示例中的属性和子节点,并根据需要进行修改。所有大小值均以字节为单位

2) 跳一跳,我们接下来将转到配置摘录中的最后一个更改。Visual Studio使用所有正确的设置创建默认行为节点,但不在该节点上设置名称,因此添加名称属性并为其指定值。ServiceWithMetadata是我在示例中找到的值,所以我使用了它-我不知道它是否真的与你所说的有关

3) 为您的服务创建服务节点。名称必须是服务类的完全限定名称,行为配置必须是服务节点的名称,如步骤2所示)。在服务节点中,您需要配置一个端点,其bindingConfiguration需要设置为步骤1)中绑定设置的名称,其协定需要设置为服务类所遵循接口的完全限定名称

真的是这样。弄明白这一点花了太长时间,我无法相信这个过程有多么复杂,或者关于这个主题的文档有多么少。对于任何一个和我有同样问题的人:祝你好运!我希望这篇文章能为你节省时间,让我花更多的时间来解决这个问题