使用功能应用将消息从Azure Service Bus中的APIM发送到API

发布于 2025-01-26 04:24:36 字数 531 浏览 3 评论 0 原文

有没有办法发送在服务总线队列上接收到的有效载荷,使用功能应用程序将其发送到API中的API?我计划在ServiceBustrigger函数上执行此操作。我已经在Queuemessage上有消息/有效载荷,我只需要直接调用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}");
    }

Is there a way to send a payload that is received on the service buss queue to be sent to an API in APIM using function app? I'm planning to do this on the ServiceBusTrigger function. I already have the message/payload on the QueueMessage, I just need to directly call the API and send the value of the QueueMessage to the API

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

用心笑 2025-02-02 04:24:36

您必须在触发方法中进行httprequest。

请找到建议在Azure函数中使用httpclient

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

在您的情况下,这可能是邮政请求:

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

You have to do an HttpRequest in your Trigger method.

Please find advice for using the HttpClient in Azure Functions 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.");
    }

In your case it might be a POST request:

public static void Run([ServiceBusTrigger("sbq-messagequery", Connection = "ServiceBusConnection")] QueueMessage queueItem, ILogger log)
{
    dynamic data = JsonConvert.SerializeObject(queueItem);
    log.LogInformation(
quot;C# ServiceBus queue trigger function processed message: {data}");
    log.LogWarning(
quot;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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文