首页 文章

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

提问于
浏览
0

我想将通过主题收到的数据存储到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; }
}

但是在视觉工作室执行后我得到了

mscorlib:执行函数时出现异常:Function1 . Microsoft.WindowsAzure.Storage:远程服务器返回错误:(400)错误请求 .

1 回答

  • 0

    你应该初始化 PartitionKeyTableEntityTableEntity .

    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功能中的 RommEntity 初始化修改为:

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

相关问题