0
votes

The problem I am trying to solve is that, I need to serialize the Entity's Target/Pre/Post-Image into a json string. I know that we cannot include nuget libraries in the plugins as ILmerge is not an option. This is the one i have tried:

var jStr = Serialize((Entity)excutionContext.InputParameters["Target"]);

public static string Serialize<TEntity>(TEntity dataObject)
{
    using (var stream = new MemoryStream())
    {
         var serializer = new DataContractJsonSerializer(typeof(TEntity));
         serializer.WriteObject(stream, dataObject);
         return Encoding.UTF8.GetString(stream.ToArray());
    }
}

But i am getting the following exception and unable to figure out what i do to solve the same:

System.Security.SecurityException: 'The data contract type 'System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' cannot be serialized in partial trust because the member 'key' is not public.'

2
Do you need legit JSON, or are you just using it for displaying it the JSON in the log?Daryl
@Daryl - Yes i need a legit JSON, as i need to pass off those images into their respective systems which can understand and process those further.animat089

2 Answers

1
votes

As far as I know we cannot serialize Entity in CRM.

At best you can create a datacontract class with it's get set members and then serialize Entity. But again this will be lot of manual work.

Look into below 2 threads wich gives more info about your issue.

Thread 1

Thread 2

0
votes

If you use the Source.DLaB.Xrm library from NuGet, it contains a SerializableEntity class that was designed to allow for Xml Serialization in sandbox plugins. You should be able to use it for your JSON here as well:

Serialize(new Source.DLaB.Xrm.Sandbox.Serialization.SerializableEntity(yourSdkEntity))

The Source.DLaB.Xrm package is a source only NuGet package, so you don't have to ILMerge anything.