开发者

Custom objects in WCF

I have following object, it works fine when I specify standart objects like int, string and not with custom object.

[DataContract(Namespace = "")]
public class StatusLog<TItem>
{
    /// <summary>
    /// Gets or sets the key.
    /// </summary>
    /// <value>
    /// The key.
    /// </value>
    [DataMember]
    public Guid Id { get; set; }

    /// <summary>
    /// Gets or sets the action status.
    /// </summary>
    /// <value>
    /// The action status.
    /// </value>
    [DataMember]
    public ActionStatus ActionStatus { get; set; }

    /// <summary>
    /// Gets or sets the object.
    /// </summary>
    /// <value>
    /// The object.
    /// </value>
    [DataMember]
    public TItem Object { get; set; }

    /// <开发者_高级运维summary>
    /// Gets or sets the message.
    /// </summary>
    /// <value>
    /// The message.
    /// </value>
    [DataMember]
    public string Message { get; set; }
}

This works: return new StatusLog { Id = Guid.NewGuid(), ActionStatus = ActionStatus.Deleted, Object = Convert.ToInt32(id), Message = "Node deleted successfully" };

This does not work: new StatusLog {Id = Guid.NewGuid(), ActionStatus = ActionStatus.Created, Object = MyCustomObject};


Have a look at KnownType Attribute.


As flosk8 mentioned, the DataContractSerializer won't know what types to consider when deserializing your DataContract unless it is decorated with the KnownType attribute.

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

Try adding the following attribute to your DataContract:

[DataContract(Namespace = "")]
[KnownType(typeof(MyCustomObject))]
public class StatusLog<TItem>
{
  // ... snip ...
}

You will need to add this attribute for each type that may need to be deserialized to the StatusLog.Object property. Additionally, each of these types need to be serializable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜