是否可以在不使用实际事件中心的情况下在本地执行事件中心触发的 Azure Functions?
Is it possible to execute Event Hub triggered Azure Functions locally without using an actual Event Hub?
我只想问一下是否可以在不依赖任何 Azure 事件中心资源的情况下仅在我的本地计算机上执行 Azure 函数(事件中心触发器)?
我一直在关注 Microsoft 在本地开发 Azure Functions 的流程 (Link),但似乎我需要填写 Event Hub 连接字符串和名称。
public static async Task Run([EventHubTrigger("samples-workitems", Connection = "eventhub-connectionstring")] EventData[] events, ILogger log)
有什么方法可以做到这一点吗?
不,不可能。
这是因为没有官方的模拟工具
对于 httptrigger,您可以使用 postman 或仅使用一些代码来命中端点。
关于 azure storage 的触发器,可以使用本地 azure storage explorer。
但是eventhub、service bus等不创建azure资源是无法触发的。
每个绑定都有一个用于本地测试和调试的 HTTP 端点。
- https://localhost:7071/admin/functions/{FUNCNAME}
至少可用于 QueueTrigger、ServiceBusTrigger、TimerTrigger、EventHubTrigger。
发送 POST 请求,预期数据为 JSON。
{ "input": "YOUR JSON SERIALIZED AND ESCAPED DATA" }
对于需要数据的触发器,将数据作为序列化字符串放入“输入”。请参阅下面的 EventHubTrigger 示例。
定时器触发器
对于 TimerTrigger 使用这个:
{ "input": null }
事件网格触发器
要在某些触发器上执行它有点棘手。这是 EventGridTrigger:
- https://localhost:7071/runtime/webhooks/EventGrid/functionName={FUNCNAME}
发送 POST 请求执行。有关详细信息,请参阅 here。对象必须是数组。
事件中心触发器
EventHubTrigger 像其他触发器一样接收数据,作为单个 JSON 对象。该结构遵循 EventData
class,但唯一需要的字段是“SystemProperties”。似乎没有序列化程序特定的设置,属性 名称不改变大小写等
Post这个作为正文;
{
"input": "{\"SystemProperties\":{},\"SomeId\":\"123\",\"Status\":\"Available\"}"
}
事件中心的主体是“输入”的转义和序列化值。
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 函数(事件中心触发器)? 我一直在关注 Microsoft 在本地开发 Azure Functions 的流程 (Link),但似乎我需要填写 Event Hub 连接字符串和名称。
public static async Task Run([EventHubTrigger("samples-workitems", Connection = "eventhub-connectionstring")] EventData[] events, ILogger log)
有什么方法可以做到这一点吗?
不,不可能。
这是因为没有官方的模拟工具
对于 httptrigger,您可以使用 postman 或仅使用一些代码来命中端点。
关于 azure storage 的触发器,可以使用本地 azure storage explorer。
但是eventhub、service bus等不创建azure资源是无法触发的。
每个绑定都有一个用于本地测试和调试的 HTTP 端点。
- https://localhost:7071/admin/functions/{FUNCNAME}
至少可用于 QueueTrigger、ServiceBusTrigger、TimerTrigger、EventHubTrigger。
发送 POST 请求,预期数据为 JSON。
{ "input": "YOUR JSON SERIALIZED AND ESCAPED DATA" }
对于需要数据的触发器,将数据作为序列化字符串放入“输入”。请参阅下面的 EventHubTrigger 示例。
定时器触发器
对于 TimerTrigger 使用这个:
{ "input": null }
事件网格触发器
要在某些触发器上执行它有点棘手。这是 EventGridTrigger:
- https://localhost:7071/runtime/webhooks/EventGrid/functionName={FUNCNAME}
发送 POST 请求执行。有关详细信息,请参阅 here。对象必须是数组。
事件中心触发器
EventHubTrigger 像其他触发器一样接收数据,作为单个 JSON 对象。该结构遵循 EventData
class,但唯一需要的字段是“SystemProperties”。似乎没有序列化程序特定的设置,属性 名称不改变大小写等
Post这个作为正文;
{
"input": "{\"SystemProperties\":{},\"SomeId\":\"123\",\"Status\":\"Available\"}"
}
事件中心的主体是“输入”的转义和序列化值。
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
}
当然,您可以使用所有路径来检索数据,包括二进制文件。编写复杂的集成测试非常方便。