我想将我的对话历史记录保存到 Azure Cosmos DB 或 storageAccount。
- 关于 Azure Cosmos DB
使用IActivityLogger似乎真的很容易。但我不知道如何获得连接在 Global.asax.cs 中的数据库连接。还是在 IActivityLogger 中重新获取更好?
- 关于 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);
}