0

Service:

enter image description here Error:

The error is with a type that has ChainedListNode<T>. Thing is, when I remove the DataMemberAttribute from Value, the service works.

[DataContract]
public class ChainedListNode<T>
{
    [DataMember]
    public T Value { get; set; }
}

Any ideas to what's causing it and/or how to solve it?

Matheus Simon
  • 668
  • 11
  • 34

2 Answers2

0

I do no think that generic is a good idea to use in the WCF, because I do not see a good serialization going in this case, even if you can achive it, although I am not sure it possible, I would think that you may still get error eventually.

The reason why it work when you remove DataMember is because it not getting serialized theoretically not used in the service only on the backed end.

COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
0

The problem is that the type parameter in the open type ChainedListNode<T> means that ChainedListNode<T>.Value could contain anything at all. WCF can't create a contract which describes all the possible values which could be placed in the Value property, so it rejects the whole type. When there is no Value property, the type parameter T is irrelevant and is ignored, and everything works OK.

In similar situations I've created a closed type derived from my generic type and used that type as my data contract:

[DataContract]
public class ChainedListNodeOfString : ChainedListNode<string>
{
    [DataMember]
    public string Value { get; set; }
}

If you need to, you can create a derived type (and a related OperationContract) for each different kind of value you need to return. This makes your API more verbose, but it works.

Steve Ruble
  • 3,875
  • 21
  • 27