Question

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?

Was it helpful?

Solution

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. Adding ToList() solves that problem but, as you pointed out, does so by loading everything into memory first so is less than ideal.

OTHER TIPS

I don't believe it can be exposed over WCF, this may explain further

Expose IQueryable Over WCF Service

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top