簡體   English   中英

如何在 ServiceBusTrigger azure function 中進行持久的后調用?

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

嘿,我正在使用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
}

我一直在閱讀有關 azure 功能的文章,為了它變得更便宜,我閱讀了有關持久功能的文章,我期待如何使用它們,以便我可以根據我的響應做出決定,我可以讓它與這個ServiceBusTrigger一起工作

需要更改當前的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}");
}

然后需要創建另一個 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
}

在我的情況下,我必須在請求中指定標頭,否則我曾經獲得415 Unsupported Media Type可以通過這種方式完成,或者只是創建請求而不在開始時指定任何 header 然后添加如下內容:

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

兩種選擇都有效

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM