我有一个Web API控制器操作,它有一个类型为CommandMessage的参数。该类型有一个包含基本类型的列表,即 CommandBase。我的问题是我不能使它们成为派生类型,在这个例子中是MyCommand。
我正在使用Web API contrib项目中的JsonNetFormatter。它使用 Json.Net 作为序列化程序。
Web API 控制器:
public class CommandController : ApiController
{
public HttpResponseMessage Post(CommandMessage command)
{
//command.Commands[0].GetType().Name is always "CommandBase" instead of "MyCommand"
var message = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(string.Format("Type of command 1 is '{0}'", command.Commands[0].GetType().FullName))
};
return message;
}
}
这是我的要求:
private void When()
{
using (client)
{
command = new MyCommand
{
CommandId = Guid.NewGuid(),
Id = Guid.NewGuid(),
Name = "Martin"
};
var message = new CommandMessage(Guid.NewGuid(),new List<CommandBase> {command});
var requestMessage = GetHttpRequestMessage(message);
var task = client.PostAsync("http://localhost.:16218/api/command", requestMessage.Content);
task.Wait();
responseMessage = task.Result;
}
}
private static HttpRequestMessage GetHttpRequestMessage<T>(T data)
{
var mediaType = new MediaTypeHeaderValue("application/json");
var jsonFormatter = new JsonNetFormatter(DefaultJsonSettings.Settings());
var requestMessage = new HttpRequestMessage<T>(data, mediaType, new[] {jsonFormatter});
return requestMessage;
}
作为 JSON:
{
"$type": "PostToBaseClassList.Models.CommandMessage, PostToBaseClassList",
"Commands": [
{
"$type": "PostToBaseClassList.Models.MyCommand, PostToBaseClassList",
"Id": "45923a41-0c15-46e3-907d-64dd06840539",
"Name": "Martin"
}
],
"MessageId": "c873970a-8621-4223-806e-b809039438ab"
}
来自 API 控制器操作的返回消息为:
命令 1 的类型是"PostToBaseClassList.Models.CommandBase"
命令类:
[DataContract]
[XmlRoot]
public class MyCommand : CommandBase
{
private readonly string name;
public MyCommand()
{
}
public MyCommand(Guid id, string name)
{
this.name = name;
Id = id;
}
[DataMember(Order = 1)]
[XmlElement]
public Guid Id { get; set; }
[DataMember(Order = 2)]
[XmlElement]
public string Name { get; set; }
}
[DataContract]
[KnownType("GetKnownTypes")]
public class CommandBase
{
public Guid CommandId { get; set; }
public static Type[] GetKnownTypes()
{
var query =
from type in typeof (CommandBase).Assembly.GetTypes()
where typeof (CommandBase).IsAssignableFrom(type)
select type;
var types = query.ToArray();
return types;
}
}
[DataContract]
[Serializable]
[XmlRoot]
public class CommandMessage
{
public CommandMessage()
{
}
public CommandMessage(Guid messageId, IEnumerable<CommandBase> commands)
{
MessageId = messageId;
Commands = new List<CommandBase>(commands);
}
[DataMember]
[XmlElement]
public List<CommandBase> Commands { get; set; }
[DataMember]
[XmlElement]
public Guid MessageId { get; set; }
}
Json.Net 设置:
//DefaultJsonSettings.Settings()
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
TypeNameHandling = TypeNameHandling.Objects,
Converters = new List<JsonConverter>
{
new IsoDateTimeConverter
()
}
};
private static void RegisterFormatters(HttpConfiguration config)
{
var jsonNetFormatter = new JsonNetFormatter(DefaultJsonSettings.Settings());
config.Formatters.Insert(0, jsonNetFormatter);
}
有什么想法吗?
尊重KnownTypes
属性,您必须提供自己的转换器。
此问题提供了几种不同的解决方案:使用 Json.NET 转换器反序列化属性
在我将这一行添加到 RegisterFormatters() 后它起作用了
config.Formatters.Remove(config.Formatters.JsonFormatter);
这还不够
config.Formatters.Insert(0, jsonNetFormatter);
完整版
private void RegisterFormatters()
{
config.Formatters.Remove(config.Formatters.JsonFormatter);
var jsonNetFormatter = new JsonNetFormatter(DefaultJsonSettings.Settings());
config.Formatters.Insert(0, jsonNetFormatter);
}