3

I have a Web API controller action which has a parameter of type CommandMessage. That type has a list with base types, CommandBase. My problem is that I can't make them to be of derived types, MyCommand in this example.

I'm using JsonNetFormatter from the Web API contrib project. It's using Json.Net as serializer.

Web API controller:

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;
    }
}

This i 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 as 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"
}

The return message from the API controller action is:

Type of command 1 is 'PostToBaseClassList.Models.CommandBase'

Command classes:

 [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:

//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);
    }

Any ideas?

4

2 回答 2

2

Json.NET 不尊重KnownTypes属性,您必须提供自己的转换器。

这个问题提供了几个不同的解决方案来解决这个问题: Using Json.NET converters to deserialize properties

于 2012-05-16T09:20:16.550 回答
0

在我将此行添加到 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);
}
于 2012-05-17T06:10:42.120 回答