发生了意想不到的错误:远程服务器返回了一个意想不到的响应:(413)请求实体太大
本文关键字:意想不到 响应 一个 实体 请求 服务器 返回 错误 发生了 | 更新日期: 2023-09-27 18:05:44
我知道这个问题被问得要死,但答案仍然难以捉摸。我已经看过了很多关于建议解决方案的帖子,但这个错误仍然困扰着我。如有任何帮助,我将不胜感激。
我有一个托管在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
}
网站的网址。当前配置如下:
<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。配置是:
<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>
我想要的是了解如何配置我的配置文件(s),以允许最大上传大小说2MB。如果有可能以二进制格式而不是Base64(或其他格式)传递数据,这也将有利于减少服务器之间的带宽需求。
在我的开发环境中,站点之间的通信目前是通过HTTP进行的,但我还需要为测试/生产配置HTTPS。附加配置上的指针加分项。
提前感谢您在这方面提供的任何帮助。我已经花了相当多的时间,但我还是没有找到解决办法。
找到了我的答案-我最初在这里发布了一个链接,但其中一个mod决定删除答案(谢谢!),所以我将发布完整的解决方案:
为了允许超过默认40KB的有效负载(如文件或大型数据数组)被发送到WCF服务方法,所需的更改主要在web中。WCF项目的配置文件。在客户端的配置中还需要做一个小的调整,所以我们先来看一下:
<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>
当你第一次向客户端的项目添加服务引用时,Visual Studio会自动在你的配置文件中创建默认绑定和端点节点。您需要为服务的绑定节点添加两个新属性:maxBufferPoolSize和maxReceivedMessageSize。这两个属性的值都应该设置为所需的文件上传大小限制(以字节为单位)。您还需要注意下一位的名称。
这就是客户端配置,以此类推到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中注意到的绑定的名称相匹配。配置文件。复制上面示例中的属性和子节点,并根据需要进行修改。AKAIK,所有的大小值都以字节为单位。
2)稍微跳跃一下,我们接下来将移动到配置提取中的最后一个更改。Visual Studio创建了一个带有所有正确设置的默认行为节点,但没有在该节点上设置名称,所以添加一个name属性并给它一个值。ServiceWithMetadata是我在例子中找到的值,所以这就是我使用的——我不知道你怎么称呼它是否真的重要。
3)为您的服务创建服务节点。名称必须是服务类的完全限定名称和behaviourConfiguration需要服务节点的名称,在步骤2)。在服务节点需要配置一个端点,bindingConfiguration的需要设置为绑定在步骤1中设置)的名称和合同需要设置为您的服务的接口类的完全限定名称坚持。
就是这样。弄清楚这一点花了太长时间,我无法相信这个过程是如此复杂,或者关于这个主题的文档是如此之少。对于那些和我有同样问题的人:祝你好运!我希望这篇文章可以节省你的时间,我不得不花时间弄清楚这个困难的方式。