如何将数据从服务总线存储到 Azure 表?

How to store data from service bus to azure tables?

我想将通过主题接收到的数据存储到 Azure 表中。 为此,我使用 Azure 函数。

public static class Function1
{
    [FunctionName("Function1")]
    public static async Task Run([ServiceBusTrigger("simple_test_topic", "stt_subscription", AccessRights.Manage, Connection = "ServiceBusConnectionString")]string mySbMsg,
        [Table("RoomBasicInformation")] CloudTable outputTable, TraceWriter log)
    {
        log.Info($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
        RoomEntry re = new RoomEntry(){ PartKey = "hpk", RoomId = "13", Temperature = "10"};
        var operation = TableOperation.Insert(re);
        await outputTable.ExecuteAsync(operation);
    }
}

public class RoomEntry : TableEntity
{
    public string PartKey { get; set; }
    public string RoomId { get; set; }
    public string Temperature { get; set; }
}

但是在 visual studio 中执行后我得到

mscorlib: Exception while executing function: Function1. Microsoft.WindowsAzure.Storage: The remote server returned an error: (400) Bad Request.

您应该初始化 TableEntityPartitionKeyRowKey

Add an entity to a table

修改 RoomEntity 以初始化这些值:

public class RoomEntry : TableEntity
{
    public RoomEntry() 
    {
    }

    public RoomEntry(string partitionKey, string roomId) 
    {
        this.PartitionKey = partitionKey;
        this.RowKey = roomId;
    }

    public string RoomId => this.RowKey;
    public string Temperature { get; set; }
}

你不需要 PartKey 属性 TableEntity 已经有一个了。

同时将 Azure Function 中的 RommEntity 初始化修改为:

var roomEntry = new RoomEntry("hpk", "13") { Temperature = "10" };