Json没有序列化Azure

时间:2016-10-14 18:32:53

标签: c# json azure serialization

我有POCO,就像

一样简单
public partial class Member
{

    public int ID { get; set; }

    [Required]
    [StringLength(100)]
    public string MemberId { get; set; }

    public DateTime CreatedOn { get; set; }

    [Required]
    [StringLength(100)]
    public string FirstName { get; set; }}

和一个看起来像这样的添加命令

   public class AddMemberCommand : ICommand
{
    public AddMemberCommand(Member member  )
    {
        ID = Guid.NewGuid();
        MemberData = member;
    }


    public  Member MemberData { get; private set; }

    public Guid ID { get; }


    public string CommandName
    {
        get { return "AddMemberCommand"; }

    }
}

继承自

public interface ICommand
{
    /// <summary>
    /// Gets the command identifier.
    /// </summary>
    Guid ID { get; }

    string CommandName { get; }
}

现在我将此代码发送到一个方法,该方法使用一些参数初始化Newton Json的序列化设置类以返回一个对象。序列化器看起来像这样

    public class JsonTextSerializer 
{
    private readonly JsonSerializer _serializer;

    public JsonTextSerializer()
    {
        _serializer = JsonSerializer.Create(new JsonSerializerSettings
        {
            TypeNameHandling = TypeNameHandling.All,
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
            TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
        });
    }

    public void Serialize(TextWriter writer, object graph)
    {
        var jsonWriter = new JsonTextWriter(writer);
        jsonWriter.Formatting = Formatting.Indented;
        _serializer.Serialize(jsonWriter, graph);
        writer.Flush();
    }

    public object Deserialize(TextReader reader)
    {
        var jsonReader = new JsonTextReader(reader);

        try
        {
            return this._serializer.Deserialize(jsonReader);
        }
        catch (JsonSerializationException e)
        {
            // Wrap in a standard .NET exception.
            throw new SerializationException(e.Message, e);
        }
    }
}

序列化程序用于将命令转换为代理消息的有效负载,如下所示

        private BrokeredMessage CreateMessage(POCOS.Member member)
    {
        var serializer = new JsonTextSerializer();
        var command = new AddMemberCommand(member);
        var stream = new MemoryStream();
        var writer = new StreamWriter(stream);
        serializer.Serialize(writer, command);
        stream.Position = 0;
        BrokeredMessage message = new BrokeredMessage(stream, true);
        return message;

    }

并且还有另一种接收方法的方法

   private POCOS.Member GetPocoFromMessage(BrokeredMessage message)
    {
        ITextSerializer serializer = new JsonTextSerializer();
        AddMemberCommand command;

        using (var stream = message.GetBody<Stream>())
        {
            using (var reader = new StreamReader(stream))
            {
                var payload = serializer.Deserialize(reader);
                command = payload as AddMemberCommand;
            }
        }

        return command.MemberData;
    }

问题在于反序列化某些属性( ID,CommandName )是否已填充值 MemberData 这是null。 我可以读取流(通过执行reader.ReadToEnd())并看到它是通过线路传输但Json无法反序列化其所有对象 曾经有一段时间我也认为它可能只选择界面中的字段,但事实并非如此

1 个答案:

答案 0 :(得分:2)

您的MemberData酒店有私人二手商家。由于序列化程序需要从外部访问该属性,因此该setter应该是公共的。