Jake
Jake

Reputation: 1781

What is the recommended way to return a large collection from a WCF service?

In my application, I want to return a collection of objects from a WCF Service (hosted as a Windows Service) to populate a DataGrid in a WPF application. The amount of objects in the collection ranges from one to several hundred depending on the method called.

I'm curious as what is the 'best' way to deal with returning large collections from a service.

These are the options I've seen suggested are:

What would be the best way approach this task, and why?

Upvotes: 2

Views: 1302

Answers (1)

Ouarzy
Ouarzy

Reputation: 3043

Increasing the max message size and returning all the objects in one go. This seems like a bad idea because there could possibly come a time when I need to return more than 2GB of data.

Definitely not the good choice, unless your're sure that your data will never exceed the new limit you set. Otherwise you will just push the problem back and have it again in a few months. 2Gb is already a lot by the way (think how long your user will wait)

Paginating the records and calling the method repeatedly until all objects have been retrieved. I've seen this one suggested for ASP.NET projects but I don't know how well it would work for desktop apps.

The most common and obvious approach, you can use pagination and only query for a defined number of elements on each page. I can't understand your question about "desktop apps" though? The relevant concept here is client/server. Your client (desktop app) need to query your server for the content of a page (if you use pagination) to display. If your client was a web page, the concept would still be relevant.

Using a stream. To be honest, I don't understand how this works since it appears to be meant for transferring large single objects rather than many smaller ones.

I guess you read things like "manage you own stream". In a few words you could consider any stream as a bit flow, and just interpret it as you wish in your client side. I would certainly not recommend that, unless you have really specific transfer issue (and having a high number of object to transfer is certainly not specific enough). Having a few very big object to transfer may be specific enough, but even here I would challenge the implementation before going this way.

Doing something with the yield keyword, but this went over my head and I couldn't follow it. :-/

Sorry I don't follow you here, but yield is only syntaxic sugar so I don't think it's relevant to solve your problem. Still have a look to understad the concept: What is the yield keyword used for in C#?

Upvotes: 5

Related Questions