3

在我目前工作的项目中,公开了一个 WCF 服务,它返回一个业务实体的数组,我们称之为 Invoice :

Invoice[] GetInvoicesByTypeAndTime(InvoiceType invoiceType, byte startHour, byte? endHour);

使用的身份验证机制是 Windows 身份验证,WCF 服务托管在 IIS 6 上托管的 Web 应用程序中。

起初,当我用来获取超过 64kB 的数据时,会抛出 CommunicationException,指出“已超出传入消息的最大消息大小配额 (65536)。要增加配额,请在适当的绑定元素上使用 MaxReceivedMessageSize 属性。”

好吧,我只是在 App.config 中将 maxReceivedMessageSize 和 maxBufferSize 的值增加到 65536000(我在末尾公然添加了三个零)(后者是因为它在 ArgumentException 中抱怨“对于 TransferMode.Buffered,MaxReceivedMessageSize 和 MaxBufferSize 必须是相同的值。参数名称:bindingElement")。

现在我可以收到更大的回应......

直到我达到另一个限制(我认为),在 624 个元素(大约 2.2 MB)之后,抛出了一个奇怪的异常:

System.ServiceModel.Security.MessageSecurityException: The HTTP request is unauthorized with client authentication scheme 'Negotiate'. The authentication header received from the server was 'Negotiate,NTLM'. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized.
   at System.Net.HttpWebRequest.GetResponse()
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   --- End of inner exception stack trace ---

服务器堆栈跟踪:

   at System.ServiceModel.Channels.HttpChannelUtilities.ValidateAuthentication(HttpWebRequest request, HttpWebResponse response, WebException responseException, HttpChannelFactory factory)
   at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory factory, WebException responseException)
   at System.ServiceModel.Channels.HttpChannelFactory.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.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

在 [0] 处重新抛出异常:

   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at Test2.DBS.IDbService.GetInvoicesByTypeAndTime(InvoiceType invoiceType, Byte startHour, Nullable`1 endHour)
   at Test2.DBS.DbServiceClient.GetInvoicesByTypeAndTime(InvoiceType invoiceType, Byte startHour, Nullable`1 endHour) in D:\TEMP\Test2\Test2\Service References\DBS\Reference.cs:line 1445
   at Test2.Program.Main(String[] args) in D:\TEMP\Test2\Test2\Program.cs:line 19

对经过身份验证的响应有限制吗?ASP.NET 设置有限制吗?

4

2 回答 2

3

我猜您使用的是 Windows 身份验证,因此使用的是 401,而不是解释您如何突破消息限制的消息。当您通过 Windows Authenticated 请求发送时,WCF 发送 SOAP 请求两次,一次失败并返回接受标头,第二次使用 Windows 身份验证标头发送。

但是,根据我的测试,如果消息确实通过了,那么如果消息实际上会失败,您似乎仍然会收到 401。

为了解决这个问题,我不得不输入服务器跟踪日志:

<system.diagnostics>
    <trace autoflush="true" />
    <sources>
      <source name="System.ServiceModel" switchValue="Critical, Error, Warning">
        <listeners>
          <add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="C:\Logs\ServiceTrace.svclog"/>
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

然后,如上所述,我必须设置更大的读者配额(但我使用了较小的值):

然后,您通常必须放入自定义行为以增加对象图中的最大项目数:

<behaviors>
  <serviceBehaviors>
    <behavior name="MaximumItemsBehaviour">
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpsGetEnabled="true" httpGetEnabled="false" />
      <!-- 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><services><service>”元素添加一个“behaviourConfiguration”属性,其值为“MaximumItemsBehaviour”。

我读过但不需要自己的其他建议是添加:

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime maxRequestLength="2097151" />
  </system.web>

和:

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="209715200"/>
      </requestFiltering>
    </security>
  </system.webServer>
于 2011-12-19T18:49:40.990 回答
1

看看客户端的readerQuotas,如果你想要 TLDR 版本 - 看看这是否确实是你的问题,你可以设置最大值(Int32.MaxValue),如下所示。

<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
于 2010-01-13T18:02:06.083 回答