Reputation: 65
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.
Upvotes: 0
Views: 1369
Reputation: 65
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;
}
}
Upvotes: 1