The question is in AfterReceiveRequest how to find out the custom attribute set on the Operation using the OperationDescription? If there's a way, is it better to set the custom attribute on operation declaration in service contract interface or the service implementation class?
To illustrate the question further:
public interface IGetterSetterService
{
[OperationContract, GetterRequest]
Data[] GetData();
[OperationContract, SetterRequest]
bool SetData(string Data);
}
OR
[WebInvoke(Method = "*", ResponseFormat = WebMessageFormat.Json, UriTemplate = "xyz"]
[GetterRequest]
public Data[] GetData()
{
return new Data[];
}
[WebInvoke(Method = "*", ResponseFormat = WebMessageformat.Json, UriTemplate = "xyz/{data}"]
[SetterRequest]
public bool SetData(string data)
{
return true;
}
Now the IDispatchMessageInspector:
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
//Here how to find out the GetterRequest or SetterRequest custom attribute set on an
//operation, may be using OperationDescription for the current context?
}
My complete solution looks like this and it works without any problem:
1. First get the operation description as discussed here
2. Then find the custom attributes set on Operations in Service
interface:
private UserAction GetIntendedUserAction(OperationDescription opDesc)
{
Type contractType = opDesc.DeclaringContract.ContractType;
var attr = contractType.GetMethod(opDesc.Name).GeCustomAttributes(typeof(RequestedAction), false) as RequestedAction[];
if (attr != null && attr.Length > 0)
{
return attr[0].ActionName;
}
else
{
return UserAction.Unknown;
}
}
public enum UserAction
{
Unknown = 0,
View = 1,
Control = 2,
SysAdmin = 3,
}
[AttributeUsage(AttributeTargets.Method)]
public class RequestedAction : Attribute
{
public UserAction ActionName { get; set; }
public RequestedAction(UserAction action)
{
ActionName = action;
}
}