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
  • 链接地址: http://www.djcxy.com/p/52396.html

    上一篇: 如何使用自定义跟踪侦听器记录.net中的痕迹

    下一篇: JSON序列化Dynamics CRM