1

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?

Merenzo
  • 5,326
  • 4
  • 31
  • 46
  • 4
    What's an "HTTP 12152 error"? – Gabe Feb 14 '12 at 06:05
  • ERROR_WINHTTP_INVALID_SERVER_RESPONSE 12152 The server response cannot be parsed. -- Starting with Windows Server 2003, and Windows XP with SP2, the maximum amount of header data WinHTTP accepts in an HTTP response is 64K, by default. If the server HTTP response contains more that 64K of total header data, WinHTTP fails the request with an ERROR_WINHTTP_INVALID_SERVER_RESPONSE error. – dtb Feb 14 '12 at 06:10
  • 1
    Why do you dispose your unit-of-work before executing the query? That doesn't look right to me. – CodesInChaos Feb 14 '12 at 12:50

2 Answers2

3

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.

Maurice
  • 27,582
  • 5
  • 49
  • 62
  • Perfecto. Well, at first glance anyway :) I got rid of the unnecessary `using`, and the `ToList()`, and now the service works as expected. SQLProfiler shows that deferred execution is happening, i.e. it is honouring `$top=1` rathering than pulling in the whole table. – Merenzo Feb 15 '12 at 01:52
0

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

Expose IQueryable Over WCF Service

Community
  • 1
  • 1
TheRealTy
  • 2,409
  • 3
  • 22
  • 32