3

I have a WCF service in which I have some data contracts. I'm using web service software factory, which uses a designer to create all the message and data and other contracts, and it creates them as partial classes. When the code is regenerated the classes are recreated.

using WcfSerialization = global::System.Runtime.Serialization;

[WcfSerialization::CollectionDataContract(Namespace = "urn:CAEService.DataContracts", ItemName = "SecurityItemCollection")]
public partial class SecurityItemCollection : System.Collections.Generic.List<SecurityItem>
{
}

The data contract is a generic List of a custom class which was working fine. However I now want to add a property to this class, so I added this partial class in the same namespace:

public partial class SecurityItemCollection
{
    public int TotalRecords { get; set; }
}

This seems to work fine on the service side, but when I compile and then update the service reference from the client, the class doesn't have the new property i.e. when it serialises it and recreates it on the client side, it's missing the new property. Anyone know why this is?

TIA

EDIT: Ok this is officially driving me nuts. The only thing I can see is that it is using the CollectionDataContract attribute instead of DataContract. Does this somehow not allow data members in the class to be serialised? Why would that be? It is working fine on the service side - I can see and populate the values no problem. However when I update the service refernce on my client there's nothing, just the colelction class without the data member.

Ciarán Bruen
  • 5,221
  • 13
  • 59
  • 69
  • What happens when you put the `[DataContract]` attribute on the new property and then regenerate your proxy? – slugster Dec 08 '10 at 11:02
  • Hi slugster - I tried the same attribute as the 1st class and I got an error: Duplicate 'WcfSerialization::CollectionDataContract' attribute C:\xxx\CAEService.DataContracts\SecurityItemCollection.cs – Ciarán Bruen Dec 08 '10 at 13:25
  • I also tried assing a normal [DataContract] attribute to the 2nd class which compiled but I got an error when I published the service: Type 'CAEService.DataContracts.SecurityItemCollection' with CollectionDataContractAttribute attribute is an invalid collection type since it has DataContractAttribute attribute – Ciarán Bruen Dec 08 '10 at 13:28

3 Answers3

3

Try to add the DataMember attribute to the TotalRecords property

Johann Blais
  • 9,389
  • 6
  • 45
  • 65
  • Are you sure the two partial classes are in the same namespace? To verify that, remove the partial keyword from one of the classes and compile. If it does not crash, they are not in the same namespace. – Johann Blais Dec 08 '10 at 18:41
  • Yes I'm sure. Apparently data mambers aren't allowed in classes marked as CollectionDataContract...see my answer above. – Ciarán Bruen Dec 10 '10 at 12:09
3

Ok after much searching I finally found out that this isn't allowed i.e. classes marked as CollectionDataContract can't have data members. Why this is I have no idea but it cost me several hours and a major headache. See link below:

WCF CollectionDataContract and DataMember

Ciarán Bruen
  • 5,221
  • 13
  • 59
  • 69
2

I know this is old but it kept coming up when I searched on this subject. I was able to get this working by adding a "DataMemberAttribute" attribute to the property. Below is a code example.

public partial class SecurityItemCollection
{
    [global::System.Runtime.Serialization.DataMemberAttribute()]
    public int TotalRecords { get; set; }

}
JCPhlux
  • 455
  • 5
  • 9