I have a web API controller action that has a parameter of type CommandMessage. This type has a list with basic CommandBase types. My problem is that I cannot get them to have derived MyCommand types in this example.
I am using JsonNetFormatter from the contrib web api project. It uses Json.Net as a serializer.
Web API Controller:
public class CommandController : ApiController
{
public HttpResponseMessage Post(CommandMessage command)
{
var message = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(string.Format("Type of command 1 is '{0}'", command.Commands[0].GetType().FullName))
};
return message;
}
}
This is my request:
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;
}
And like 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"
}
Return message from API controller action:
Command Type 1 - "PostToBaseClassList.Models.CommandBase"
Classes of teams:
[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 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);
}
Any ideas?