要在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获取参数类型吗?
答案 0 :(得分:0)
在WCF中,继承的概念仅限于接口,您不能在WCF中的服务类级别使用继承的概念。请参阅http://www.codeproject.com/Questions/302481/WCF-Service-Inharitance