1

假设我使用以下合同实现了 WCF REST服务。

[ServiceContract]
interface INotesService
{
    [OperationContract]
    [WebInvoke(Method = "GET",
        UriTemplate = "notes/{id}")]
    Note GetNote(string id);

    [OperationContract]
    [WebInvoke(Method = "GET",
        UriTemplate = "notes")]
    IEnumerable<Note> GetNotes();
}

现在,我在管道中有一个 HttpModule 来进行授权,但是该代码需要知道请求将被分派到哪个方法。如何找到将由 WCF 调用的方法的签名?

4

2 回答 2

1

尽管另一个答案让我走上了正确的道路,但它并没有真正回答我的问题。

后来我发现这个链接给了我一个可行的解决方案: http: //tech.blog.oceg.org/2009/04/authorizing-rest-calls-in-wcf.html

但是,我发现它比需要的更复杂。在 .NET 4.5(这就是我正在使用的)中,您可以执行以下操作。

我从 ServiceHost.ApplyConfiguration 覆盖注册了我的 ServiceAuthorizationManager。

this.Authorization.ServiceAuthorizationManager = 
       new MyServiceAuthorizationManager();

然后,在它的 CheckAccessCore 方法中,我调用了下面的方法,为我提供了请求将被分派到的方法的名称。

private string GetOperationName(OperationContext operationContext)
{
    return messageProperties["HttpOperationName"] as string;
}
于 2013-04-20T23:48:35.443 回答
1

我认为您应该使用IDispatchOperationSelector

另请参阅本文:WCF 可扩展性 - 操作选择器

于 2013-04-11T04:47:57.423 回答