使用 WCF 数据服务限制或修改 OData 源的查询
如果我使用 WCF 数据服务通过 OData 公开对象集合,并且我想防止用户执行可能过于复杂或消耗过多资源的查询,并且我能够在返回结果之前以某种方式挂钩到查询?
据我了解,如果用户在客户端执行 LINQ 查询,则会将其转换为包含所有查询参数的 REST URL,然后在服务器端完成查询。如果是这样,这就是我希望能够以某种方式挂钩并可能将它们限制为仅某些操作。
If I am exposing a collection of objects through OData using WCF Data Services, and I want to prevent the user from doing queries that may be too complex or consume too many resources, and I able to hook into the query somehow before the results are returned?
From what I understand, if a user does a LINQ query on the client side, this is converted to a REST URL with all the queries parameters, and then the query is done on the server side. If so, this is what I am wanting to be able to hook in somehow and possibly limit them to only certain operations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在服务器上,每个传入查询都会转换为 LINQ 表达式,然后针对数据上下文公开的 IQueryable 执行该表达式。您可以包装 IQueryable,检查要执行的 LINQ,如果发现它太复杂则失败。
我写了一系列关于该服务将生成的 LINQ 表达式树以及它们映射到哪些查询的博客文章。 http:// /blogs.msdn.com/b/vitek/archive/2010/02/25/data-services-expressions-part-1-intro.aspx
第二部分还有一个如何拦截查询的示例(那里的示例将其写出来,但您也可以在那里添加检查代码)。
On the server each incomming query is translated into a LINQ expression which is then executed against the IQueryable exposed by the data context. You can wrap the IQueryable, inspect the LINQ to be executed and fail if you find it too complex.
I wrote a series of blog posts about the LINQ expression trees which the service will generate and what queries they map to. http://blogs.msdn.com/b/vitek/archive/2010/02/25/data-services-expressions-part-1-intro.aspx
The second part also has a sample how to intercept the query (the sample there writes it out, but you can add your inspection code there as well).