简体   繁体   English

如何在 ServiceBusTrigger azure function 中进行持久的后调用?

[英]How to do a durable post call in an ServiceBusTrigger azure function?

Hey I'm using a ServiceBusTrigger azure function to get the messages received in a queue, and then send them to my webapi which is going to do some stuff with that content嘿,我正在使用ServiceBusTrigger azure function 来获取在队列中接收到的消息,然后将它们发送到我的 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
}

I've been reading about azure functions and in order to it gets cheaper I read about durable functions, I'm looking forward how to use them so I can take decisions based on my response and I can get it working with this ServiceBusTrigger我一直在阅读有关 azure 功能的文章,为了它变得更便宜,我阅读了有关持久功能的文章,我期待如何使用它们,以便我可以根据我的响应做出决定,我可以让它与这个ServiceBusTrigger一起工作

Needs to be changed the current ServiceBusTrigger function so it calls another Function that will actually do the job:需要更改当前的ServiceBusTrigger function 所以它会调用另一个 Function 来实际完成这项工作:

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

Then is needed to create another function that will be OrchestrationTrigger type, that will look like this:然后需要创建另一个 function,它将是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
}

In my case I had to specicify the headers in the request, otherwise I used to get 415 Unsupported Media Type can be done in that way, or just creating the request without specifying any header at the begining and then adding those like this:在我的情况下,我必须在请求中指定标头,否则我曾经获得415 Unsupported Media Type可以通过这种方式完成,或者只是创建请求而不在开始时指定任何 header 然后添加如下内容:

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

Both options work两种选择都有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure Function.Net 5 使用ServiceBusTrigger - Azure Function .Net 5 Using ServiceBusTrigger Azure 耐用 Function - 在 Orchestrator 中执行 - Azure Durable Function - Do While in Orchestrator 如何停止 Azure Durable 函数的并行执行? - How to stop parallel execution on Azure Durable function? 如何更新 Azure Durable Function 的 runtimeStatus? - How to update runtimeStatus for Azure Durable Function? Azure耐用功能如何恢复? - How do Azure Durable Functions resume? 是否可以在运行时通过 Azure Function ServiceBusTrigger 连接 - Is it possible to Pass Azure Function ServiceBusTrigger connection at runtime Azure Function 与 ServiceBusTrigger 未按计划触发 - Azure Function with ServiceBusTrigger not firing when scheduled 适用于Azure Function 2.0的ServiceBusTrigger绑定属性 - ServiceBusTrigger for Azure Function 2.0 binding Properties Azure功能 - c# - 带有Blob绑定的ServicebusTrigger - Azure Function - c# - ServicebusTrigger with Blob binding 当消息无法在ServiceBusTrigger Azure功能中处理时,如何在x分钟内延迟处理相同的消息? - When a message fails to process in a ServiceBusTrigger Azure Function, how can I delay processing the same message for x minutes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM