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

本文关键字:请求 实体 服务器 返回 意外 响应 | 更新日期: 2024-09-19 22:23:15

我正在尝试使用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="2147.43647"maxReceivedMessageSize=但错误仍然存在。

这里是我的完整客户端应用程序配置

<?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=&quot;data source=INFOFILE2'SQL2008R2;initial catalog=0001ConneryFerland;user id=sa;password=kermit80;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

Any help would be very welcome :-)

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

对于记录

我想我明白了。来自服务的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>

请注意,绑定没有指定名称。

您的服务没有显式端点(意味着在配置文件中定义的端点),因此您声明的绑定配置("BasicHttpBinding_IService")没有被使用。WCF提供了一个默认端点和一个默认绑定(basicHttpBinding,除非您在配置文件的protocolMapping部分中重写它)。

您有两种方法可以在服务的配置文件中解决此问题:

您可以通过删除name属性使"BasicHttpBinding_IService"配置成为默认配置:

<binding maxBufferPoolSize="2147483647".....

或者在配置中显式定义一个端点,并将绑定配置分配给端点的bindingConfiguration属性。

<services>
    <endpoint address="" 
              binding="basicHttpBinding"
              bindingConfiguration="BasicHttpBinding_IService"     
              contract="ServiceReference1.IService"  />
</services>

如果您创建自定义绑定,例如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" />

解决此问题并更好地查看web.config文件的另一种方法是使用"Microsoft服务配置编辑器"(C:''Program Files(x86)''Microsoft SDKs''Windows''v8.1A''bin''NETFX 4.5.1 Tools''SvcConfigEditor.exe)编辑web.config文件

如果未指定绑定并且httpsgetenabled为true,则可能需要在服务应用程序的web.config 中设置基本httpsbinding

<bindings>
  <basicHttpsBinding>
    <binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
      <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    </binding>
  </basicHttpsBinding>

我收到了这个错误。我发现我已经创建了绑定配置,但未能将端点设置为该配置。因此,端点使用了具有默认设置的未命名绑定配置。因此,最大接收消息大小为65k。

这也可能是由于IIS限制。IIS管理器->选择您的应用程序->功能Veiw->请求筛选->编辑功能设置->最大允许内容长度30000000(28.6M)->增加值