To customize authorization in WCF server, I override ServiceAuthorizationManager.CheckAccessCore(). In it I need to locate the method called by client using OperationContext. I found partial solution in this excellent post: WCF: Retrieving MethodInfo from OperationContext
My case (simplified) is as below:
[ServiceContract]
public interface IMyService
{
[OperationContract]
void Hello(string name);
}
public Class MyService : IMyService
{
// this method is not part of service contract
public void Hello()
{
Console.WriteLine("Hello World!");
}
public void Hello(string name)
{
Console.WriteLine(string.Format("Hello {0}!", name);
}
}
The code to get MethodInfo from above post is:
string action = operationContext.IncomingMessageHeaders.Action;
DispatchOperation operation =
operationContext.EndpointDispatcher.DispatchRuntime.Operations.FirstOrDefault(o =>
o.Action == action);
Type hostType = operationContext.Host.Description.ServiceType;
MethodInfo method = hostType.GetMethod(operation.Name);
When Hello("Jake") is called, operationContext.IncomingMessageHeaders.Action provides method name "Hello", while I also need the parameter types to get the right method. (hostType.GetMethod(operation.Name) throws an AmbiguousMatchException)
Can I get parameter types from OperationContext?
In WCF the concept of inheritance is limited to interfaces, You can't use the concept of inheritance at service class level in WCF. Please refer http://www.codeproject.com/Questions/302481/WCF-Service-Inharitance