gpt4 book ai didi

c# - 如何在 ServiceBusTrigger azure 函数中进行持久的后调用?

转载 作者:行者123 更新时间:2023-12-03 00:59:44 26 4
gpt4 key购买 nike

嘿,我正在使用 ServiceBusTrigger azure 函数来获取队列中收到的消息,然后将它们发送到我的 webapi,该 webapi 将对该内容执行一些操作

[FunctionName("MyAzureFunction")]
public async void Run(
[ServiceBusTrigger("<MyQueue>", Connection = "<MyConnectionString>")] Message myQueueItem, ILogger log)
{
log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem.ToString()}");
var client = new HttpClient();
// Retrieving the string content from the message
var bodyMessage = Encoding.UTF8.GetString(myQueueItem.Body);
// Calling my API to do something based on the message content
var response = await client.PostAsync("<MyAPIUrl>", new StringContent(bodyMessage, Encoding.UTF8, "application/json"));
// doing something based on the response
}

我一直在阅读有关 azure 函数的内容,为了让它变得更便宜,我阅读了有关持久函数的内容,我期待着如何使用它们,以便我可以根据我的回复做出决定,并且我可以让它与此一起使用ServiceBusTrigger

最佳答案

需要更改当前的 ServiceBusTrigger 函数,以便它调用另一个实际执行该工作的函数:

[FunctionName("MyAzureFunction")] public async void Run(
[ServiceBusTrigger("<MyQueue>", Connection = "<MyConnectionString>")] Message myQueueItem,
[DurableClient] IDurableOrchestrationClient orchestratorClient,
ILogger log) {

log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem.ToString()}");

// Here is where you need to specify in the first parameter the name of the function to be called
// and the last parameter are the params you'll send to that one
var instanceId = await orchestratorClient.StartNewAsync("MyPostFunction", null, myQueueItem);

log.LogInformation($"C# ServiceBus queue trigger function created an async instance of 'MyPostFunction' with the ID: {instanceId}");
}

然后需要创建另一个 OrchestrationTrigger 类型的函数,如下所示:

[FunctionName("MyPostFunction")] public async void RunOrchestrator([OrchestrationTrigger] IDurableOrchestrationContext context) {
// using the context can be retrieved the parammeters passed in the function above
// in this case I just specify the type of that one and that's it
var myQueueItem = context.GetInput<Message>();
var bodyMessage = Encoding.UTF8.GetString(myQueueItem.Body);

// Create a URI of your API url
var postUri = new Uri($"<MyAPIUrl>");

// depending on your WebAPI you'll need to specify the content type in the headers
var headers = new Dictionary<string, StringValues>() { { "Content-Type", "application/json" } };
// creating durable http request
var request = new DurableHttpRequest(HttpMethod.Post, postUri, headers, bodyMessage);

// Doing the http call async, in this context you'll save money since your function will not be completely waiting for a response
// this one will keep just checking to see if there's a response available or not
var response = await context.CallHttpAsync(request);

// do your stuffs depending in the response
}

就我而言,我必须在请求中指定 header ,否则我曾经得到 415 Unsupported Media Type 可以通过这种方式完成,或者只是创建请求而不在 header 处指定任何 header 开始然后添加这样的内容:

var request = new DurableHttpRequest(HttpMethod.Post, postUri, null, bodyMessage);
request.Headers.Add("Content-Type", "application/json");

两个选项都有效

关于c# - 如何在 ServiceBusTrigger azure 函数中进行持久的后调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62577003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com