Unknown type when serializing

Topics: Web Api
Aug 25, 2011 at 10:21 PM

In my solution I can post a command to my REST server but I can't make posting several commands in one call to work. The problem now is that the types are not known. Some code maybe make it more clear.

[DataContract]
[XmlRoot(Namespace = Schema.Default)]
public class CompositeCommand : CommandBase
{
  public CompositeCommand()
  {
  }

  [DataMember]
  [XmlElement]
  public List<CommandBase> Commands { get; set; }

  public CompositeCommand(IEnumerable<CommandBase> commands)
  {
    Commands = new List<CommandBase>(commands);
   }
}

Resource:

[WebInvoke(Method = HttpMethods.Post, UriTemplate = "composite")]
public HttpResponseMessage Post(HttpRequestMessage<CompositeCommand> request)
{
    var command = request.Content.ReadAs();   <= This fails with exception below
    ...
}

"The specified type was not recognized: name='SomeCommand', namespace='http://schemas.domain.net', at <Commands xmlns='http://schemas.domain.net'>."

Anyone that have been doing something similar?

 

Aug 26, 2011 at 2:39 PM

You'll most likely need to specify [KnownType(typeof(DerivedCommand))] for all your commands.

http://msdn.microsoft.com/en-us/library/ms730167.aspx

I'm not sure this is considered a best practice though.  I've read mixed opinions on blogs.