JSON 序列化 Dynamics CRM
JSON serialization Dynamics CRM
我试图在自定义 activity 中序列化 JSON 中的约会。
这是class 的约会:
//<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());
并且当执行 activity 时,出现以下错误:
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
我不明白我必须添加什么才能使其正常工作?
感谢您的帮助
您不能序列化类型的非 public 成员,因为沙箱强制执行部分信任,并且序列化程序利用反射。
您可以:
- 切换到
Isolation mode: None
(仅限内部部署)
- 为你的数据写一个模型class,只有
public
个成员,然后包装记录。这会使您的代码更大,但适用于 Isolation mode: Sandbox
我试图在自定义 activity 中序列化 JSON 中的约会。
这是class 的约会:
//<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());
并且当执行 activity 时,出现以下错误:
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
我不明白我必须添加什么才能使其正常工作?
感谢您的帮助
您不能序列化类型的非 public 成员,因为沙箱强制执行部分信任,并且序列化程序利用反射。
您可以:
- 切换到
Isolation mode: None
(仅限内部部署) - 为你的数据写一个模型class,只有
public
个成员,然后包装记录。这会使您的代码更大,但适用于Isolation mode: Sandbox