实施“Ping”使用 Message Inspector 的功能会导致 WCF 运行时抛出 NullReferenceException

发布于 2024-08-12 18:38:03 字数 974 浏览 4 评论 0原文

我正在使用 WCF 来实现 Web 服务。此 Web 服务需要“ping”功能作为每个服务的运行状况监视器。此功能已使用 IDispatchMessageInspector 实现,并为服务的每个端点进行配置。这是由于业务要求“ping”尽可能接近实际的服务代码。同时,我不想将它与每个服务实现的代码联系起来,IDispatchMessageInspector 似乎很合适。

该服务使用请求-答复 MEP。每个请求消息都包含一个指定需要进行哪些处理的元素。然后,服务将使用该值来确定如何处理消息中的数据。相同的元素用于将请求消息定义为“心跳”检查。

“ping”消息检查器将在 AfterReceiveRequest() 方法中预处理请求消息,如果它确定该请求是“心跳”,它将生成正确的响应并通过从 AfterReceiveRequest() 返回的关联对象。 AfterReceiveRequest() 的请求消息参数(通过引用)随后被设置为 null,以防止服务实现代码处理该消息。

将请求消息设置为 null 的技术是在一个网站或博客中发现的,我不记得也找不到其 URL。这项技术本身效果很好,如果是“心跳”请求,我可以阻止执行服务实现代码。

不幸的是,在消息检查器中将请求消息设置为 null 将导致 WCF 运行时始终抛出 NullReferenceException。从堆栈跟踪中,我收集运行时仍会将消息对象(在通过“Ping”消息检查器后将为 null)传递给调度程序,并且当调度程序尝试反序列化 null 消息对象时,会导致 NullReferenceException。

但是,我的系统还实现了 IErrorHandler 来捕获服务中任何未处理的异常并将其记录下来。这意味着每个成功的“心跳”请求都将生成 NullReferenceException 的日志条目,并且“心跳”可能每分钟频繁一次。

问题:

当“Ping”通过将请求设置为 null 来阻止服务实现代码运行时,我可以采取什么措施来防止记录“无用”NullReferenceException。

非常感谢。
~汞

I'm using WCF to implement a web service. This web service requires a 'ping' feature as a health monitor for each service. This functionality has been implemented using IDispatchMessageInspector and is configured for each endpoint of a service. This is due to a business requirement for the 'ping' to be as near the actual service code as possible. At the same time, I did not want to tie it to each service implementation's code and IDispatchMessageInspector seems to be a good fit.

The service uses a Request-Reply MEP. Each request message contains an element that specifies what processing is required. The service will then use this value to determine how to process the data in the message. The same element is used to define a request message as a 'heartbeat' check.

The 'ping' message inspector will pre-process a request message in the AfterReceiveRequest() method and if it determines the request is a 'heartbeat', it will then generate the correct response and pass that on to the BeforeSendReply() method via a correlation object returned from AfterReceiveRequest(). The request message parameter of AfterReceiveRequest(), which is by reference, is then set to null to prevent the message from being processed by the service implementation code.

The technique of setting request message to null was found in a web site or blog which I can't remember nor find the URL for. This technique works great on it's own and I can prevent service implementation code from being executed if it's a 'heartbeat' request.

Unfortunately, setting the request message to null in a message inspector will cause the WCF runtime to always throw a NullReferenceException. From the stack trace, I gather the runtime will still pass the message object (which will be null after going through 'Ping' message inspector) to the dispatcher and when the dispatcher tries to deserialise a null message object, causes the NullReferenceException.

However, my system also implements IErrorHandler to catch any unhandled exceptions in the service and log it. This means every successful 'heartbeat' request will generate a log entry for the NullReferenceException and the 'heartbeat' could be as frequent as every minute.

The Question :

What can I do to prevent logging of 'useless' NullReferenceException thrown when 'Ping' prevents service implementation code from running by setting request to null.

Many thanks in advance.
~hg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南街女流氓 2024-08-19 18:38:03

不是最优雅的解决方案,而是潜在的解决方法(我没有测试过),但是当您在检查器代码中检测到 ping 调用时,您是否不能抛出自己的自定义异常类型(即 PingRequestException),并在它返回到客户端时处理它?这是否可以避免您访问 WCF 运行时代码,从而避免记录未处理的异常。

否则,您可以尝试使用由所有服务(wcf 运行时代码的另一端)继承的基本服务,该服务在调用实际服务代码之前检测并处理构造函数中的 ping 请求。

Not the most graceful solution but potential workarounds (that i've not tested), but where you detect your ping calls in the inspector code, could you not throw your own custom exception type i.e. PingRequestException, and handle this when it returns to the client? Would that avoid you hitting the WCF Runtime code, thus avoiding the logging of unhandled exceptions.

Otherwise you could try to use a base service, inherited by all of your services (the other side of the wcf runtime code) that detects and handles ping requests in the constructor before hitting the actual service code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文