Expose IQueryable Over WCF Service

点点圈 提交于 2019-11-27 05:34:28

You should check WCF Data Services which will allow you to define Linq query on the client. WCF Data Services are probably the only solution for your requirement.

IQueryable is still only interface and the functionality depends on the type implementing the interface. You can't directly expose Linq-To-Sql or Linq-To-Entities queries. There are multiple reasons like short living contexts or serialization which will execute the query so the client will get list of all objects instead of the query.

as far as i know, the datacontext is not serializable meaning that you cannot pass it around with WCF

You can use http://interlinq.codeplex.com/ which allows you to send Linq query over WCF.

WCF Data Services only can be using with webHttpBinding and not all Linq queries can be expressed. Writing queries when WCF Data Service is used is not so attractive - requires string expressions such as:

.AddQueryOption("$filter", "Id eq 100");

https://remotelinq.codeplex.com/ is another choice. But it works in AppDomain to scan the Current Assemblies and serialize them. This tech is not suitable to WinRT as no Domains for WinRT App

I've been struggling with the same question, and realized that a well formed question is a problem solved.

IQueryable basically serves to filter the query before sending it to your DB call so instead of getting 1000 records and filter only 10, you get those 10 to begin with. That filtering belongs to your Service Layer, but if you are building an API I would assume you would map it with AND/OR parameters in your URL.

http://{host}/{entity}/q?name=john&age=21.

So you end up with something like this:

Filter:Column1=Value1 > http://{host}/{entity}q?column1=value1 >  SELECT *
                                                                  FROM  Entity
                                                                  WHERE Column1=Value1

MVC                   > WCF                                    >  DB

You can find a very good sample [here]

Lastly, since your payload from the WCF will be most likely a JSON, you can (and should) then deserialize them in your Domain Models inside a collection. It is until this point where the paging should occur, so I would recommend some WCF caching (and since its HTTP, it's really simple). You still will be using LINQ on the WebApp side, just w/o "WHERE" LINQ clause (unless you want to dynamically create the URL expressed above?)

For a complex OR query, you mind end up with multiple WCF queries (1 per "AND") and then concatenate them all together

If it's possible to send IQuerable<> over WCF it's not a good thing security wise, since IQuerable<> could expose stuff like the connection string to the database. Some of the previous comments seem promising though.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!