JSON序列化动态CRM

本文关键字:CRM 动态 序列化 JSON | 更新日期: 2023-09-27 18:17:03

我尝试在自定义活动中序列化JSON中的约会。

下面是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));
    }
}
....

下面是序列化的代码:

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()); 

当活动被执行时,我有以下错误:

插件(Execute)异常:SmartwatchMeeting_PushGCM。SmartwatchMeeting:System.Security.SecurityException:数据契约类型System.Collections.Generic.KeyValuePair 2[[系统。字符串,mscorlib,Version = 4.0.0.0、文化=中立,都必须b77a5csadsad089]、[系统。对象,mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5csadsad089]]'无法在部分信任中序列化,因为成员"密钥"不是公共

我不明白我必须添加什么才能使它工作

谢谢你的帮助

JSON序列化动态CRM

您不能序列化类型的非公共成员,因为沙箱强制部分信任,而序列化器利用反射。

  • 切换到Isolation mode: None(仅限内部部署)
  • 为你的数据写一个模型类,只有public成员,并包装记录。这使得你的代码更大,但工作在Isolation mode: Sandbox