asp.net - WCF 响应太长 - 身份验证错误

标签 asp.net wcf windows-authentication communicationexception

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

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

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

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

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

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

直到我达到另一个限制(我认为),在 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 设置有限制吗?

最佳答案

我猜测您正在使用 Windows 身份验证,因此会出现 401,而不是一条解释您如何超出消息限制的消息。当您通过 Windows 身份验证请求发送时,WCF 会发送 SOAP 请求两次,一次是失败并返回 Accept header ,第二次是使用 Windows 身份验证 header 发送。

但是,根据我的测试,如果消息确实发送成功,您仍然会收到 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>

关于asp.net - WCF 响应太长 - 身份验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2055631/

相关文章:

c# - 无法为表中的标识列插入显式值

asp.net - 在 Linux 上部署 .Net Web 应用程序

c# - WCF REST - 覆盖传入的请求数据

asp.net - Windows 身份验证多次请求

iis - 本地 iis 上的 asp.net core windows 身份验证

asp.net - IIS7 为另一台服务器上的文件创建虚拟目录

javascript - 检查 3 个文本框是否为空的最佳方法

visual-studio-2008 - Windows 7 x64 上的 WCF 调试

linux - MONO WCF 自托管服务对在使用 net.tcp 时由对等方重置连接

jquery - Active Directory 身份验证在 IPAD(Safari 浏览器)上不起作用