在WCF终止客户端请求之前挂钩到WCF

时间:2012-02-02 00:42:49

标签: wcf

我想了解如何在发送错误的WCF请求时WCF返回错误之前捕获异常(日志请求url)。

例如,我使用Fiddler在下面使用错误的Action元素发布WCF请求:

<a:Action s:mustUnderstand="1">http://www.xxx.com/Wrong</a:Action>

WCF服务将以下错误返回给客户端并终止请求。如何捕获请求url,在WCF终止之前捕获异常。

从WCF返回异常:

http://www.w3.org/2005/08/addressing/faulturn:uuid:872ca27f-24c8-4855-8739-e36bd1d921e71e67f1d2-7550-4c41-88cf-acdf8ebd6bd4s:Sendera:ActionNotSupportedThe message with Action 'http://www.xxx.com/Wrong' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).


<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
  <s:Header>
    <a:Action s:mustUnderstand="1">http://www.xxx.com/Wrong</a:Action>

    </a:ReplyTo>
    <a:To s:mustUnderstand="1">http://www.xxx.com/Service.svc</a:To>
  </s:Header>
</s:Envelope>

请注意,当我使用fiddler时,实际请求网址可能与使用To元素的网址不同。

1 个答案:

答案 0 :(得分:1)

查看IErrorHandler界面。如果配置正确,所有异常都将通过它。我没有尝试过,但在HandleError中你可以记录你想要的任何内容。 OperationContext可以为您提供所需的数据。看看这个sample