如何使用Azure函数将数据发送到Service Bus主题?

How to send data to Service Bus Topic with Azure Functions?(如何使用Azure函数将数据发送到Service Bus主题?)
本文介绍了如何使用Azure函数将数据发送到Service Bus主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有默认的基于C#的HTTP触发器,我希望将数据"Hello name"发送到Service Bus主题(已经创建)。我在门户网站编程。

如何进行Service Bus输出绑定?

这不起作用。有可用的帮助吗?

-缺少处理服务总线的引用?

-如何定义服务总线的连接?Functions.json在哪里

-如何将消息发送到服务总线?

//This FunctionApp get triggered by HTTP and send message to Azure Service Bus

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace Company.Function

{
    public static class HttpTriggerCSharp1
{
    [FunctionName("HttpTriggerCSharp1")]
    [return: ServiceBus("myqueue", Connection = "ServiceBusConnection")] // I added this for SB Output. Where to define.

    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        ILogger log)

    {
        log.LogInformation("C# HTTP trigger function processed a request.");
        string name = req.Query["name"];
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        name = name ?? data?.name;
        string responseMessage = string.IsNullOrEmpty(name)
            ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
            : $"Hello, {name}. This HTTP triggered function executed successfully.";
        return new OkObjectResult(responseMessage);
        // I added this for SB Output
        return responseMessage;
    }
}

}

推荐答案

首先,有两个向服务总线发送数据的绑定。首先是您展示的,使用return绑定,在安装两个包Microsoft.Azure.WebJobs.Extensions.ServiceBusWindowsAzure.ServiceBus之后,您就可以发送数据了。但您无法执行此操作,因为您的函数类型为IActionResult,并且您正在尝试返回string(ResponseMessage)。

因此,如果您想发送整个responseMessage,只发送return new OkObjectResult(responseMessage);,就可以了。结果如下图所示。

如果要使用return responseMessage;应将您的方法类型更改为字符串,则它将为public static async Task<string> RunAsync,结果将在下面。

下面的代码或此sample可以引用的另一个绑定。

[FunctionName("Function1")]
        [return: ServiceBus("myqueue", Connection = "ServiceBusConnection")]
        public static async Task RunAsync(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            [ServiceBus("myqueue", Connection = "ServiceBusConnection")] MessageSender messagesQueue,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            byte[] bytes = Encoding.ASCII.GetBytes(responseMessage);
            Message m1 = new Message(bytes);
            await messagesQueue.SendAsync(m1);

        }

如何定义服务总线的连接?Functions.json在哪里

在本地,您应该在local.settings.jon中定义连接,您可以在连接中使用任何名称,然后在绑定中Connection值应该是您在json文件中设置的名称。并且因为您使用的是c#,所以您无法修改function.json文件,调试文件夹中将有一个function.json文件。所以您只能更改代码中的绑定。

希望这能对您有所帮助,如果您还有其他问题,请随时通知我。

这篇关于如何使用Azure函数将数据发送到Service Bus主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)