0

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.

4
  • Does your service use SOAP?
    – lockstock
    Mar 24, 2014 at 22:34
  • Yes it uses the SOAP Messages to communicate.
    – Dibyendu
    Mar 24, 2014 at 22:52
  • You can inspect the SOAP message to determine what objects are serialized within the request. But why is it that you want to determine the type defined within the message before it has been de-serialized by the service?
    – lockstock
    Mar 24, 2014 at 23:35
  • As stated in the problem statement I wanted to read only certain values of the contracts which are decorated by the custom attribute.
    – Dibyendu
    Mar 25, 2014 at 16:20

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.