我正在尝试将对象写入 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; }
}
这是将数据保存到 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);
}
这是我序列化数据的方法。
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;
}
出于某种原因.. 除了 ModifiedDate 之外,所有值都正确存储在 Azure XML 中.. 即使在我调用 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>
如您所见,modifiedDate 为空。有人知道为什么吗?
最佳答案
因此,答案似乎是 DateTimeOffset 并非设计为使用 XmlSerializer ( http://connect.microsoft.com/VisualStudio/feedback/details/288349/datetimeoffset-is-not-serialized-by-a-xmlserializer ) 进行序列化。
解决方法似乎是创建可序列化的属性(字符串或 DateTime 和 int 作为偏移量)或根据连接错误使用数据契约序列化程序。
这个问题有更多可能的解决方案。
关于c# - 将对象序列化到 Azure Blob 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24236563/