4

I am an azure novice. My setup is as follows. I've got an IoT Hub, An Event Hub, an Azure function. The idea is to send messages to the IoT hub, route them to the Event Hub based on a certain message type, handle these events with the use of the function.

I created a sample console app to send messages from my machine to the IoT hub. Created an azure function on Visual Studio (.Net Core) and published it to an azure function app.

Now, when I run the console app project and the function app projects together , I see the messages being received on the IoT hub (by looking at the azure portal), and the function getting triggered and receiving the message that I sent (by looking at the console that pops up on my machine, with the function app).

My issue is, when I then check the azure portal, I see the function execution count as 0, even though I can see the function is picking the events when I check locally. Any idea why this might be the case?

Event received by the function

zero executions shown on azure portal

The function code is based on the template that comes with Visual Studio

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.EventHubs;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace FunctionApp2
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task Run([EventHubTrigger("azureiotquickstarteventhub", Connection = "AzureEventHubConnectionString")] EventData[] events, ILogger log)
        {
            var exceptions = new List<Exception>();

            foreach (EventData eventData in events)
            {
                try
                {
                    string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);

                    // Replace these two lines with your processing logic.
                    log.LogInformation($"C# Event Hub trigger function processed a message: {messageBody}");
                    await Task.Yield();
                }
                catch (Exception e)
                {
                    // We need to keep processing the rest of the batch - capture this exception and continue.
                    // Also, consider capturing details of the message that failed processing so it can be processed again later.
                    exceptions.Add(e);
                }
            }

            // Once processing of the batch is complete, if any messages in the batch failed processing throw an exception so that there is a record of the failure.

            if (exceptions.Count > 1)
                throw new AggregateException(exceptions);

            if (exceptions.Count == 1)
                throw exceptions.Single();
        }
    }
}
  • Have you configured your application settings in your function app? Can you also share your function code here? – Serkant Karaca Sep 30 '20 at 15:05
  • when you run the function app locally for debugging purposes, why would you want to see the execution count on the Azure portal side? Stop the local debug of the function app and let the published function app get triggered from the IoTHub events and then check the execution count in the portal. – SatishBoddu Sep 30 '20 at 21:04
  • @SatishBoddu-MSFT When I don't run the local azure function, I should expect the published function on azure to get triggered yeah? This is what I tried. I run the console app which sends the messages, IoT Hub receives the messages, but the function doesn't seem to run on Azure – Miraj Mohajireen Sep 30 '20 at 23:08
  • @SerkantKaraca I updated the question with the function code – Miraj Mohajireen Sep 30 '20 at 23:16

1 Answers1

0

If it works fine at local, but not being executed in azure portal, then please check if the "AzureEventHubConnectionString" is specified in azure portal -> your function app configuration. The steps as below:

Nav to azure portal -> go to your function app -> Configuration -> Application settings, on that page, please check if "AzureEventHubConnectionString" is specified and it should have the correct value. The screenshot as below:

enter image description here

By the way, you'd better enable Application Insights for your function app which can give you better monitor experience. You can follow the screenshot below to enable Application Insights:

1.In azure portal, click your function app instance like EventhubTrigger1:

enter image description here

2.Then click the Monitor -> click the Configure button to enable application insights:

enter image description here

After that, when I execute the function, I can see it's execution on azure portal:

enter image description here

Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60