我只是想问是否可以纯粹在本地计算机上执行 Azure 函数(事件中心触发器)而不依赖于任何 Azure 事件中心资源? 我一直在遵循 Microsoft 在本地开发 Azure Functions 的流程 ( Link ),但似乎我需要填写事件中心连接字符串和名称。
public static async Task Run([EventHubTrigger("samples-workitems", Connection = "eventhub-connectionstring")] EventData[] events, ILogger log)
有什么办法可以实现这一点吗?
最佳答案
每个绑定(bind)都有一个用于本地测试和调试的 HTTP 端点。
- https://localhost:7071/admin/functions/{FUNCNAME}
这至少适用于 QueueTrigger、ServiceBusTrigger、TimerTrigger、EventHubTrigger。
发送包含预期数据(JSON 格式)的 POST 请求。
{ "input": "YOUR JSON SERIALIZED AND ESCAPED DATA" }
对于需要数据的触发器,将数据作为序列化字符串放入“输入”中。请参阅下面的 EventHubTrigger 示例。
定时器触发器
对于 TimerTrigger 使用此:
{ "input": null }
EventGridTrigger
要在某些触发器上执行此操作有点棘手。这是 EventGridTrigger:
- https://localhost:7071/runtime/webhooks/EventGrid/functionName={FUNCNAME}
发送POST请求来执行。请参阅here了解详情。该对象必须是数组。
EventHubTrigger
EventHubTrigger 像其他触发器一样接收数据作为单个 JSON 对象。该结构遵循 EventData
类,但唯一必需的字段是“SystemProperties”。似乎没有序列化器特定设置,属性名称不更改大小写等。
将其作为正文发布;
{
"input": "{\"SystemProperties\":{},\"SomeId\":\"123\",\"Status\":\"Available\"}"
}
事件中心的主体是“input”的转义和序列化值。
Note that the same applies for the IoT Hub
元数据
对于所有触发器,您可以通过 GET 请求获取元数据。对于EventHubTrigger,这可能如下所示:
{
"name": "StateChange",
"script_root_path_href": "http://localhost:7071/admin/vfs/StateChange/",
"script_href": "http://localhost:7071/admin/vfs/bin/MyApp.Notifications.Functions.dll",
"config_href": "http://localhost:7071/admin/vfs/StateChange/function.json",
"test_data_href": null,
"href": "http://localhost:7071/admin/functions/StateChange",
"invoke_url_template": null,
"language": "DotNetAssembly",
"config": {
"generatedBy": "Microsoft.NET.Sdk.Functions-3.0.11",
"configurationSource": "attributes",
"bindings": [
{
"type": "eventHubTrigger",
"consumerGroup": "regular",
"connection": "EventHub_Hub_Status",
"eventHubName": "%EventHub_Status%",
"name": "statusUpdateMessage"
}
],
"disabled": false,
"scriptFile": "../bin/MyApp.Notifications.Functions.dll",
"entryPoint": "MyApp.Notifications.Functions.StateChange.Run"
},
"files": null,
"test_data": null,
"isDisabled": false,
"isDirect": true,
"isProxy": false
}
当然,您可以使用所有路径来检索数据,包括二进制文件。编写复杂的集成测试非常方便。
关于azure - 是否可以在不使用实际事件中心的情况下在本地执行事件中心触发的 Azure Functions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63846854/