消息大小超过64K时,WCF服务的HTTP 400错误请求错误

时间:2011-12-23 01:45:24

标签: wcf http service

我有一个.NET 4.0 WCF服务。如果我发送大于64K的soap消息,那么我得到“远程服务器返回意外响应:(400)错误请求”错误。它一直很好,直到我超过64K消息大小。我已经阅读了很多关于如何处理此错误的帖子,据我所知,我有正确的web.config值,但我仍然得到错误。以下是我的web.config中的设置。我错过了什么?在与本地ASP.NET VS服务器和远程Windows 2008 R2 IIS服务器进行通信时会发生这种情况。有没有办法实时或在调试器中验证或记录服务绑定中的maxReceivedMessageSize设置等?如果这有任何不同,该服务将托管在MVC中。

<httpRuntime  maxRequestLength="50000000" />

...

    ...

<bindings>
  <basicHttpBinding>

    <binding name="IpsApiBinding" receiveTimeout="00:15:00" sendTimeout="00:05:00" maxReceivedMessageSize="40000000">
      <readerQuotas maxDepth="5000000" maxStringContentLength="50000000"
        maxArrayLength="50000000" maxBytesPerRead="50000000" />
    </binding>

  </basicHttpBinding>
</bindings>


<services>

  <service behaviorConfiguration="ApiBehavior" name="IPSApi.IpsApi">
    <endpoint behaviorConfiguration="endpointBehavior" binding="basicHttpBinding"
      bindingConfiguration="IpsApiBinding" name="IPSApi.IpsApi"
      contract="IPSApi.IIPSApi" />
  </service>

</services>

<behaviors>

  <endpointBehaviors>
    <behavior name="endpointBehavior">
      <dataContractSerializer maxItemsInObjectGraph="6553600" />
      <callbackDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </endpointBehaviors>

  <serviceBehaviors>
    <behavior name="ApiBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>

</behaviors>

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"  />

在客户端,堆栈轨道显示...

  

服务器堆栈跟踪:at   System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest的   请求,HttpWebResponse响应,HttpChannelFactory工厂,   WebException responseException,ChannelBinding channelBinding)at   System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度   超时)at   System.ServiceModel.Channels.RequestChannel.Request(消息消息,   TimeSpan超时)at   System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息   消息,TimeSpan超时)at   System.ServiceModel.Channels.ServiceChannel.Call(String action,   Boolean oneway,ProxyOperationRuntime操作,Object [] ins,   对象[]出局,TimeSpan超时)at   System.ServiceModel.Channels.ServiceChannel.Call(String action,   Boolean oneway,ProxyOperationRuntime操作,Object [] ins,   对象[]出局)   System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage   methodCall,ProxyOperationRuntime operation)at   System.ServiceModel.Channels.ServiceChannelProxy.Invoke(即时聊天   消息)在[0]处重新抛出异常:at   System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(即时聊天   reqMsg,IMessage retMsg)...

3 个答案:

答案 0 :(得分:3)

我发现了我的问题。这是详细信息。我在MVC应用程序中托管我的WCF服务。 WCF服务位于单独的DLL中。我在web.config中有配置设置,认为它会从那里读取它的配置。快来找出,如果在一个单独的DLL中,那么它将不会使用web.config设置。它只是使用默认绑定等。

为了让它读取web.config中的配置,我使用了http://blogs.msdn.com/b/dotnetinterop/archive/2008/09/22/custom-service-config-file-for-a-wcf-service-hosted-in-iis.aspx中建议的方法。我创建了自己的自定义ServiceHostFactory和ServiceHost。在ServiceHostFactory.CreateServiceHost中,我创建了一个ServiceHost(派生类)的实例。在我的ServiceHost派生类中,我接管了ApplyConfiguration并从web.config加载配置。

有效!

答案 1 :(得分:0)

我有一个类似的问题,并且还必须增加绑定上的两个额外字段,我在你的代码中没有看到... maxBufferSize和maxBufferPoolSize。不确定这是否是你的问题,但我知道这些的默认值大约是64k。

示例Web.config设置:

<binding name="HTTPNoneBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
      receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false"
      bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
      messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
      useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
        maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />

答案 2 :(得分:0)

我建议您在服务上启用跟踪,以确定错误请求的确切原因。如果是因为消息大小,框架会清楚地告诉您需要设置哪个属性。为了启用跟踪,请遵循此link