我正在wcf服务中创建一个post方法,应用程序可以推送响应我的网站发送的任何命令。
响应可以是单个对象或对象列表的形式。与手机详细信息和手机中安装的应用程序列表一样。
以下是我的DTO
[DataContract]
[KnownType(typeof(HandsetInfoReceiveDTO))]
[KnownType(typeof(List<ApplicationReceiveDTO>))]
public class CommandReceiveDTO : MessageCommandDTO
{
[DataMember]
public string CommandKey { get; set; }
[DataMember]
public string MAC { get; set; }
[DataMember]
public CommandReceiveBaseDTO Response { get; set; }
}
[DataContract]
public class HandsetInfoReceiveDTO : CommandReceiveBaseDTO
{
[DataMember]
public int Battery { get; set; }
[DataMember]
public bool Wifi { get; set; }
}
[DataContract]
public class ApplicationReceiveDTO : CommandReceiveBaseDTO
{
[DataMember]
public string AppName { get; set; }
}
[DataContract]
public class CommandReceiveBaseDTO
{
}
服务方法
public ResponseDTO<bool> ReceiveCommandResponseFromDevice(CommandReceiveDTO ObjResponse)
{
ResponseDTO<bool> Result = new ResponseDTO<bool>();
return Result;
}
帮助从服务
生成JSON代码{
"MessageCommandId":9223372036854775807,
"CommandKey":"String content",
"MAC":"String content",
"Response":{
"__type":"HandsetInfoReceiveDTO:#CommandService.DTO.Commands.Receive",
"Battery":2147483647,
"Wifi":true
}}
现在的问题是,如果发送ApplicationReceiveDTO列表,它就无法运行。 我从this link获得了引用,但没有显示任何对象列表的示例。
截至今天我尝试改变
[DataMember]
public CommandReceiveBaseDTO Response { get; set; }
要
[DataMember]
public List<CommandReceiveBaseDTO> Response { get; set; }
然后它接受json作为列表但不接受单个对象。
最终我需要一个可以容纳对象和对象列表的方法。 任何帮助肯定会节省我的时间。