Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hello experts,

In wcf service, I have data contract. One of the data contract property having return type is 'object'. How could I serialize this contract. See below contract and property item. I need to serialize this contract. I did some googling and tried with serviceknown type attributes but no luck yet. If I use this contract, my server is forcefully closed. Of course, I know this is because of object return type. Can I make this property generic OR any other solution?

[DataContract]
    public class Message
    {
        private string messageIdField;
        private MessageOperationType operationTypeField;
        private object itemField;
        [DataMember]
        public string MessageId
        {
            get
            {
                return this.messageIdField;
            }
            set
            {
                this.messageIdField = value;
            }
        }
        [DataMember]
        public MessageOperationType OperationType
        {
            get
            {
                return this.operationTypeField;
            }
            set
            {
                this.operationTypeField = value;
            }
        }
        [DataMember]
        public object Item
        {
            get
            {
                return this.itemField;
            }
            set
            {
                this.itemField = value;
            }
        }
    }
Posted
Updated 9-Jun-11 2:18am
v2
Comments
Sergey Alexandrovich Kryukov 9-Jun-11 11:16am    
May I know what's the point to return untyped object Item?
--SA

1 solution

ServiceKnownType[^] always does the trick for me. Admittedly, I do not use object as my return type, but I often use interfaces in conjunction with ServiceKnownType attributes.

I assume that there is no common interface that the classes of the objects that you return would implement, otherwise you would declare your Item property as such. In cases like that (i.e. when I must return objects from disjoint class hierarchies) I use "poor man's variant" implementation, which looks as follows:
C#
[DataContract]
public class ReturnType1 {...}
[DataContract]
public class ReturnType2 {...}
[DataContract]
public class ReturnType3 {...}

[DataContract]
public class ReturnVariant {
   [DataMember] private ReturnType1 RetMember1 {get; set}
   [DataMember] private ReturnType2 RetMember2 {get; set}
   [DataMember] private ReturnType3 RetMember3 {get; set}
   public object Item {
      get {
          return RetMember1 ?? RetMember2 ?? (object)RetMember3;
      }
   }
   public ReturnVariant(object item) {
       if (item is ReturnType1) RetMember1 = (ReturnType1)item;
       else if (item is ReturnType2) RetMember2 = (ReturnType2)item;
       else if (item is ReturnType3) RetMember3 = (ReturnType3)item;
       else throw new ArgumentException("item");
   }
}
Use ReturnVariant to encapsulate your objects.
 
Share this answer
 
Comments
Kiran Sonawane 10-Jun-11 0:55am    
Hey dasblinkenlight, Nice solution for disjoint class hierarchies. I haven't tried this solution yet. I will try this. But somehow I fill it could be solution to my problem. Thanks Dude. My 5 plus accepted solution. Thanks once again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900