IQueryable WebAPI 服务:为什么我需要调用 .ToList()?
我从上周离开的开发人员那里继承了这段代码。他的代码基于以下文章:
“Restful WCF / EF POCO / UnitOfWork / Repository / MEF”。
此方法有效(当我浏览到 http://myapp/myservice/Returns 时):
[WebGet(UriTemplate = "Returns")]
public IQueryable<ReturnSnapshot> GetReturnSnapshots()
{
using (UnitOfWork)
{
ReturnSnapshotsRepository.EnrolInUnitOfWork(UnitOfWork);
return ReturnSnapshotsRepository.FindAll().ToList().AsQueryable();
}
}
但不会 < code>ToList() 会导致整个表从存储库中提取吗?我们将生产 500K+ 行。
我想我可以将最后一行更改为:
return ReturnSnapshotsRepository.FindAll();
因为 FindAll
返回 IQueryable。然而,我的更改破坏了服务,该服务现在因 HTTP 12152 错误而崩溃。
我应该做什么?
I inherited this code from a developer who left last week. His code is based on the article:
"Restful WCF / EF POCO / UnitOfWork / Repository / MEF".
This method works (when I browse to http://myapp/myservice/Returns):
[WebGet(UriTemplate = "Returns")]
public IQueryable<ReturnSnapshot> GetReturnSnapshots()
{
using (UnitOfWork)
{
ReturnSnapshotsRepository.EnrolInUnitOfWork(UnitOfWork);
return ReturnSnapshotsRepository.FindAll().ToList().AsQueryable();
}
}
but won't the ToList()
will cause the whole table to be pulled from the repository? We'll have 500K+ rows in production.
I thought I could change the last line to this:
return ReturnSnapshotsRepository.FindAll();
as FindAll
returns IQueryable. However, my change breaks the service, which now craps out with an HTTP 12152 error.
What should I be doing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够从 WCF Web API REST 服务返回
IQueryable
。我怀疑 using 块导致了您的问题,因为一旦您从此方法返回,但在执行实际的数据库查询之前,UnitOfWork 就会被释放。添加ToList()
解决了这个问题,但是,正如您所指出的,通过首先将所有内容加载到内存中来实现这一点,因此不太理想。You should be able to return an
IQueryable<T>
from a WCF Web API REST service. I suspect the using block is causing your problem because the UnitOfWork is being disposed as soon as you return from this method but before the actual database query can be executed. AddingToList()
solves that problem but, as you pointed out, does so by loading everything into memory first so is less than ideal.我不相信它可以通过 WCF 公开,这可能会进一步解释
通过 WCF 服务公开 IQueryable< /a>
I don't believe it can be exposed over WCF, this may explain further
Expose IQueryable Over WCF Service