为了在 WCF 服务器中自定义授权,我重写了 ServiceAuthorizationManager.CheckAccessCore()。在其中我需要找到客户端使用 OperationContext 调用的方法。我在这篇出色的帖子中找到了部分解决方案:WCF: Retrieving MethodInfo from OperationContext
我的情况(简化)如下:
[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);
}
}
从上面的帖子中获取 MethodInfo 的代码是:
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);
调用 Hello("Jake") 时, operationContext.IncomingMessageHeaders.Action 提供方法名称“Hello”,而我还需要参数类型来获取正确的方法。(hostType.GetMethod(operation.Name) 抛出 AmbiguousMatchException)
我可以从 OperationContext 中获取参数类型吗?