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();
}
}
}
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:
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
:
2.Then click the Monitor
-> click the Configure
button to enable application insights:
After that, when I execute the function, I can see it's execution on azure portal: