Jon
Jon

Reputation: 1115

Problems with Azure Function App triggered by IoT Hub using Visual Studio Code

I want to develop an azure function to process D2C messages from IoT Hub. Previously I developed a function, coding directly on the portal, and I could see logs printed by the function for each message I send using a desktop app I developed. But I need to implement more complex actions, so I decided to do the same but using Visual Studio Code. I installed the neccessary extensions for that and I created a new project following the steps from bellow:

In .vscode/settings.json file I changed "azureFunctions.projectRuntime" value from "~3" to "~2", to avoid this warning message in deployment process:

Visual Studio Code warning

This the default file created:

using IoTHubTrigger = Microsoft.Azure.WebJobs.EventHubTriggerAttribute;

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.EventHubs;
using System.Text;
using System.Net.Http;
using Microsoft.Extensions.Logging;

namespace MyNamespace
{
    public static class MyFunctionName
    {
        private static HttpClient client = new HttpClient();

        [FunctionName("MyFunctionName")]
        public static void Run([IoTHubTrigger("messages/events", Connection = "")]EventData message, ILogger log)
        {
            log.LogInformation($"C# IoT Hub trigger function processed a message: {Encoding.UTF8.GetString(message.Body.Array)}");
        }
    }
}

I think that I'm missing the connection string. I can find it in local.settings.json file, the AzureWebJobsStorage value (some values removed):

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName={xxxxx};AccountKey={yyyyy};EndpointSuffix=core.windows.net",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet"
    }
}

I think that I have to fill the Connection parameter with AzureWebJobsStorage value:

public static void Run([IoTHubTrigger("messages/events", Connection = "AzureWebJobsStorage")]EventData message, ILogger log)

But where do I need to define this AzureWebJobsStorage value to use it when running on Azure? If I deploy this function, I don't see any log when I send D2C message.

Upvotes: 1

Views: 460

Answers (1)

Matthijs van der Veer
Matthijs van der Veer

Reputation: 4085

If I understand your question right, you want to know where to define configuration values when running your Functions in Azure? You can do that by going to the Azure Portal, navigating to your Function and clicking on Configuration in the menu (it's under settings). Azure Portal blade

From there, you can enter a new application setting with the name AzureWebJobsStorage and the connection string to your storage account. Without that, you're definitely not going to see any logging, because your Function just won't run.

Note: Thanks for being so thorough in your question! However, a side note, I wonder why changing azureFunctions.projectRuntime is necessary?

Upvotes: 1

Related Questions