Maintain data compatibility with .NET Serialized when moving classes

I have data that has been serialized. The classes associated with serialized data are part of a large legacy project that has a number of third-party references that are not needed for this basic dataset. Now I need to read this data in another application. I would like to reorganize the data classes into a separate project that can be shared between two applications, so I do not need all third-party libraries. I also want to maintain compatibility with previously saved data. I do not need to change any fields in the classes, just the project where they are located.

So far, I have moved classes to a new project. I saved namespaces in the same way as in the old project. But this is not enough to read objects. I get a SerializationException that says "An analysis error, there is no type associated with the Xml key a1 MyCorp.MyApp.DatabaseRoot MyCorp.MyApp". By looking at the XML-XML associated with SOAP, schema references have changed. For example, I have a class MyCorp.Dashboard.DatabaseRoot natively in a DashboardLibrary project . This has been ported to the DashboardData strong> project (but the MyCorp.Dashboard.DatabaseRoot namespace is still used ). XML has changed as follows:

Orig: <a1:DatabaseRoot id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/MyCorp.Dashboard/MyCorp.Dashboard">
New:  <a1:DatabaseRoot id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/MyCorp.Dashboard/DashboardData">

So my questions are:

  • Is it possible to move classes and maintain compatibility? I find myself next to him.
  • , (MyCorp.Dashboard vs. DashboardData). , - . , . - ?

.

+2
1

SerializationBinder. BindToType, :

public override Type BindToType(string assemblyName, string typeName)
{
    if (assemblyName == "MyOldAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
    {
        if (typeName == "MyOldNamespace.MyOldClass")
        {
            return typeof(MyNewClass);
        }
    }
    // Fall back to the default behavior, which will throw
    // a SerializationException if the old assembly can't be found
    return null;
}

( , , , ).

BindToName, , . .

SerializationBinder, Binder .

( , ...), ISerializationSurrogate, .

+5

Source: https://habr.com/ru/post/1795444/


All Articles