简体   繁体   中英

Find which method WCF will dispatch a RESTful request to

Suppose I implement a WCF REST service with the following contract.

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

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

Now, I have an HttpModule in the pipeline to do the authorization, but that code needs to know to which method the request will be dispatched. How do I find the signature of the method that will be invoked by WCF?

I think you should use IDispatchOperationSelector .

See also this article: WCF Extensibility – Operation Selectors

Even though the other answer put me on the right path, it did not really answer my question.

I later found this link which gave me a working solution: http://tech.blog.oceg.org/2009/04/authorizing-rest-calls-in-wcf.html

However, I found it to be more complex than needed. In .NET 4.5 (which is what I'm on) you can do the following.

I registered my ServiceAuthorizationManager from the ServiceHost.ApplyConfiguration override.

this.Authorization.ServiceAuthorizationManager = 
       new MyServiceAuthorizationManager();

Then, in its CheckAccessCore method, I called the method below to give me the name of the method to which the request will be dispatched.

private string GetOperationName(OperationContext operationContext)
{
    return messageProperties["HttpOperationName"] as string;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM