c# - 将 Azure Function 连接到 IoT Hub 云到设备反馈端点
问题描述
是否有可能以某种方式将 Azure Function 连接到 IoT Hub 云到设备反馈端点?此终结点似乎与 Azure 事件中心不兼容。
编写自定义事件触发器?
我使用 C# Azure 函数。
解决方案
是的,您可以为 IoT 中心创建自定义函数。每当 IoT 中心为事件中心兼容终结点提供新消息时,都会运行此函数。您可以按照以下步骤操作:
- 使用 IoT 中心(事件中心)模板创建自定义函数。
创建一个名为project.json的 json 文件,其内容如下:
{ "frameworks": { "net46":{ "dependencies": { "Microsoft.Azure.Devices": "1.4.1" } } } }
上传 project.json 文件,用于引用 Microsoft.Azure.Devices 的程序集。您可以查看此文档以获取更多信息。
将 run.csx 修改为以下代码:
#r "Microsoft.ServiceBus" using System.Configuration; using System.Text; using System.Net; using Microsoft.Azure.Devices; using Microsoft.ServiceBus.Messaging; using Newtonsoft.Json; static Microsoft.Azure.Devices.ServiceClient client = ServiceClient.CreateFromConnectionString(ConfigurationManager.AppSettings["iothubConnectionstring"]); public static async void Run(EventData myIoTHubMessage, TraceWriter log) { log.Info($"{myIoTHubMessage.SystemProperties["iothub-connection-device-id"]}"); var deviceId = myIoTHubMessage.SystemProperties["iothub-connection-device-id"].ToString(); var msg = JsonConvert.SerializeObject(new { temp = 20.5 }); var c2dmsg = new Microsoft.Azure.Devices.Message(Encoding.ASCII.GetBytes(msg)); await client.SendAsync(deviceId, c2dmsg); }
保存并运行该函数后,如果 IoT Hub 下发了一条新消息,该函数将被触发,并在该函数中发送一条云到设备的消息。
推荐阅读
- autodesk-forge - 如何列出特定存储桶的对象?
- c# - 如何使用 c# 将信件从 Exchange 移动到共享点库?
- javascript - 如何将此数据拆分为供应商和价格并将其分别存储在二维数组中
- z3 - 是否可以将两个字符串与 Z3 进行比较?
- c# - Entity Framework Core 在控制台应用程序中获取上下文
- c# - C# 中的语义解释 SRGS
- java - Querydsl:如何选择特定列
- curl - 将请求从 chrome 复制到 curl 时出现 403
- php - 如何在 API 使用 php 中的 JSON 中使用 for 循环
- c# - Convert.ChangeType to DateTime 在不同的机器上返回不同的结果