views:

1823

answers:

3

What is the proper way to pass an answer (a collection) back to Silverlight?

For example, if I have a service application that sits on top of the Northwind sample database and the service has a method called GetEmployees(). What is the proper "thing" to pass back to Silverlight? An IQueryable ?

Then considering the Async/Result casting stuff on the Silverlight side what do I cast it too? An IQueryable ?

UPDATE:
Is it the declaration of [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] that allows a remote client to see the structs (entities) from a service?? I didn't know there was such thing as a Silverlight-friendly-WCF class so thats not what I started with. Once I added that attribute in the solution wouldn't let me use the service any more without it. So I could not test what I was seeing before. Any thoughts on what AspNetCompatibilityRequirementsMode.Allowed does under the hood?

UPDATE 2:
See comments to Terry Donaghe.

A: 

In using regular web services, you have to return collections like List the SOAP formatter to know what to do with it. However, I have not used WCF, so I don't know if that does things differently.

Jeff Yates
I figured that might be the case. When you use the classic ASMX web service with Silverlight does the SOAP wrapper incorporate your business objects as structs, the way it would in "full-blown" .NET?
tyndall
yes, it recreates my business objects on the client side.
Jeff Yates
A: 

Just return a List. If you can return a single T, then you can return a List<T>.

John Saunders
But how do I get my entities into Silverlight? My Silverlight app (unless I missing something) has no idea what an Employee or a List<Employee> is.
tyndall
When you add the service reference, it will create a proxy version of Employee. And Silverlight already knows about List<T>.
John Saunders
+2  A: 

Did you just try passing arrays of base types around? Unless you create DataContracts, WCF doesn't know what any of your objects are. When you try to pass a List WCF really just passes an array since it's designed to try to be inter-operable.

Define a DataContract to contain information about the Employees (or whatever). Try to keep it relatively simple. When you create your proxy (probably with svcutil or Add Service Reference) VS2008 will auto-magically define the DataContracts on your client side and then you can use them just like a regular object.

I prefer to use WCF manually - I create my own contract, implementation and proxy dlls. Doing that gives me a great deal of flexibility as far as de-serialization and other stuff is concerned. For more on that, see these two references:

WCF the Manual Way, the Right Way

Manual WCF - an Extension

Also, please see Chapter 3 of the WCF Bible, "Programming WCF Services" by Juval Lowy. It's chock full of info on DataContracts.

Terry Donaghe
I think I figured it out... List<Employee> gets creates an Employee struct on the client IQueryable<Employee> does not. So it depends on the generic class that you use whether the T class gets incorporated. It would be nice to see a list of which generic classes support this.
tyndall
I understand the DataContract stuff. I think with EDM-based classes you get that for "free". But I have manually had to do this for some NHibernate stuff I was doing the other day.
tyndall
+1 on the extra WCF info. Do you think the WCF Bible is the best book out there? I'll mainly be calling WCF over HTTP from Silverlight, WPF SmartClients, and Web Sites.
tyndall
For basic understanding of WCF, "Programming WCF" is by far the best book. You, however are going places that Juval Lowy doesn't i.e. System.ServiceModel.Web. The basics are the same, but we need a new book that focuses more on Silverlight and the Web. O'Reilly has some new ones coming out soon.
Terry Donaghe