Azure Functions - IoTHubTrigger 但它不会触发
Azure Functions - IoTHubTrigger But It will not trigger
我使用 IoT 中心触发器创建了一个 azure 函数。作为示例,我使用此
Function1.cs
using IoTHubTrigger = Microsoft.Azure.WebJobs.ServiceBus.EventHubTriggerAttribute;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.ServiceBus.Messaging;
using System.Text;
using System.Net.Http;
namespace LogTheIoTHubMessage
{
public static class Function1
{
private static HttpClient client = new HttpClient();
[FunctionName("Function1")]
public static void Run([IoTHubTrigger("messages/events", Connection = "ConnectionString")]EventData message, TraceWriter log)
{
log.Info($"C# IoT Hub trigger function processed a message: {Encoding.UTF8.GetString(message.GetBytes())}");
}
}
}
local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"ConnectionString": "HostName=AAA.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=YYYYYY"
}
}
但是当我测试功能时它会启动但触发器不会触发。为了测试我使用
C:\Program Files\mosquitto>mosquitto_pub -d -h AAA.azure-devices.net
-i TestRaspberryPi -u "AAA.azure-devices.net/TestRaspberryPi" -P "SharedAccessSignature sr=YYY" -m "noch ein test" -t
"devices/TestRaspberryPi/messages/events/readpipe/" --cafile
"c:\Projects\azureiot.pem" -p 8883 -V mqttv311
Client TestRaspberryPi sending CONNECT Client TestRaspberryPi received
CONNACK (0) Client TestRaspberryPi sending PUBLISH (d0, q0, r0, m1,
'devices/TestRaspberryPi/messages/events/readpipe/', ... (13 bytes))
Client TestRaspberryPi sending DISCONNECT
EventHubTriggerAttribute
的第一个参数是 eventHubName
,而您传递的是端点名称。
您必须使用端点的 "Event Hub-compatible name":
"Event Hub-compatible endpoint" 应该用作连接字符串。
顺便说一句,建议您为触发器使用专用的消费者组。
希望对您有所帮助。
您的函数看起来一切正常,只是您的连接字符串错误。您需要来自事件中心终结点的连接字符串。它应该是这样的:
Endpoint=sb://iothub-ns-xxxxxxx.servicebus.windows.net/;SharedAccessKeyName=iothubowner;SharedAccessKey=*******;EntityPath=abc
有关类似示例,请参见此处:https://github.com/sebader/iotedge-end2end/blob/master/CloudFunctions/IotHubMessageProcessor.cs
我使用 IoT 中心触发器创建了一个 azure 函数。作为示例,我使用此
Function1.cs
using IoTHubTrigger = Microsoft.Azure.WebJobs.ServiceBus.EventHubTriggerAttribute;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.ServiceBus.Messaging;
using System.Text;
using System.Net.Http;
namespace LogTheIoTHubMessage
{
public static class Function1
{
private static HttpClient client = new HttpClient();
[FunctionName("Function1")]
public static void Run([IoTHubTrigger("messages/events", Connection = "ConnectionString")]EventData message, TraceWriter log)
{
log.Info($"C# IoT Hub trigger function processed a message: {Encoding.UTF8.GetString(message.GetBytes())}");
}
}
}
local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"ConnectionString": "HostName=AAA.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=YYYYYY"
}
}
但是当我测试功能时它会启动但触发器不会触发。为了测试我使用
C:\Program Files\mosquitto>mosquitto_pub -d -h AAA.azure-devices.net -i TestRaspberryPi -u "AAA.azure-devices.net/TestRaspberryPi" -P "SharedAccessSignature sr=YYY" -m "noch ein test" -t "devices/TestRaspberryPi/messages/events/readpipe/" --cafile "c:\Projects\azureiot.pem" -p 8883 -V mqttv311
Client TestRaspberryPi sending CONNECT Client TestRaspberryPi received CONNACK (0) Client TestRaspberryPi sending PUBLISH (d0, q0, r0, m1, 'devices/TestRaspberryPi/messages/events/readpipe/', ... (13 bytes)) Client TestRaspberryPi sending DISCONNECT
EventHubTriggerAttribute
的第一个参数是 eventHubName
,而您传递的是端点名称。
您必须使用端点的 "Event Hub-compatible name":
"Event Hub-compatible endpoint" 应该用作连接字符串。
顺便说一句,建议您为触发器使用专用的消费者组。
希望对您有所帮助。
您的函数看起来一切正常,只是您的连接字符串错误。您需要来自事件中心终结点的连接字符串。它应该是这样的:
Endpoint=sb://iothub-ns-xxxxxxx.servicebus.windows.net/;SharedAccessKeyName=iothubowner;SharedAccessKey=*******;EntityPath=abc
有关类似示例,请参见此处:https://github.com/sebader/iotedge-end2end/blob/master/CloudFunctions/IotHubMessageProcessor.cs