【问题标题】:Find which method WCF will dispatch a RESTful request to查找 WCF 将向哪个方法分派 RESTful 请求
【发布时间】:2013-04-02 21:47:37
【问题描述】:

假设我使用以下合同实施 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 调用的方法的签名?

【问题讨论】:

    标签: wcf wcf-rest method-dispatch


    【解决方案1】:

    我认为你应该使用IDispatchOperationSelector

    另见这篇文章:WCF Extensibility – Operation Selectors

    【讨论】:

    • 这看起来很有希望,谢谢!如果对我有用,我会尝试并将此答案标记为已接受。
    【解决方案2】:

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

    我后来发现这个链接给了我一个可行的解决方案: 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-02-17
      • 1970-01-01
      • 2013-03-27
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多