使用 Function App 从 Azure 服务总线发送消息到 API APIM

Send Message to API in APIM from Azure Service Bus using Function App

有没有一种方法可以使用函数应用程序将在服务总线队列上接收到的负载发送到 API 中的 API?我计划在 ServiceBusTrigger 函数上执行此操作。我已经在QueueMessage上有message/payload,我只需要直接调用API并将QueueMessage的值发送到API

public static void Run([ServiceBusTrigger("sbq-messagequery", Connection = "ServiceBusConnection")]
    QueueMessage queueItem, ILogger log)
    {
        dynamic data = JsonConvert.SerializeObject(queueItem);
        log.LogInformation($"C# ServiceBus queue trigger function processed message: {data}");
        log.LogWarning($"Id = {queueItem.Payload}");
    }

您必须在 Trigger 方法中执行 HttpRequest。

请查找有关在 Azure Functions 中使用 HttpClient 的建议in the documentation

        [FunctionName("MyHttpTrigger")]
    public async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        var response = await _client.GetAsync("https://microsoft.com");
        var message = _service.GetMessage();

        return new OkObjectResult("Response from function with injected dependencies.");
    }

您的情况可能是 POST 请求:

public static void Run([ServiceBusTrigger("sbq-messagequery", Connection = "ServiceBusConnection")] QueueMessage queueItem, ILogger log)
{
    dynamic data = JsonConvert.SerializeObject(queueItem);
    log.LogInformation($"C# ServiceBus queue trigger function processed message: {data}");
    log.LogWarning($"Id = {queueItem.Payload}");

    var requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://yourtarget/api/lorem");
    var json = JsonConvert.SerializeObject(queueItem.Payload);
    requestMessage.Content = new StringContent(json, Encoding.UTF8, "application/json");
    var response = _client.SendAsync(httpRequestMessage).Result;
}