使用Azure SDK 1.6创建一些遗留代码,我的任务是更新到最新版/ 2.7。以下是保存实体的快速示例。请注意,我们似乎使用了自己的自定义序列化程序。我知道如何转换为新的CloudTable进行基本读/写,但我不确定如何移植我们正在使用的序列化/反序列化逻辑。
internal class FaultyMessageRecord : TableServiceEntity {
public DateTime Created { get; set; }
public Byte[ ] Content { get; set; }
}
public void StoreMessage( FaultyMessage faultyMessage ) {
var context = tableClient.GetDataServiceContext( );
context.WritingEntity += ( o, e ) => entrySerializer.WritingEntity( e );
var now = DateTime.UtcNow;
context.AddObject( tableName, new FaultyMessageRecord {
PartitionKey = "",
RowKey = now.ToString( "s" ), //sortable datetime
Content = (byte[])faultyMessage.TransportMessageContent,
Created = now
} );
}
部分移植的代码:
internal class FaultyMessageRecord : TableEntity {
public DateTime Created { get; set; }
public Byte[ ] Content { get; set; }
}
public void StoreMessage( FaultyMessage faultyMessage ) {
var table = tableClient.GetTableReference(tableName);
var now = DateTime.UtcNow;
var insertOperation = TableOperation.Insert(new FaultyMessageRecord
{
PartitionKey = "",
RowKey = now.ToString("s"), //sortable datetime
Content = (byte[]) faultyMessage.TransportMessageContent,
Created = now
});
table.Execute(insertOperation);
}
阅读似乎有一个类似的逻辑,我可以在必要时提供,但基本上它使用context.ReadingEntity来传递反序列化器。
以下是序列化程序类的表示:
public class UntypedAtomSerializer {
protected string[ ] hardcodedProperties;
public UntypedAtomSerializer( params string[ ] hardcodedProperties ) {
this.hardcodedProperties = hardcodedProperties;
}
public void WritingEntity( System.Data.Services.Client.ReadingWritingEntityEventArgs e ) {
var propertyEl = e.Data
.Elements( ).Where( el => el.Name.LocalName == "content" )
.Elements( ).Where( el => el.Name.LocalName == "properties" )
.FirstOrDefault( );
SerializeProperties( e.Entity, propertyEl );
}
private void SerializeProperties( object tableEntry, XElement propertyEl ) {
SerializeProperties( tableEntry, propertyEl, this.hardcodedProperties );
}
private static void SerializeProperties( object entry, XElement propertyEl, params string[ ] hardcodedProperties ) {
var tableEntry = entry as IDictionaryEntity;
if ( tableEntry != null ) {
var elNS = XNamespace.Get( BaseAtomSerializer.WCF_DATA_NAMESPACE );
var attrNS = XNamespace.Get( BaseAtomSerializer.WCF_METADATA_NAMESPACE );
//add any properties that are part of the BaseStorageEntity dictionary
foreach ( var kvp in tableEntry.ExtraProperties.Where( kvp => !hardcodedProperties.Contains( kvp.Key ) ) ) {
var newEl = new XElement( XName.Get( kvp.Key, elNS.NamespaceName ), kvp.Value );
if ( kvp.Value == null )
newEl.Add( new XAttribute( XName.Get( "null", attrNS.NamespaceName ), "true" ) );
else
newEl.Add( new XAttribute( XName.Get( "type", attrNS.NamespaceName ), BaseAtomSerializer.MapCLRtoAtomType( kvp.Value.GetType( ) ) ) );
propertyEl.Add( newEl );
}
}
}
}
它被实例化为UntypedAtomSerializer entrySerializer = new UntypedAtomSerializer( "PartitionKey", "RowKey", "Timestamp", "Created", "Content" );
,供上面的StoreMessage方法使用。
答案 0 :(得分:0)
如果您需要保持对Atom的相同功能和依赖性,则可以通过CloudTableClient实例上的GetTableServiceContext()方法获取表服务上下文。请注意,您需要将格式设置为使用Atom(.Format.UseAtom()方法)。 Context具有相同的WritingEntity和ReadingEntity事件处理程序。
但是,这些API已经过时了。我们建议使用ITableEntity / TableEntity的ReadEntity和WriteEntity方法。 如果您有第三方对象无法从ITableEntity派生,您可以参考以下博客文章,了解如何序列化它们:http://blogs.msdn.com/b/windowsazurestorage/archive/2013/09/07/announcing-storage-client-library-2-1-rtm.aspx