1

我想将我的对话历史记录保存到 Azure Cosmos DB 或 storageAccount。

  1. 关于 Azure Cosmos DB

使用IActivityLogger似乎真的很容易。但我不知道如何获得连接在 Global.asax.cs 中的数据库连接。还是在 IActivityLogger 中重新获取更好?

  1. 关于 storageAccount

据此 SDK 还提供了一种使用 storageAccount 保存对话历史记录的方法。但是Activity已经被压缩了。我想将未压缩的真实对话 [JSON] 保存到 storageAccount。

我正在尝试向具有压缩数据的 Azure 表存储插入一行。但 Activity0 始终为空。

public class ActivityEntity : TableEntity
    {
        /// <summary>
        /// Empty constructor.
        /// </summary>
        public ActivityEntity()
        { }

        /// <summary>
        /// Construct from an IActivity.
        /// </summary>
        /// <param name="activity"></param>
        public ActivityEntity(byte[] Activity)
        {
            PartitionKey = "111";
            RowKey = "11";
            From = "111";
            Recipient = "111";
            Activity0 = Activity;
            Version = 3.0;
        }

        /// <summary>
        /// Version number for the underlying activity.
        /// </summary>
        public double Version { get; set; }

        /// <summary>
        /// Channel identifier for sender.
        /// </summary>
        public string From { get; set; }

        /// <summary>
        /// Channel identifier for receiver.
        /// </summary>
        public string Recipient { get; set; }

        /// <summary>
        /// Logged activity.
        /// </summary>
        [IgnoreProperty]
        public byte[] Activity0 { get; set; }

        /// <summary>
        /// Generate a partition key given <paramref name="channelId"/> and <paramref name="conversationId"/>.
        /// </summary>
        /// <param name="channelId">Channel where activity happened.</param>
        /// <param name="conversationId">Conversation where activity happened.</param>
        /// <returns>Partition key.</returns>
        public static string GeneratePartitionKey(string channelId, string conversationId)
        {
            return $"{channelId}|{conversationId}";
        }

        /// <summary>
        /// Generate row key for ascending <paramref name="timestamp"/>.
        /// </summary>
        /// <param name="timestamp">Timestamp of activity.</param>
        /// <returns></returns>
        public static string GenerateRowKey(DateTime timestamp)
        {
            return $"{timestamp.Ticks:D19}";
        }

    }




     class Program
    {
        public static object CloudConfigurationManager { get; private set; }

        static void Main(string[] args)
        {

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=storagetesthuafu;AccountKey=iQ0EZexm3wbpWZqly2HtVH0/CZKRyMY9l2b0g20AQkUz7BX0BFLuBinMyYLe8Ow/zOA7vJqAMSxSHllT3JTL2g==;EndpointSuffix=core.windows.net");

            // Create the table client.
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            // Create the CloudTable object that represents the "TemperatureData" table.
            CloudTable table = tableClient.GetTableReference("messagelog");



            TableQuery<BotDataRow> query = new TableQuery<BotDataRow>();
            var data = table.ExecuteQuery(query);
            var dataarry = data.ToArray();


            var aa = dataarry.First();
            var activity = aa.Activity0;
            var after = Decompress(activity);


            CloudTable tableTEST = tableClient.GetTableReference("messagelog");


            byte[] bb = Encoding.UTF8.GetBytes(after);
            ActivityEntity customer4 = new ActivityEntity(bb);

            // Create the InsertOrReplace TableOperation.
            TableOperation insertOrReplaceOperation = TableOperation.InsertOrReplace(customer4);

            // added to the table.
            table.Execute(insertOrReplaceOperation);
        }
4

1 回答 1

1

创建使用DocumentDb的IActivityLogger实现不需要太多代码。下面是使用Azure Cosmos DB 模拟器的示例:

class DocumentDbActivityLogger : IActivityLogger
{
    const string DbKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
    const string DbUri = "https://localhost:8081";
    const string DbId = "ActivityLogsDb";
    const string CollectionId = "ActivityLogsColleciton";

    async Task IActivityLogger.LogAsync(IActivity activity)
    {
        try
        {
            var message = activity.AsMessageActivity();
            if (message != null)
            {
                using (var documentClient = new DocumentClient(new Uri(DbUri), DbKey))
                {
                    await documentClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DbId, CollectionId), message);
                }
            }
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.Print(e.ToString());
        }
    }
}

然后,在 Global.asax.cs 中注册它:

Conversation.UpdateContainer(builder =>
{
     builder.RegisterType<DocumentDbActivityLogger>().AsImplementedInterfaces().InstancePerDependency();
}

在此处输入图像描述

于 2018-08-02T23:04:55.257 回答