locked
How to determine if query is directly accessing entity RRS feed

  • Question

  • Hello,

    I have logic in my WCF data service that does some data partitioning by requiring that a customer id is always included in the querystring. I however have decided that I want to bypass this rule if the client is directly referencing a particular entity. So, for example, if somebody queries my service like this:

    http://myservice/myservice.svc/orders?$top=10

    They will receive an operation exceptiont hat states they must specify a CustomerId either in the filter or via a custom $customer parameter I have added support for. So, the following would work:

    http://myservice/myservice.svc/orders?$top=10&$customer=12345

    I would like to be able to bypass this rule (that lives in a IDispatchMessageInspector implementation) if the client is directly accessing a particular entity instance (a given order in the example provided). So, if the query is:

    http://myservice/myservice.svc/orders(10)

    I do not want them to have to provide the CustomerID. I could do a RegEx on the url and look for number in parens but that can become very tricky with more complex scenarios. Is there another way to access this information at this level (the IDispatchMessageInspector implementation)?

    Any help is greatly appreciated.

    Best,

    Ryan

    Monday, March 21, 2011 5:58 PM

Answers

  • Ryan, I agree that using IDispatchMessageInspector is a good way to go.  

    What you request about detecting whether the entity is referenced directly would be really hard to realize.   

    • Marked as answer by rkbrown Monday, March 28, 2011 4:21 PM
    Thursday, March 24, 2011 8:50 AM

All replies

  • How about: In your Application_BeginRequest(), try fetching the arguments from query string. For instance like:

    private void Application_BeginRequest( object sender, EventArgs e )
    {
      var top = Request.QueryString["$top"];
      if(null != top)
      {
        DoSomething(top);
      }
    }
    
    

    http://izlooite.blogspot.com
    Tuesday, March 22, 2011 6:54 AM
  • Kamran,

    Thanks for the reply. This is not a bad alternative to IDispatchMessageInspector. However, my real question surrounds determining if an entity is being referenced directly. So, what I need is a way to determine if a query contains an id like this http://myservice/myservice.svc/orders(10). Just checking the querystring does not really accomplish this for me unfortunately since you can have a query like http://myservice/myservice.svc/orders(10)/Items?$filter=ItemName eq 'Widget' that has the direct reference as well as a filter.

    If all else fails I can just do a regex. However, I am afraid there will be unforseen circumstances that make this invalid. (Just thinking of some of the string functions off the top-of-my-head)

    Thanks,

    Ryan

    Wednesday, March 23, 2011 9:23 PM
  • Ryan, I agree that using IDispatchMessageInspector is a good way to go.  

    What you request about detecting whether the entity is referenced directly would be really hard to realize.   

    • Marked as answer by rkbrown Monday, March 28, 2011 4:21 PM
    Thursday, March 24, 2011 8:50 AM