2

起初,我想如果我添加了IDispatchMessageInspector的实现,也许这会在对限制进行任何检查之前拦截消息大小。我很快发现,事实并非如此。正在运行的服务显然会确保在较低级别上执行此规则。现在我很好奇是否有一种方法可以在服务器级别捕获此类异常并将响应返回给客户端。将IErrorHandler实现为 ServiceBehavior 可以解决问题吗?

也许更普遍的问题是:这可以在服务器级别进行跟踪吗?

值得注意的是,我无法控制 WSDL。

4

2 回答 2

1

消息大小在较低级别(传输级别)进行验证。

没有一种简单的方法可以捕获该异常(WCF 提供的用于处理异常的 IErrorHandling 接口在此级别不起作用)。

一种方法是创建您自己的自定义 wcf 传输通道(参见此处的示例)

请看下面超出配额时生成的异常的样子 - 您可以看到这是从低级别(HttpChannelListener)抛出的。

<ExceptionType>System.ServiceModel.ProtocolException, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
<Message>The maximum message size quota for incoming messages (225) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.</Message>
<StackTrace>
at System.ServiceModel.Channels.HttpInput.ThrowHttpProtocolException(String message, HttpStatusCode statusCode, String statusDescription)
at System.ServiceModel.Channels.HttpInput.ThrowMaxReceivedMessageSizeExceeded()
at System.ServiceModel.Channels.HttpInput.GetMessageBuffer()
at System.ServiceModel.Channels.HttpInput.ParseMessageAsyncResult.DecodeBufferedMessageAsync()
at System.ServiceModel.Channels.HttpInput.ParseMessageAsyncResult.BeginParse()
at System.ServiceModel.Channels.HttpInput.BeginParseIncomingMessage(HttpRequestMessage httpRequestMessage, AsyncCallback callback, Object state)
at System.ServiceModel.Channels.HttpPipeline.EmptyHttpPipeline.BeginParseIncomingMessage(AsyncCallback asynCallback, Object state)
at System.ServiceModel.Channels.HttpPipeline.EnqueueMessageAsyncResult..ctor(ReplyChannelAcceptor acceptor, Action dequeuedCallback, HttpPipeline pipeline, AsyncCallback callback, Object state)
at System.ServiceModel.Channels.HttpPipeline.EmptyHttpPipeline.BeginProcessInboundRequest(ReplyChannelAcceptor replyChannelAcceptor, Action dequeuedCallback, AsyncCallback callback, Object state)
at System.ServiceModel.Channels.HttpChannelListener`1.HttpContextReceivedAsyncResult`1.ProcessHttpContextAsync()
at System.ServiceModel.Channels.HttpChannelListener`1.BeginHttpContextReceived(HttpRequestContext context, Action acceptorCallback, AsyncCallback callback, Object state)
at System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result)
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state)
at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
</StackTrace>
于 2012-12-16T11:10:46.173 回答
0

您能做的最好的事情(以及监控这一点的常用方法)是在消息级别启用日志记录。 http://geekswithblogs.net/mnf/archive/2008/10/03/use-wcf-message-logging.aspx

问题是必须在客户端和服务器上设置 maxMessageSize 属性。因此,如果消息太大而无法在客户端发送或接收,您的服务器将不会知道这一点。

于 2012-12-15T10:31:21.113 回答