基于计时器的 Azure 功能,具有 Table 存储、HTTP 请求和 Azure 服务总线

Timer based Azure function with Table storage, HTTP request, and Azure Service Bus

我现在有一个在控制台应用程序中编写的进程,它会触发计划任务以从 Azure table 存储中读取数据,并根据该数据向第三方发出 API 调用我们使用的供应商,反序列化响应数据,循环结果中的数组,将循环的各个迭代保存到 Azure table 存储中的不同 table,然后为每个迭代发布消息循环到 Azure 服务总线,这些消息由另一个客户端使用。

为了将更多任务转移到云中,我进行了一些研究,似乎 Azure 功能是替代我的控制台应用程序的理想选择。我在 Visual Studio 2019 年启动了一个新的 Azure 函数项目作为 "timer" 函数,然后开始阅读一些我很快就迷失了的东西。

我读过的文章谈到在我的 运行() 方法参数中使用 "bindings" 并用连接字符串等属性装饰,但我不确定这是我应该的方向标题。听起来这样可以更轻松地对我的 table 存储进行身份验证,但我不知道如何使用这些 "hooks" 来查询我的 table 然后执行插入。我什至还没有接触到服务总线的东西,也没有研究过对我们第三方供应商的 api.

进行 HTTP 调用

我知道这是一个非常宽泛的问题,我没有 post 的任何代码,因为我什至很难从这个开始。 MS 文档到处都是,我找不到任何符合我需要的内容,我保证我已经花了很多时间尝试。

Azure 函数是否是我应该走的正确道路?如果没有,还有哪些其他选择?

TIA

您应该使用带有时间触发器的 Azure Functions 来替换您的控制台应用程序。

绑定(可用于输入/输出)是帮助您节省一些代码行的助手,例如:

而不是使用以下代码将数据插入 azure table:

// Retrieve storage account information from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);

// Create a table client for interacting with the table service
CloudTableClient tableClient = storageAccount.CreateCloudTableClient(new TableClientConfiguration());

// Create a table client for interacting with the table service 
CloudTable table = tableClient.GetTableReference("MyTable");

//some code to populate an entity
var entity = new { PartitionKey = "Http", RowKey = Guid.NewGuid().ToString(), Text = input.Text };

// Create the InsertOrReplace table operation
TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(entity);

// Execute the operation.
TableResult result = await table.ExecuteAsync(insertOrMergeOperation);

你会使用:

[FunctionName("TableOutput")]
[return: Table("MyTable")]
public static MyPoco TableOutput([HttpTrigger] dynamic input, ILogger log)
{
    log.LogInformation($"C# http trigger function processed: {input.Text}");
    return new MyPoco { PartitionKey = "Http", RowKey = Guid.NewGuid().ToString(), Text = input.Text };
}

PS: 前面代码中的input trigger是一个HTTP Trigger,只是为了说明如何使用output binding。

您可以在此处找到更多信息:

https://docs.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings

https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-table

你应该看:https://docs.microsoft.com/en-us/learn/modules/chain-azure-functions-data-using-bindings/