Как сделать устойчивый пост-вызов в лазурной функции ServiceBusTrigger?

Привет, я использую функцию ServiceBusTrigger azure, чтобы получать сообщения, полученные в очереди, а затем отправлять их на свой веб-интерфейс, который собирается делать некоторые вещи с этим содержимым

[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
}

Я читал о лазурных функциях, и, чтобы они стали дешевле, я читал о надежных функциях, я с нетерпением жду, как их использовать, чтобы я мог принимать решения на основе своего ответа, и я мог заставить его работать с этим ServiceBusTrigger


person Herberth Gomez    schedule 25.06.2020    source источник


Ответы (1)


Необходимо изменить текущую функцию 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
}

В моем случае мне пришлось указать заголовки в запросе, иначе я использовал 415 Unsupported Media Type, можно было бы сделать таким образом или просто создать запрос без указания какого-либо заголовка в начале, а затем добавить такие, как это:

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

Оба варианта работают

person Herberth Gomez    schedule 25.06.2020