1

I create a azure function with IoT Hub trigger. As an example I use this Azure Functions - how to set up IoTHubTrigger for my IoTHub messages?

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"
  }
}

But when I test the function it will start but the trigger will not trigger. For testing I use

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

Stefan
  • 555
  • 5
  • 18

2 Answers2

1

The first parameter of the EventHubTriggerAttribute is eventHubName, whereas you are passing in an endpoint name.

You have to use the "Event Hub-compatible name" of your endpoint: enter image description here

The "Event Hub-compatible endpoint" should be used as a connection string.

By the way, it is recommended to use a dedicated consumer group for your trigger.

Hope this helps.

Vladislav
  • 2,772
  • 1
  • 23
  • 42
  • This is not correct. The IoTHubTrigger attribute he is using looks fine as it is. – silent Jul 30 '19 at 09:22
  • No, this is correct. (is it not possible to have two correct approaches? :) ). Refer to [this](https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-iot#trigger---attributes) and [this](https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-iot#trigger---configuration) – Vladislav Jul 30 '19 at 09:29
  • Ok, granted :) He could also use the EventHubTrigger attribute. In this case yes, he would require the eventhubname. But since he is connecting to IoT Hub and is already using the trigger for that, I would just stick with that. – silent Jul 30 '19 at 09:30
  • he *is* using `EventHubTrigger`, look at his 1st line – Vladislav Jul 30 '19 at 09:31
1

Your Function looks all good, just your connection string is the wrong one. You need the connection string from the event hub endpoint. It should look like this:

Endpoint=sb://iothub-ns-xxxxxxx.servicebus.windows.net/;SharedAccessKeyName=iothubowner;SharedAccessKey=*******;EntityPath=abc

See here for a similar example: https://github.com/sebader/iotedge-end2end/blob/master/CloudFunctions/IotHubMessageProcessor.cs

silent
  • 14,494
  • 4
  • 46
  • 86