JSON序列化Dynamics CRM
我尝试在自定义活动中使用JSON序列化一个appointement。
这是预约课程:
//<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());
而当活动执行时,我有以下错误:
来自插件的意外异常(执行):SmartwatchMeeting_PushGCM.SmartwatchMeeting:System.Security.SecurityException:数据协定类型'System.Collections.Generic.KeyValuePair`2 [[System.String,mscorlib,Version = 4.0.0.0,Culture =中性,PublicKeyToken = b77a5csadsad089],[System.Object,mscorlib,版本= 4.0.0.0,文化=中立,PublicKeyToken = b77a5csadsad089]]'不能在部分信任序列化,因为成员的'密钥'不公开
我不明白我必须添加什么才能使其工作?
感谢您的帮助
您不能序列化非公共类型的成员,因为沙箱会强制部分信任,并且序列化程序会利用反射。
你可以:
Isolation mode: None
(仅限本地) public
成员,并包装记录。 这会让您的代码更大,但可以在Isolation mode: Sandbox