2
votes

I am trying to serialize a complete set of Dynamics CRM metdata, using roughly the following code:

RetrieveAllEntitiesRequest metaDataRequest = new RetrieveAllEntitiesRequest()
{
    EntityFilters = EntityFilters.All
};

// Execute the request
RetrieveAllEntitiesResponse metaDataResponse = (RetrieveAllEntitiesResponse)service.Execute(metaDataRequest);

entitiesMetadata = metaDataResponse.EntityMetadata;

// Store Metadata for faster usage in the future
Serialize<EntityMetadata[]>(entitiesMetadata, "foobar_metadata.xml");

public static void Serialize<T>(T value, string filePath)
{
    if (value == null)
    {
        return;
    }
    try
    {
        var xmlserializer = new XmlSerializer(typeof(T));
        var stringWriter = new StringWriter();
        using (var writer = new XmlTextWriter(stringWriter) { Formatting = System.Xml.Formatting.Indented })
        {
            xmlserializer.Serialize(writer, value);
            File.WriteAllText(filePath, stringWriter.ToString());
        }
    }
    catch (Exception ex)
    {
        throw new Exception("An error occurred", ex);
    }
}

Yet, some of the metadata classes do not have parameterless constructors. So I get the following exception:
[...] cannot be serialized because it does not have a parameterless constructor

I know that the common advice is to extend the affected classes and give them a parameterless constructor. Yet, this could mean many iterations of trial and error as many classes are involved in the Dynamics CRM metadata.

Is there any other way of serializing/deserializing, which doesn't need a parameterless constructor for each class?
Did somebody go through the effort of extending classes for Dynamics CRM metadata and can share the code?
Is there a possibility to automatically omit any non-serializable classes and only serialize the rest?

1

1 Answers

0
votes

I have never manually serialized an entity's metadata. Fortunately, exporting a solution that contains an entity serializes the metadata.

While adding the constructors might allow you to get over the hump with "manual" serializing, you could programmatically create a solution, add all the entities to it, and export it as an unmanaged solution.

The exported solution will be a zip file. All of the entities' metadata will be the cutomizations.xml file. You could then parse that XML and split each entity out to its own file.

Out of curiosity, what is driving the requirement to serialize the metadata? Is it for documentation purposes?