是否可以在不使用实际事件中心的情况下在本地执行事件中心触发的 Azure Functions?

The*_*ong 3 azure azure-eventhub azure-functions

我只是想问是否可以纯粹在本地计算机上执行 Azure 函数(事件中心触发器)而不依赖于任何 Azure 事件中心资源?我一直在关注 Microsoft 在本地开发 Azure Functions 的流程(链接),但似乎我需要填写事件中心连接字符串和名称。

public static async Task Run([EventHubTrigger("samples-workitems", Connection = "eventhub-connectionstring")] EventData[] events, ILogger log)

有什么办法可以做到这一点吗?

Joe*_*use 10

每个绑定都有一个用于本地测试和调试的 HTTP 端点。

  • https://localhost:7071/admin/functions/{FUNCNAME}

这至少适用于 QueueTrigger、ServiceBusTrigger、TimerTrigger、EventHubTrigger。

发送带有 JSON 格式的预期数据的 POST 请求。

{ "input": "YOUR JSON SERIALIZED AND ESCAPED DATA" }
Run Code Online (Sandbox Code Playgroud)

对于需要数据的触发器,将数据作为序列化字符串放入“输入”中。请参阅下面的EventHubTrigger示例。

定时器触发

对于 TimerTrigger 使用:

{ "input": null }
Run Code Online (Sandbox Code Playgroud)

事件网格触发器

要在某些触发器上执行此操作有点棘手。这是EventGridTrigger的:

  • https://localhost:7071/runtime/webhooks/EventGrid/functionName={FUNCNAME}

发送POST请求来执行。详细信息请参见此处。该对象必须是一个数组。

事件中心触发器

EventHubTrigger像其他触发器一样接收数据作为单个 JSON 对象结构遵循EventData类,但唯一必需的字段是“SystemProperties”。似乎没有序列化器特定的设置,属性名称不改变大小写等。

将其作为正文发布;

{
    "input": "{\"SystemProperties\":{},\"SomeId\":\"123\",\"Status\":\"Available\"}"
}
Run Code Online (Sandbox Code Playgroud)

事件中心的主体是“输入”的转义和序列化值。

请注意,这同样适用于 IoT 中心

元数据

对于所有触发器,您可以通过 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
}
Run Code Online (Sandbox Code Playgroud)

当然,您可以使用所有路径来检索数据,包括二进制文件。编写复杂的集成测试非常方便。

  • 我仔细研究了十亿多篇微软文章来寻找这些信息,但一无所获。你在 StackOverflow 的回答中很好地解释了这一点。无论您在哪里挖掘出此信息 - 感谢您分享! (2认同)