Access MethodInfo in the implementation of IOperationInvoker

I implemented IOperationInvoker to configure WCF activation. In the Invoke method, I want to access the user attributes of the method that OperationInvoker calls. I wrote the following code. But this does not give the custom attributes that are specified in this method.

public MyOperationInvoker(IOperationInvoker operationInvoker, DispatchOperation dispatchOperation) { this.operationInvoker = operationInvoker; } public object Invoke(object instance, object[] inputs, out object[] outputs) { MethodInfo mInfo=(MethodInfo)this.operationInvoker.GetType().GetProperty("Method"). GetValue(this.operationInvoker, null); object[] objCustomAttributes = methodInfo.GetCustomAttributes(typeof(MyAttribute), true); } 
+4
source share
1 answer

At run time, OperationInvoker is of type SyncMethodInvoker, which contains MethodInfo. But due to its level of protection, we cannot use OperationInvoker for SyncMethodInvoker. However, there is a MethodInfo object in OperationDescription. So, what I usually do is pass MethodInfo in the IOperationBehavior.ApplyDispatchBehavior method to the CustomOperationInvoker constructor.

 public class OperationBehaviourInterceptor : IOperationBehavior { public void ApplyDispatchBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation) { MethodInfo currMethodInfo = operationDescription.SyncMethod; var oldInvoker = dispatchOperation.Invoker; dispatchOperation.Invoker = new OperationInvokerBase(oldInvoker,currMethodInfo); } // other method } public class CustomOperationInvoker : IOperationInvoker { private IOperationInvoker oldInvoker; private MethodInfo methodInfo; public CustomOperationInvoker(IOperationInvoker oldOperationInvoker, MethodInfo info) { this.oldInvoker = oldOperationInvoker; this.methodInfo = info; } // then you can access it } 
+4
source

All Articles