Using ICollection property with WCF Service
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?
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[].
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.
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?
精彩评论