使用服务总线绑定(bind)在本地开发 Azure Functions 时,可以使用 HTTP 请求发送请求来测试函数 http://localhost:{port}/admin/functions/{function_name}
(请参阅 https://learn.microsoft.com/en-us/azure/azure-functions/functions-run-local 处的非 HTTP 触发函数)。
在 HTTP 请求的正文中,可以发送以下内容来填充服务总线消息的正文:
{
"input": "<trigger_input>"
}
它绑定(bind)到方法中的 message.Body 属性: 例如
public class TestServiceBusTrigger
{
[FunctionName("test-servicebus-trigger")]
public static void ProcessQueueMessage([ServiceBusTrigger("inputqueue")] Message message, TextWriter logger)
{
logger.WriteLine(message.Body);
}
}
如何在示例请求中填充 message.UserProperties
。
当前使用以下版本:
- Microsoft.Azure.WebJobs 3.0.0-beta5
- Microsoft.Azure.WebJobs.ServiceBus 3.0.0-beta5
- Microsoft.NET.Sdk.Functions 1.0.13
- Azure Functions 核心工具2.0.1-beta.31
- 函数运行时版本:2.0.11888.0
最佳答案
正如您提到的文件所说,
The message body is required to have the following JSON format
{ "input":"<trigger_input>"}
.
您想要实现的是模拟一个消息对象,其中包含 UserProperties 字段。显然 Json 格式字符串不符合此条件。在我的测试中,整个trigger_input始终被识别为消息正文。
通常来说(不使用管理端点),我们可以在触发器中声明附加参数来接收 metadata 。所以我尝试使用管理端点,也不起作用。看起来函数 host 只接受以 input
命名的字符串,尽管请求正文中有其他参数。
所以管理端点似乎是一个死胡同。您可以使用this code sample测试服务总线触发器。
关于azure - 使用服务总线触发器在 Azure Functions 上进行本地测试时提供 UserProperties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51249592/