1

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?

Nick Heiner
  • 119,074
  • 188
  • 476
  • 699

3 Answers3

2

See 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[].

Community
  • 1
  • 1
Andreas Paulsson
  • 7,745
  • 3
  • 25
  • 31
1

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?

Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
  • 1
    You cannot, see http://stackoverflow.com/questions/1873741/wcf-exposing-readonly-datamember-properties-without-set. "Services share schema and contract, not class". – Andreas Paulsson Oct 10 '10 at 19:02
1

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.

Pragmateek
  • 13,174
  • 9
  • 74
  • 108
  • If I make a third project with the data classes, will adding a service reference still generate those classes within the Silverlight project? – Nick Heiner Oct 10 '10 at 17:59
  • 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. – Pragmateek Oct 10 '10 at 20:23
  • So, if the classes would be generated anyway, what's the point of a third `Data` class library project? – Nick Heiner Oct 11 '10 at 22:33
  • 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. – Pragmateek Oct 12 '10 at 12:14