本文介绍了如何使用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.ServiceBus
和WindowsAzure.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主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!