Serializing object in Plugin CRM 365

As part of our migration process, I had to upload several plugins that were made for CRM 13 into a CRM 365 Online organization. After firing one of them, I received the next error:

Unhandled Exception: Microsoft.Crm.CrmException: Unexpected exception from plug-in (Execute): loremimpusium.ExampleAssembly.loremimpusium: System.MethodAccessException: Attempt by security transparent method ‘loremimpusium.ExampleAssembly.loremimpusium.UpdateAccount(Microsoft.Xrm.Sdk.ITracingService, System.String)’ to access security critical method ‘System.Web.Script.Serialization.JavaScriptSerializer..ctor()’ failed.Assembly ‘XYZXZY.CRMPlugins.XYZXZY, Version=, Culture=neutral, PublicKeyToken=8l6b8h5761905b08′ is partially trusted, which causes the CLR to make it entirely security transparent regardless of any transparency annotations in the assembly itself. In order to access security critical code, this assembly must be fully trusted.

Sometime ago, I found an error similar to this, which was related to an external library referenced in a Partial Trust plugin(Sandbox). Hence, I started to search for external libraries referenced in my assembly. Then, I came across that Newtonsoft.Json was being used, as well as the following lines of code:

JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
* Business Logic

After finding this, I confirmed that the reason of why I was receiving the error is because of the external library referenced in my assembly. MS Dynamics Online does not support full trust plugins, so, anytime you reference an external library in a partial trus plugin(Sandbox), you’ll get errors similar to this as they can’t be loaded.

How to solve the issue

First, I had to remove the reference to the external library in my assembly. Then, I had to do research of how I can replace the lines of code that were using the external library(The ones showed above). Fortunately, DataContractJsonSerializer works perfectly for this job. Here’s an example:

// Serialize object into a JSON
string Serialized_Object = "";
// Custom object to be serialized
CustomClass Obj = new CustomClass();
Obj.FirstName = "Example Name";

// Do actual serialization
using (MemoryStream memoryStream = new MemoryStream())
  DataContractJsonSerializer serializer = new DataContractJsonSerializer(Obj .GetType());
  serializer.WriteObject(memoryStream, Obj);
  Serialized_Object = Encoding.Default.GetString(memoryStream.ToArray());
//Return serialized object
return Serialized_Object;

Also, I had to add [DataContract] and [DataMember] to my CustomClass(Which is being serialized):

 public class CustomClass
    public string ContactId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

After uploading the plugin with these new changes, it worked perfectly!

Hope it helps!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s