不为 void 方法调用 AfterReceiveReply

AfterReceiveReply is not invoked for void methods

我们要求记录没有回复的请求(以防超时)。 我们正在使用 IClientMessageInspector 的实现来做到这一点。不幸的是,在第二种情况下,AfterReceiveReply 没有被调用——当服务方法为 void(没有 return 任何东西)时。有什么方法可以识别 BeforeSendRequest 方法中的 void 方法吗?

没有漂亮的方法可以做到这一点,但我发现了一个丑陋的方法:)

    /// <summary>
    /// Checks internal operation formatter for action reply attribute which is empty for one way methods.
    /// Based on above returns whether service method will have reply or not.
    /// </summary>
    /// <param name="request">Request message.</param>
    /// <returns>Whether service method will have reply.</returns>
    private bool WillRequestHaveReply(Message request)
    {
        FieldInfo operationFormatterField =
            request.GetType()
                .GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
                .FirstOrDefault(f => f.Name == "operationFormatter");

        if (operationFormatterField != null)
        {
            object operationFormatter = operationFormatterField.GetValue(request);
            if (operationFormatter != null)
            {
                PropertyInfo actionReplyProperty =
                    operationFormatter.GetType()
                        .GetProperties(BindingFlags.Instance | BindingFlags.NonPublic)
                        .FirstOrDefault(p => p.Name == "ReplyAction");

                if (actionReplyProperty != null)
                {
                    return actionReplyProperty.GetValue(operationFormatter) != null;
                }
            }
        }

        // Every request should have operationFormatter inside.
        // Use standard behaviour (requests with replies) if it doesn't.
        return true;
    }