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:
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.