有没有办法通过Rest发送不可序列化的对象? 我有一个请求类,其中一些属性可以序列化,但不是全部。
该类从SDK继承了另一个类(参见下面的“IWorkflowContext”)。此类受到保护,无法序列化 - 但需要进行转移。我尝试使用JSON,其中SDK对象的非序列化是一个问题,当我尝试序列化它二进制时我得到一个序列化器异常
“请求获得类型的许可 'System.Security.Permissions.SecurityPermission,mscorlib, Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089' 失败“。
我的要求&对于二进制文件,序列化看起来像这样:
byte[] bytes;
IFormatter formatter = new BinaryFormatter();
using (MemoryStream s = new MemoryStream())
{
formatter.Serialize(s, userRequest);
bytes = s.ToArray();
}
stream.Write(bytes, 0, bytes.Length);
这里是Json序列化
DataContractJsonSerializer ser = new DataContractJsonSerializer(userRequest.GetType());
ser.WriteObject(stream, userRequest);
或其他Json序列化程序(导致相同的非序列化问题)
string json = JsonConvert.SerializeObject(userRequest);
byte[] buffer = Encoding.UTF8.GetBytes(json);
stream.Write(bytes, 0, bytes.Length);
供参考,这里是我的类(为二进制序列化而修改 - 对于Json,我为每个属性都有一个Datamember标志。
[Serializable()]
public class AzureRequest
{
public string _orgaName = string.Empty;
public string _connectionString = string.Empty;
public IWorkflowContext _contextWorkflow = null;
public string _webhookOperation = string.Empty;
/// <summary>
/// The Connectionrequest describes a schema to connect to a CRM.
/// e.g. "Url=http://OrgName.crm.dynamics.com/OrgName;AuthType=Office365;Username=UserName@OrgName.onmicrosoft.com;Password=Password";
/// </summary>
public string ConnectionString
{
get
{
return _connectionString;
}
set
{
_connectionString = value;
}
}
/// <summary>
/// The Organization Name of the CRM to contact
/// </summary>
public string OrgaName
{
get
{
return _orgaName;
}
set
{
_orgaName = value;
}
}
/// <summary>
/// Is the running Context for the Worklfow instance
/// </summary>
public IWorkflowContext ContextWorkflow
{
get
{
return _contextWorkflow;
}
set
{
_contextWorkflow = value;
}
}
/// <summary>
/// The Operation, the Webhook should perform
/// </summary>
public string WebhookOperation
{
get
{
return _webhookOperation;
}
set
{
_webhookOperation = value;
}
}
}