Azure Functions - 如何为我的 IoTHub 消息设置 IoTHubTrigger? [英] Azure Functions - how to set up IoTHubTrigger for my IoTHub messages?
问题描述
如何正确设置和配置 IoTHubTrigger 以触发我的 IoTHub 消息的 Azure 函数 (C#)?在何处以及如何插入 IoTHub 的连接字符串?
使用 Visual Studio 2017 的步骤:
- 首先确保您拥有最新版本的 Azure Functions 和 Web 作业工具
转到 File->New->Project->Azure Functions 并选择IoT Hub Trigger"
选择函数 V1 或 V2(了解其中的差异
如果使用 Functions V2,请使用从这里的门户中获取的 IoTHub 的 EventHub 兼容端点:
- 现在在您的函数中设置一个断点并按 F5.您将看到您的消息从 IoTHub 流向您的 Azure 函数(假设您连接了正在发送数据的设备或模拟器)
使用 Azure 门户的步骤
创建新的函数应用资源并选择 EventHub 触发器模板
为 EventHub 连接点击新建"并选择 IotHub 和您想要的集线器
编辑并保存您的函数代码 - 您现在可以开始运行了!
切换到监控"查看您的事件流入
创建 IoTHub 触发 Azure 函数的更多选项
a) 将 VS Code 与 Azure 函数扩展一起使用
b) 从命令行使用 Azure Functions Core ToolsHow do I setup and configure an IoTHubTrigger correctly to trigger an Azure Function (C#) for my IoTHub messages? Where and how do I plug in my IoTHub's connection string?
解决方案Steps using Visual Studio 2017:
- First make sure you have the latest version of the Azure Functions and Web Jobs Tools
Go to File->New->Project->Azure Functions and select "IoT Hub Trigger"
Select Functions V1 or V2 (learn about there differences here). And enter an arbitrary name that will serve as key for your connection string configuration.
Open local.settings.json and enter a key/value pair for your connection string:
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "AzureWebJobsDashboard": "UseDevelopmentStorage=true", "ConnectionString": "<your connection string>" } }
IMPORTANT
If using Functions V1, use your IoTHub connection string obtained in the portal from here:
If using Functions V2, use your IoTHub's EventHub compatible endpoint obtained in the portal from here:
- Now set a breakpoint in your function and hit F5. You will see your messages flowing from IoTHub to your Azure Function (assuming you have devices or simulators connected that are sending data)
Steps using the Azure Portal
Create a new Function App resource and select the EventHub Trigger template
Hit "New" for EventHub Connection and select IotHub and your desired hub
Edit and save your function code - you are now up and running!
Switch to "Monitor" see your events flowing in
More options to create IoTHub Trigger Azure Functions
a) Using VS Code with the Azure Functions Extension
b) From the command line using Azure Functions Core Tools这篇关于Azure Functions - 如何为我的 IoTHub 消息设置 IoTHubTrigger?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!