对于void方法,不会调用AfterReceiveReply

时间:2015-03-17 10:05:19

标签: .net wcf iclientmessageinspector

我们要求在没有回复的情况下记录请求(如果超时)。 我们正在使用IClientMessageInspector的实现来执行此操作。不幸的是,第二种情况是未调用AfterReceiveReply - 当服务方法为void(不返回任何内容)时。有没有办法在BeforeSendRequest方法中识别void方法?

1 个答案:

答案 0 :(得分:0)

没有很好的方法可以这样做,但我找到了一个丑陋的方法:)

    /// <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;
    }