Azure Service Bus队列 - 使用BrokeredMessage进行序列化

时间:2014-11-07 01:04:36

标签: azure serialization queue servicebus brokeredmessage

尝试在邮件正文中提交带有对象的队列邮件,但在BrokeredMessage的以下行中接收异常

QueueClient queueClient = QueueClient.CreateFromConnectionString(_connectionString, _queuePathName);
var data = new ABSurvey
                {
                    name = "somename",
                    version = 1,
                    language = "eng",
                    SelfSurvey = new Survey()
                    {
                        SurveyItems = new List<ISurveyItem>() { new SurveyItem(){IsSelected = true, ItemId = 1}},
                        SurveyPerception = Constants.Perception.Self
                    },
                    SelfConcept = new Survey()
                    {
                        SurveyItems = new List<ISurveyItem>() { new SurveyItem(){IsSelected = true, ItemId = 1}},
                        SurveyPerception = Constants.Perception.SelfConcept
                    }
                };
                BrokeredMessage message = new BrokeredMessage(data);
                queueClient.Send(message); 

异常消息 - 输入&#39;。调查&#39;数据合同名称&#39;调查:http://schemas.datacontract.org/2004/07/namespace&#39;不是预期的。考虑使用DataContractResolver或将任何静态未知的类型添加到已知类型列表中 - 例如,使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型列表中。

这些是我的模型 -

    [DataContract]
public class ABSurvey
{
    [DataMember] public string name;
    [DataMember] public int version;
    [DataMember] public string language;
    [DataMember] public ISurvey SelfSurvey;
    [DataMember] public ISurvey SelfConcept;
}

[DataContract]
public class SurveyItem : ISurveyItem
{
    [DataMember]
    public int ItemId { get; set; }
    [DataMember]
    public bool IsSelected { get; set; }
    public SurveyItem()
    {
        ItemId = -1;
        IsSelected = false;
    }
}

[DataContract]
public class Survey : ISurvey
{
     [DataMember]
    public IList<ISurveyItem> SurveyItems { get; set; }
     [DataMember]
    public Constants.Perception SurveyPerception { get; set; }
    public Survey()
    {
        SurveyItems = new List<ISurveyItem>();
    }
}

public interface ISurvey
{
    [DataMember]
    IList<ISurveyItem> SurveyItems { get; set; }
    [DataMember]
    Constants.Perception SurveyPerception { get; set; }
}
public interface ISurveyItem
{
    [DataMember]
    int ItemId { get; set; }
    [DataMember]
    bool IsSelected { get; set; }
}

请帮助找到问题。

1 个答案:

答案 0 :(得分:3)

您的数据合约缺少[KnownType]属性,这就是您的邮件无法序列化的原因。详细说明可以在hehe找到。

只需添加[KnownType]属性即可告诉序列化器可以使用哪种具体实现:

[KnownType(typeof(Survey))]
[DataContract]  
public class ABSurvey
{
    [DataMember]
    public string name;
    [DataMember]
    public int version;
    [DataMember]
    public string language;
    [DataMember]
    public ISurvey SelfSurvey;
    [DataMember]
    public ISurvey SelfConcept;
}

[KnownType(typeof(SurveyItem))]
[DataContract]
public class Survey : ISurvey
{
    [DataMember]
    public IList<ISurveyItem> SurveyItems { get; set; }

    public Survey()
    {
        SurveyItems = new List<ISurveyItem>();
    }
}