gpt4 book ai didi

c# - 如何使用Azure Functions将数据发送到服务总线主题?

转载 作者:行者123 更新时间:2023-12-03 18:42:14 24 4
gpt4 key购买 nike

我这里有默认的基于 C# 的 HTTP 触发器,我希望将数据“Hello Name”发送到服务总线主题(已创建)。我正在门户网站上编码。

如何做到服务总线输出绑定(bind)?

这不起作用。有什么帮助吗?

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

-如何定义服务总线的连接? 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;
}
}

}

最佳答案

首先,有两个绑定(bind)将数据发送到服务总线。首先是您使用return显示的内容绑定(bind),安装两个包后 Microsoft.Azure.WebJobs.Extensions.ServiceBusWindowsAzure.ServiceBus ,然后您就可以发送数据了。你不能这样做,因为你的函数类型是 IActionResult并且您正在尝试返回 string (响应消息)。

所以如果你想发送整个responseMessage ,只是return new OkObjectResult(responseMessage); ,它会起作用。结果如下图所示。

enter image description here

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

enter image description here

另一个绑定(bind)你可以引用下面的代码或这个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);

}

How to define Connection of service bus? Where is Functions.json

在本地,您应该在 local.settings.jon 中定义连接,您可以在连接中使用任何名称,然后在绑定(bind) Connection 中使用value 应该是您在 json 文件中设置的名称。由于您使用的是c#,因此您无法修改 function.json文件,在debug文件夹下会有一个function.json文件。所以你只能改变代码中的绑定(bind)。

希望这可以帮助您,如果您还有其他问题,请随时告诉我。

关于c# - 如何使用Azure Functions将数据发送到服务总线主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60607880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com