开发者

Complex situations to deal with concerning DataContract in WCF

There are thousands of questions regarding DataContract(s) in WCF, with special attention for those situations where inheritance is involved. However I did not find no examples regarding particular situations all summarized in the one I am about to provide here.

Consider the following DataContract applied to a type used in a WCF service communication (Code listing 1):

// Data contract for my comm type
[DataContract]
public class MyCommsType {
   [DataMember]
   public Type1 field1;
   [DataMember]
   public Type2 field2;
   [DataMember]
   public Type1 field3;
   [DataMember]
   public Type2 field4;
   [DataMember]
   public List<Type2> field5;
   [Data开发者_如何学编程Member]
   public List<Type1> field6;
}
// Used types
public class Type1 {
   ...
}
public class Type2 {
   ...
}

This type is supposed to be used here (Code listing 1.1):

[ServiceContract]
public interface IMyService {
   [OperationContract]
   string CommOp1(MyCommType mct);
   [OperationContract]
   MyCommType CommOp2(string s);
}

My personal type MyCommsType is a type involved in communications. But, of course, CLR does not know how to serialize it since it is not a native type in the Base Class Library. A data contract is needed for MyCommsType, and so I provided it. However, this state of art is not enough to get things work (nothing works like this!!).

Why does not that work? Is that because, inside my MyCommsType there are unknown types?

Well, should I do the following (Code listing 2)?

// Data contract for my comm type
[DataContract]
[KnownType(typeof(Type1))]
[KnownType(typeof(Type2))]
[KnownType(typeof(List<Type1>))]
[KnownType(typeof(List<Type2>))]
public class MyCommsType {
   [DataMember]
   public Type1 field1;
   [DataMember]
   public Type2 field2;
   [DataMember]
   public Type1 field3;
   [DataMember]
   public Type2 field4;
   [DataMember]
   public List<Type2> field5;
   [DataMember]
   public List<Type1> field6;
}

Is it correct????

However, if it is correct, how does the CLR know how to serialize Type1 and Type2????? I never added an attribute to these two types???? Do I have to place [Serializable] as shown here (Code listing 3)?

// Used types
[Serializable]
public class Type1 {
   ...
}
[Serializable]
public class Type2 {
   ...
}

How to deal with this situation?

To make it simpler (for answering) consider the following questions:

1) Does Code listing 1 completely solve the serialization problems?

1a) If not, then, how to deal with the problem?

2) In the case 1) is ok, how to deal with serialization problems of Type1 and Type2? Does [Serializable] application solve?


Your issue may be solved in a much simpler way.

No matter what kind of type you are using, all you need to do is, simply make it serializable. The simpler solution to it is make your sub classes as Data contracts themselves.

For example if you have

// Data contract for my comm type  
[DataContract]  
public class MyCommsType {     
[DataMember]     public Type1 field1;     
[DataMember]     public Type2 field2;     
[DataMember]     public Type1 field3;     
[DataMember]     public Type2 field4;     
[DataMember]     public List<Type2> field5;     
[DataMember]     public List<Type1> field6;  
}  

Make sure Type1 and Type2 are Datacontracts. That's all you need to do.

[DataContract]
public class Type1 {...}

[DataContract]
public class Type2 {...}

If you are doing any type Inherence, then you may need to place the KnownType attribute. Otherwise just keeping the sub types (Type1 and Type2) as Datacontracts would do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜