Search code examples
c#wcfdatacontractserializerdatacontract

DataContract, default DataMember value will api work between applications


I have a class like this:

[DataContract, Serializable]
    public class MyInfo
    {
        string firstName;
        string fullName;
    }

There is an api that is exposed like this GetMyInfo(MyInfo myInfo) in which above class is being used.

This contract is being used by my four(app1, app2, app3, app4) applications. As of now everything is running fine. But i got a new requirement to add a new member to the interface which is a dictionary.

New class will be like this:

[DataContract, Serializable]
    public class MyInfo
    {
        string firstName;
        string fullName;
        Dictionary<string, string> parameter = null;
    }

Now my question is since the class definition is changed, my app1 and app2 will compile with new class definition as because of app1 and app2 require that dictionary and app3 and app4 doesn't want that, that's why i created dictionary as default parameter.

Now if app3 or app4 doesn't compile with new class definition and give a call to GetMyInfo(MyInfo myInfo) will the things work, i mean all serialization and nothing will break.


Solution

  • According to your description, I made a demo, which can run normally.I didn't find anything wrong.

    Client1 uses a class like this:

            [DataContract, Serializable]
            public class MyInfo
           {
              [DataMember(Name = "firstName")]
              public string firstName { get; set; }
              [DataMember(Name = "fullName")]
              public string fullName { get; set; }
           }
    

    The server-side will get this message:

    enter image description here

    Client2 uses a class like this:

            [DataContract, Serializable]
            public class MyInfo
           {
                [DataMember(Name = "firstName")]
                public string firstName { get; set; }
                [DataMember(Name = "fullName")]
                public string fullName { get; set; }
                [DataMember(Name = "parameter")]
                public Dictionary<string, string> parameter { get; set;}
           }
    

    The server-side will get this message: enter image description here

    The server-side uses the same classes as client2.