0

我正在编写一个自定义 ServiceBehavior,它希望我知道请求消息的类型,以推断消息是否用自定义属性装饰。

我的示例合同可能如下所示:

    [DataContract]
[Auditable]
public class CompositeType
{
    bool boolValue = true;
    string stringValue = "Hello ";

    [DataMember]
    [Audit]
    public bool BoolValue
    {
        get { return boolValue; }
        set { boolValue = value; }
    }

    [DataMember]
    [Audit]
    public string StringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }
}

我试图通过使用来识别行为方面的自定义属性:

public object AfterReceiveRequest(ref Message request, IClientChannel channel,
    InstanceContext instanceContext)
{
    var typeOfRequest = request.GetType();

    if (!typeOfRequest.GetCustomAttributes(typeof (AuditableAttribute), false).Any())
    {
        return null;
    }
}

但是typeOfRequest总是作为{Name = "BufferedMessage" FullName = "System.ServiceModel.Channels.BufferedMessage"}

有没有办法可以通过使用请求来推断消息的类型?

注意:我直接引用了包含合同的程序集,并且服务不是通过 wsdl 引用的。

4

1 回答 1

1

上述问题的解决方案是不使用 MessageInspector(如IDispatchMessageInspectorIClientMessageInspector),而是使用参数 Inspector(如IParameterInspector)。

在 BeforeCall 方法中,我们可以执行以下操作:

public object BeforeCall(string operationName, object[] inputs)
{

        var request = inputs[0];

        var typeOfRequest = request.GetType();

        if (!typeOfRequest.GetCustomAttributes(typeof(CustomAttribute), false).Any())
        {
            return null;
        }
}
于 2014-03-25T16:25:06.820 回答