迁移 Azure SDK 1.6 TableStorage 和 Writing/Reading 实体处理程序

Migrating Azure SDK 1.6 TableStorage and Writing/Reading Entity handlers

有一些遗留代码使用 Azure SDK 1.6,我的任务是更新到 latest/2.7。下面是保存实体的快速示例。请注意,我们似乎使用了自己的自定义序列化程序。我知道如何为基本 read/write 转换到新的 CloudTable,但我不确定如何移植我们正在使用的 serialize/deserialize 逻辑。

 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 来传递反序列化器。

这是序列化程序的表示 class:

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 方法使用。

如果您硬性要求保持相同的功能和对 Atom 的依赖性,您可以通过 CloudTableClient 实例上的 GetTableServiceContext() 方法获取 table 服务上下文。请注意,您需要设置格式以使用 Atom(.Format.UseAtom() 方法)。 上下文具有相同的 WritingEntity 和 ReadingEntity 事件处理程序。

但是,这些 API 已过时。我们建议使用 ITableEntity/TableEntity 的 ReadEntity 和 WriteEntity 方法。 如果您有无法从 ITableEntity 派生的第 3 方对象,您可以参考以下博客 post 了解如何序列化它们:http://blogs.msdn.com/b/windowsazurestorage/archive/2013/09/07/announcing-storage-client-library-2-1-rtm.aspx