繁体   English   中英

写入 Azure 表存储后抛出异常

[英]Exception thrown after writing in Azure Table Storage

我正在将 Microsoft Azure 表存储与 Microsoft Bot 框架结合使用。 我的目标是,机器人将所有传入的消息直接写入表存储。 连续工作两周没有任何问题。 但是从一两周前开始,机器人不会将消息写下来,我也没有对代码进行任何更改——但为什么呢?

这是机器人在写下来自用户的消息时执行的方法:

protected async Task TableStorageEintrag(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
    {
        // Schlüssel für den Table-Zugriff
        string accountKey = "MY STORAGE ACCOUNT KEY 1";
        string accountName = "MY STORAGE ACCOUNT NAME";

        // Schlüssel werden hier für den Table-Eintrag zusammengepackt
        TableQueries tableQueries = new TableQueries
        {
            accountKey = accountKey,
            accountName = accountName
        };

        // Werte für den Table-Eintrag
        string rowKey = DateTime.Now.ToString();
        string partitionKey = rowKey;
        string userStatement = $"{turnContext.Activity.Text}";

        System.Diagnostics.Debug.WriteLine(rowKey);
        System.Diagnostics.Debug.WriteLine(partitionKey);
        System.Diagnostics.Debug.WriteLine(userStatement);

        // Methode für den Table-Eintrag wird hier ausgeführt
        Task<bool> bLinkCreated = tableQueries.InsertURL(partitionKey, rowKey, userStatement);
        bLinkCreated.Wait();

        // Wird ausgeführt, wenn keine KnowledgeBase gefunden wird
        System.Diagnostics.Debug.WriteLine("### FINDE KEINE PASSENDE ANTWORT ###");
        await turnContext.SendActivityAsync(MessageFactory.Text("Leider kann ich Ihnen hierbei noch nicht weiterhelfen. Ich bin aber schon dabei, Neues zu lernen!"), cancellationToken);
    }

这是输入的地方:

public class TableQueries
{
    public string accountKey { get; set; }
    public string accountName { get; set; }

    public async Task<bool> InsertURL(string partitionKey, string rowKey, string userStatement)
    {
        bool bSuccess = false;

        CloudStorageAccount storageAccount = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(accountName, accountKey), true);
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

        CloudTable linkTable = tableClient.GetTableReference(accountName);

        // linkTable.CreateIfNotExists();
        await linkTable.CreateIfNotExistsAsync();

        // Erstelle eine neue Entität
        LinkEntity link = new LinkEntity(partitionKey, rowKey);

        // Füge im neuen Table-Eintrag die Nachricht des Benutzers hinzu
        link.userStatement = userStatement;

        // Create the TableOperation that inserts the customer entity.
        TableOperation insertOperation = TableOperation.InsertOrMerge(link);

        try
        {
            await linkTable.ExecuteAsync(insertOperation);
            bSuccess = true;
        }
        catch (Exception e)
        {
            bSuccess = false;
        }
        return bSuccess;
    }
}

LinkEntry.cs:

public class LinkEntity : TableEntity
{
    public LinkEntity(string partitionKey, string rowKey)
    {
        PartitionKey = partitionKey;
        RowKey = rowKey;
    }


    public LinkEntity() { }

    public string userStatement { get; set; }
}

链接摘要.cs

public class LinkSummary
{
    public string userStatement { get; set; }
}

这是我正在使用的 Nugget 软件包: 在此处输入图片说明 在此处输入图片说明

在此处输入图片说明 抛出异常:System.Private.CoreLib.dll 中的“Microsoft.WindowsAzure.Storage.StorageException”

一种可能的原因是PartitionKeyRowKey有一些不正确的字符。 有关无效字符,请参阅此文档 并确保您没有插入具有相同PartitionKeyRowKey记录,这也会导致错误。

您还应该检查注释中提到的异常,可以获得详细信息以查找根本原因。

顺便说一下,您使用的WindowsAzure.Storage包真的很旧。 如果可能,您应该使用最新的包Microsoft.Azure.Cosmos.Table来存储 Azure 表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM