简体   繁体   中英

JSON serialization Dynamics CRM

I tried to serialize an appointement in JSON in a custom activity.

Here is the class for Appointment :

//<summary>
// Commitment representing a time interval with start/end times and duration.
// </summary>
//
[System.Runtime.Serialization.DataContractAttribute()]
[Microsoft.Xrm.Sdk.Client.EntityLogicalNameAttribute("appointment")]
[System.CodeDom.Compiler.GeneratedCodeAttribute("CrmSvcUtil", "7.1.0001.3108")]
public partial class Appointment : Microsoft.Xrm.Sdk.Entity, System.ComponentModel.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged
{

// <summary>
// Default Constructor.
// </summary>
public Appointment() : 
        base(EntityLogicalName)
{
}

public const string EntityLogicalName = "appointment";

public const int EntityTypeCode = 4201;

public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

public event System.ComponentModel.PropertyChangingEventHandler PropertyChanging;

private void OnPropertyChanged(string propertyName)
{
    if ((this.PropertyChanged != null))
    {
        this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
    }
}
....

And here is the code for the serialization :

Entity entity = (Entity) context.InputParameters["Target"];

        ColumnSet csAll = new ColumnSet(true);
        Appointment appointment = (Appointment) service.Retrieve(entity.LogicalName, entity.Id, csAll);

        System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Appointment));
        MemoryStream ms = new MemoryStream();
        serializer.WriteObject(ms, appointment);
        string jsonNotification = Encoding.Default.GetString(ms.ToArray()); 

And when the activity is executed, I have the following error :

Unexpected exception from plug-in (Execute): SmartwatchMeeting_PushGCM.SmartwatchMeeting: System.Security.SecurityException: The data contract type 'System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5csadsad089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5csadsad089]]' cannot be serialized in partial trust because the member 'key' is not public

I don't understand what I have to add to make it work ?

Thank you for your help

You cannot serialize non-public members of types because the sandbox enforces partial trust, and the serializer leverages reflection.

You can either:

  • Switch to Isolation mode: None (On-Premise only)
  • Write a model class for your data, with only public members, and wrap the record. This makes your code larger but works in Isolation mode: Sandbox

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM