远程服务器返回意外响应:(413) 请求实体太大。
Posted
技术标签:
【中文标题】远程服务器返回意外响应:(413) 请求实体太大。【英文标题】:The remote server returned an unexpected response: (413) Request Entity Too Large. 【发布时间】:2014-01-01 18:42:12 【问题描述】:我正在尝试使用 FW4.0 构建 WCF 应用程序服务。 在服务器和客户端之间传输 EntiryFramework 对象时,我的服务正常工作。但我在将 EF 对象从客户端传递到服务器时遇到问题。
这里有一些关于我的环境的更多细节: - 服务在 IIS 本地以调试模式运行 - 我在我的 Windows 7 上运行所有这些 - 我在 FW4.0 上使用 Visual Studio 2010
我正在尝试将对象 (tblClient) 发送到服务器以保存记录,但一直出现错误 (413) Request Entity Too Large。 这里是完整的堆栈:
System.ServiceModel.ProtocolException occurred
HResult=-2146233087
Message=The remote server returned an unexpected response: (413) Request Entity Too Large.
Source=mscorlib
StackTrace:
Server stack trace:
at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory`1 factory, WebException responseException, ChannelBinding channelBinding)
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at ClientApp.ServiceReference1.IService.SaveClient(tblClient client)
at ClientApp.ServiceReference1.ServiceClient.SaveClient(tblClient client) in C:\dufh\WPF Project\ClientApp\Service References\ServiceReference1\Reference.vb:line 2383
at ClientApp.ViewModel.ClientViewModel.SaveClient() in C:\dufh\WPF Project\ClientApp\ViewModel\ClientViewModel.vb:line 48
InnerException: System.Net.WebException
HResult=-2146233079
Message=The remote server returned an error: (413) Request Entity Too Large.
Source=System
StackTrace:
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
InnerException:
我做了一些研究,都指出客户端配置 App.Config 中的 maxBufferSize 和/或 maxBufferPoolSize 和/或 maxReceivedMessageSize 巫婆不够大。所以我将它们增加到最大值: maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" 但错误仍然存在。
这里是我的完整客户端 App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<sources>
<!-- This section defines the logging configuration for My.Application.Log -->
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLog"/>
<!-- Uncomment the below section to write to the Application Event Log -->
<!--<add name="EventLog"/>-->
</listeners>
</source>
</sources>
<switches>
<add name="DefaultSwitch" value="Information" />
</switches>
<sharedListeners>
<add name="FileLog"
type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
initializeData="FileLogWriter"/>
<!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="MVVM Sampling"/>
</sharedListeners>
</system.diagnostics>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"/>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:7803/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference1.IService"
name="BasicHttpBinding_IService" />
</client>
</system.serviceModel>
</configuration>
以及完整的 Web.Config WCF 服务 Web.Config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
</system.web>
<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>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="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>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<connectionStrings>
<add name="MaitreEntities" connectionString="metadata=res://*/Schemat.csdl|res://*/Schemat.ssdl|res://*/Schemat.msl;provider=System.Data.SqlClient;provider connection string="data source=INFOFILE2\SQL2008R2;initial catalog=0001ConneryFerland;user id=sa;password=kermit80;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
Any help would be very welcome :-)
【问题讨论】:
【参考方案1】:记录在案
我想我明白了。 服务中的 Web.Config 没有绑定信息。 我把这个信息放进去,瞧!
<bindings>
<basicHttpBinding>
<binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
<readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
请注意,绑定没有指定名称。
【讨论】:
谢谢! “来自服务的 web.config”是指:“服务器上的 web.config”。 谢谢。我花了几个小时试图找到缺少哪个绑定,它是服务端 web.config 绑定,没有像您的示例中那样的名称。 谢谢!就我而言,我有一个缺少您的服务没有明确的端点(即在您的配置文件中定义的端点),因此您声明的绑定配置(“BasicHttpBinding_IService”)没有被使用。 WCF 提供了一个默认端点以及一个默认绑定(basicHttpBinding
,除非您在配置文件的 protocolMapping
部分覆盖它)。
您有两种方法可以在服务的配置文件中解决此问题:
您可以通过删除name
属性使“BasicHttpBinding_IService”配置成为默认配置:
<binding maxBufferPoolSize="2147483647".....
或者您在配置中明确定义一个端点并将您的绑定配置分配给端点的bindingConfiguration
属性。
<services>
<endpoint address=""
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService"
contract="ServiceReference1.IService" />
</services>
【讨论】:
感谢蒂姆的帮助,但错误仍然存在。我将暂时搁置我的 WCF 服务。让它炖一会儿......【参考方案3】:如果您创建自定义绑定,例如MybasicBinding,
<basicHttpBinding>
<binding name="MybasicBinding" closeTimeout="01:00:00" openTimeout="01:00:00" receiveTimeout="01:00:00" sendTimeout="01:00:00"
maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" >
<readerQuotas maxDepth="32" maxBytesPerRead="200000000"
maxArrayLength="200000000" maxStringContentLength="200000000" />
</binding>
</basicHttpBinding>
为避免 413 错误,不要忘记为 服务端点指定 bindingConfiguration="MybasicBinding",
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="MybasicBinding" contract="WCFService.IService" />
【讨论】:
【参考方案4】:解决此问题并更好地查看 web.config 文件的另一种方法是使用“Microsoft 服务配置编辑器”(C:\Program Files (x86)\Microsoft SDKs\Windows\ v8.1A\bin\NETFX 4.5.1 工具\SvcConfigEditor.exe)
【讨论】:
【参考方案5】:如果没有指定绑定并且 httpsgetenabled 为真,那么您可能需要在服务应用程序的 web.config 中设置 basichttpsbinding
<bindings>
<basicHttpsBinding>
<binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
<readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpsBinding>
【讨论】:
【参考方案6】:我收到了这个错误。我发现我已经创建了绑定配置,但未能将端点设置为该配置。因此,端点使用默认设置的未命名绑定配置。所以,maxReceivedMessageSize 是 65k。
【讨论】:
以上是关于远程服务器返回意外响应:(413) 请求实体太大。的主要内容,如果未能解决你的问题,请参考以下文章
具有 30mb 输入字符串的 WCF 服务 - 远程服务器返回错误:(413)请求实体太大
将 JSON 发送到 C# WCF 服务会导致:请求实体太大
远程服务器返回意外响应:(413) Request Entity Too Large exception when using custom binding