简体   繁体   中英

How to get type(as in request.GetType) of a request message in Service Behavior of WCF Service

I am writing a custom ServiceBehavior which expects me to know the Type of the request message to infer if the message is decorated with a custom attribute.

My Sample Contract could look like:

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

I am trying to identify the custom attribute on the behavior side by using :

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

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

But the typeOfRequest is always coming in as a {Name = "BufferedMessage" FullName = "System.ServiceModel.Channels.BufferedMessage"}

Is there a way that I can infer the type of a message by using the request ?

Note: I have a direct reference to the assembly which holds the contract and service is not referred through wsdl.

The solution to the above problem is not to use a MessageInspector (as in IDispatchMessageInspector or IClientMessageInspector ) instead use a parameter Inspector (as in IParameterInspector ).

In the BeforeCall Method we can do something like:

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

        var request = inputs[0];

        var typeOfRequest = request.GetType();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM