将对象集中到Azure Blob存储

use*_*346 3 c# azure azure-storage-blobs

我试图将对象写入azure blob以进行持久存储,并且由于某种原因,1属性从未被序列化,我不确定为什么.

这是对象

[Serializable]
public class BlobMetaData
{
    public DateTimeOffset? ModifiedDate { get; set; }
    public string ContentType { get; set; }
    public string Name { get; set; }
    // size of the file in bytes
    public long Length { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是将数据保存到Azure存储的功能.

public void Save(string filename,BlobProperties blobProperties)
{
    //full path to the blob
    string saveFile = _clientDirectory + filename;
    CloudBlockBlob blockBlob = _blobContainer.GetBlockBlobReference(saveFile);
    //blobMetaData properly gets all the right values.
    BlobMetaData blobMetaData = ConvertBlobProperties(blobProperties,filename);
    // I convert it to a stream
    MemoryStream stream = SerializeToStream(blobMetaData);
    blockBlob.UploadFromStream(stream);
}
Run Code Online (Sandbox Code Playgroud)

这是我如何序列化数据.

    private MemoryStream SerializeToStream(BlobMetaData blobMetaData)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(BlobMetaData));
        MemoryStream stream = new MemoryStream();
        serializer.Serialize(XmlWriter.Create(stream), blobMetaData);
        stream.Seek(0, SeekOrigin.Begin);
        return stream;
    }
Run Code Online (Sandbox Code Playgroud)

由于某种原因..所有值都正确存储在Azure XML中,除了ModifiedDate ..它总是空白,即使在我调用SerializeToStream()之前我检查blobMetaData并且它确实具有值..因此它在序列化过程中丢失.

这是XML的样子

<?xml version="1.0" encoding="utf-8"?><BlobMetaData   
xmlns:xsd="http://www.w3.org/2001/XMLSchema"   
xmlns:xsi="http://www.w3.org/2001/XMLSchema- 
instance"><ModifiedDate /><ContentType>application/octet-stream</ContentType><Name>u_ex14060716.log</Name><Length>652</Length></BlobMetaData>
Run Code Online (Sandbox Code Playgroud)

正如您所见,modifiedDate为空.任何人都知道为什么?

Gar*_*y.S 5

所以答案似乎是DateTimeOffset不是用XmlSerializer序列化的(http://connect.microsoft.com/VisualStudio/feedback/details/288349/datetimeoffset-is-not-serialized-by-a-xmlserializer) .

解决方法似乎是创建可以序列化的属性(字符串或DateTime和int用于偏移)或根据连接错误使用datacontract序列化程序.

这个问题有更多可能的解决方案.

我如何XML序列化DateTimeOffset属性?