views:

40

answers:

3

I have a class, ExpenseInfo that includes an ICollection<String> property:

public ICollection<String> WhoOwes { get; private set; }

I have a WCF service that returns objects of type ExpenseInfo. I added a reference to the service in a Silverlight project within the same solution. This generated a bunch of code, including an ExpenseInfo class within Reference.cs in the Silverlight project.

This class looks pretty good (although, if I want to add RIA validation data annotations, how can I do that?), but it's missing the WhoOwes property. Is there some reason that it can't be sent across the wire? Do I need to represent that data another way? Or did I mess up some setting?

+2  A: 

See http://stackoverflow.com/questions/1139834/wcf-webget-and-icollection

Add the

[ServiceKnownType(typeof(string[]))]

attribute to you class (not your method) and the WhoOwes property will be sent as string[].

Andreas Paulsson
Interesting idea, but it doesn't work. (I still have the same error.)
Rosarch
+1  A: 

Looks like the private set was a problem. I removed it, and now the field is showing up.

Is there any way to specify that the collection cannot be set, and still use it here?

Rosarch
You cannot, see http://stackoverflow.com/questions/1873741/wcf-exposing-readonly-datamember-properties-without-set. "Services share schema and contract, not class".
Andreas Paulsson
+1  A: 

AFAIK C# properties semantics is not represented in the meta-data describing a web-service.

These meta-data are in the different XML schemas files (with the ".xsd" extension) generated by WCF. The same is true for the RIA attributes that you could add to your data types.

The solution is to make the client aware of them by sharing the dll that embeds the types. You could create a third project "Data" to hold your data classes and reference it from both the server and client projects.

Serious
If I make a third project with the data classes, will adding a service reference still generate those classes within the Silverlight project?
Rosarch
Yes : each class referenced by the interface of your service will be added to the description. This is true if the reference is direct, if the class is used as a parameter or return value, or indirect, if the class appears in a "KnownType" attribute.
Serious
So, if the classes would be generated anyway, what's the point of a third `Data` class library project?
Rosarch
When your .Net classes are mapped to WCF entities a loss of information occurs : some stuff specific to .Net, like the fact the property is read-only, or the RIA meta-data, are not preserved. So from the standpoint of the client classes appear simpler. The solution to make the client aware of the .Net specificities is to provide it with a .Net representation of the classes. This is where you will need a third project that your client will reference to have the same knowledge of the entities the server has.
Serious