要自定义WCF服务器中的授权,我重写ServiceAuthorizationManager.CheckAccessCore()。在这里我需要使用OperationContext定位客户端调用的方法。我发现在这个优秀的后部分解决方案:WCF: Retrieving MethodInfo from OperationContextWCF从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);
当你好( “Jake”)被调用,operationContext.IncomingMessageHeaders.Action提供方法名称“Hello”,同时我也需要参数类型来获取正确的方法。 (hostType.GetMethod(operation.Name)引发一个AmbiguousMatchException)
我可以从OperationContext获取参数类型吗?
基类不是WCF服务。它只是WCF服务类的基类。由于它造成了误解,我从这个问题中消除了继承的噪音。希望现在很清楚。 – sz9
我已经更新了我的答案。请检查。 –
在我的方案中,一个重载的方法被指定为服务操作,而另一个不是。我可以重新组织代码以避免服务类中的重载方法,但它会引入其他复杂性,如果可能,我宁愿避免它。 – sz9