Mantaining backwards compatibility of a Dictionary DataMember in WCF
In version 1 of my product I have a DataMember that looks like this:
[DataMember]
private Dictionary< EnumType1, Class1> Class1Dictionary;
Where EnumType1 is defined as:
[Flags]
public enum EnumType1
{
EnumMember1 = 1;
EnumMember2 = 2;
EnumMember3 = 4;
}
The SOAP message looks like this (I'm paraphrasing):
<Class1Dictionary>
<KeyValueOfEnumType1Class1UTLV0zE5>
<Key>EnumMember1 &l开发者_JAVA百科t;/Key>
<Value> ... </Value>
</KeyValueOfEnumType1Class1UTLV0zE5>
</Class1Dictionary>
In version 2 I want to change Class1Dictionary to be a Dictionary<string, Class1>
rather than Dictionary<EnumType1, Class1>
, but I want to keep service level compatibility with the previous version. Of course the version v2 server/client would only know how to deal with the particular values for Key that match the enum values it knows about. For all other an exception is the expected behavior.
My first idea was to just change Class1Dictionary:
[DataMember]
private Dictionary< string, Class1> Class1Dictionary;
but That will distort the SOAP message, for example changing KeyValueOfEnumType1Class1UTLV0zE5 to something else. So the question is, how can I anotate this DataMember, or use any other WCF feature, to keep my Dictionary< string, Class1> on the v2 code, but use a DataContract compatible with the DataContract of version 1?
See http://msdn.microsoft.com/en-us/library/aa347850.aspx
[CollectionDataContract
(Name = "Class1Dictionary",
ItemName = "KeyValueOfEnumType1Class1UTLV0zE5",
KeyName = "Key",
ValueName = "Value")] public class MyDictionary : Dictionary<string, Class1> { }
Based on the information you give in the question I am not sure you can do what you want: typically for any backwards compatibility you add new data members, you do not change the type of existing data members. Therefore you might have to create another Service contract with the corresponding changes to the data contract. It would be best if you read the information at this link: Best Practices: Data Contract Versioning and apply that information to your circumstances.
You could also read the information at the following links: 1. Data Contract Versioning 2. Forward-Compatible Data Contracts
I ended up figuring out the correct answer. As long as the DataContracts are compatible than it will just work. And those funky generated names are ignored in that regard. That means that
List<KeyValuePair<string,Class1>>
is compatible with:
Dictionary<string,Class1>
精彩评论