Search code examples
c#azureazure-storageazure-table-storage

Exception thrown after writing in Azure Table Storage


I am working with Microsoft Azure Table Storage in combination with the Microsoft Bot Framework. My goal is, that the bot writes down all of the incoming messages directly into the Table Storage. That worked for two weeks straight without any problems. But since one or two weeks ago, the bot won't write the messages down and I changed nothing in the code - but why?

Here is the method the bot executes, when it writes down the message from the user:

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);
    }

this is the place where the entry is made:

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; }
}

LinkSummary.cs

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

Here are the Nugget packages i am using: enter image description here enter image description here

enter image description here Exception thrown: 'Microsoft.WindowsAzure.Storage.StorageException' in System.Private.CoreLib.dll


Solution

  • One possible reason is due to there're some incorrect chars in PartitionKey or RowKey. Please refer to this doc for the invalid chars. And do make sure that you do not insert records with same PartitionKey and RowKey, this can also cause errors.

    You should also check the exceptions as mentioned in the comment, which can get the detailed information for finding the root cause.

    By the way, the package WindowsAzure.Storage you're using is really old. If possible, you should use the latest package Microsoft.Azure.Cosmos.Table for azure table storage.