1

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:

  • I selected a new folder
  • Language: C#
  • Template: IotHubTrigger
  • Function name: MyFunctionName
  • Namespace: MyNamespace
  • Create new local app setting file
  • Event hub namespace: Skip for now
  • Name of iot hub endpoint to which messages will be sent: messages/events
  • I selected the storage account used by my azure function.

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.

Jon
  • 891
  • 13
  • 32

1 Answers1

1

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?

Matthijs van der Veer
  • 3,865
  • 2
  • 12
  • 22
  • Thank you I checked the configuration and AzureWebJobsStorage was there, so the connection string I used was wrong. I realized that there is another value named myappname_events_IOTHUB, with this value: ```Endpoint=sb://{xxxxx}namespace.servicebus.windows.net/;SharedAccessKeyName=iothubowner;SharedAccessKey={yyyyyy};EntityPath={zzzzz}```. If I use this it works fine! – Jon Oct 22 '20 at 13:55
  • I edited the question to show you the warning window. This is the reason to change ```azureFunctions.projectRuntime```. Anyway, it works with ```~2``` and ```~3``` – Jon Oct 22 '20 at 13:57