Azure Functions - how to set up IoTHubTrigger for my IoTHub messages? - azure

How do I setup and configure an IoTHubTrigger correctly to trigger an Azure Function (C#) for my IoTHub messages? Where and how do I plug in my IoTHub's connection string?

Steps using Visual Studio 2017:
First make sure you have the latest version of the Azure Functions and Web Jobs Tools
Go to File->New->Project->Azure Functions and select "IoT Hub Trigger"
Select Functions V1 or V2 (learn about there differences here). And enter an arbitrary name that will serve as key for your connection string configuration.
Open local.settings.json and enter a key/value pair for your connection string:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"ConnectionString": "<your connection string>"
}
}
IMPORTANT
If using Functions V1, use your IoTHub connection string obtained in the portal from here:
If using Functions V2, use your IoTHub's EventHub compatible endpoint obtained in the portal from here:
Now set a breakpoint in your function and hit F5. You will see your messages flowing from IoTHub to your Azure Function (assuming you have devices or simulators connected that are sending data)
Steps using the Azure Portal
Create a new Function App resource and select the EventHub Trigger template
Hit "New" for EventHub Connection and select IotHub and your desired hub
Edit and save your function code - you are now up and running!
Switch to "Monitor" see your events flowing in
More options to create IoTHub Trigger Azure Functions
a) Using VS Code with the Azure Functions Extension
b) From the command line using Azure Functions Core Tools

I also needed to install a NuGet package Microsoft.Azure.WebJobs.Extensions.EventHubs

I would like to add, if you want to publish the function on Azure, you must add the connectionstring for the portal side

Updating the thread for cloud deployment issue. If you don't find an option to pass the connection string through the publish pop up window in Visual Studio 2022, you can provide the configuration from the Azure portal UI.
Navigate to the Azure function resource on the Azure portal and click on the Configuration under thh Settings section. Click the New Application Setting button on top and add in the connection string parameter name (same as the value set to Connection in the Azure function declaration) and provide the event hub end point connection string as value.

Related

How to set value for EventHub and AzureWebJobsStorage

I have created an event hub and storage account in the Azure portal and written an azure function with an eventhub trigger to trigger the function.
To run this function locally, I set the "AzureWebJobsStorage" and "EventHubconnectionstring" in local.settings.json and this works fine.
If I deploy this function to azure portal, how can I set this "EventHubconnectionstring" and "AzureWebJobsStorage" values ? does it picked up automatically?
When you deploy it from Visual Studio the values become null. Then you need to open Configuration section in function app and enter those values there as below:
If you deploy using Vs code the values will be reflected in configuration section.
If you face the issue as empty connection string, then you need to add it in Configuration section then save it.And you should do the same for event hub connection string.
References:
Storage account connection string does not exist - deploying Queue storage trigger for Azure Functions - Stack Overflow

Where do Azure WebJobs read configuration settings from?

I’m developing an Azure WebJob with Visual Studio 2019, with Microsoft.NETCore.App v5.0.0 framework. I need to read values from configuration, but I don’t understand where I should put them to finally overwrite them in the Azure WebJob’s page in the Azure portal. So far, I’ve created the following appsettings.json:
{
"ConnectionStrings": {
"AzureWebJobsStorage": "..."
},
"firstValue": "...",
"secondValue": "..."
}
The following public method
public static async Task Example(
[BlobTrigger("%firstValue%/{blobName}")] Stream blobReceived
[Blob("%secondValue%/{blobName}", FileAccess.Write)] Stream blobToWrite,
ILogger logger)
Correctly reads the values of firstValue and secondValue from appsettings.json. That said, I see other projects that use app.config files, whose values are then retrieved with something like:
ConfigurationManager.AppSettings["..."];
ConfigurationManager.ConnectionStrings["..."].ConnectionString;
Should I use app.config instead of appsettings.json? How do I read values I put in the appsettings.json file when we speak about Azure WebJobs?
You cannot access the app settings from Azure WebJobs directly, but you could consider using rest API to access the AppSettings in the Azure portal.
Actually, WebJobs are console app, which uses the environment variables stored in the system, which is different from the AppSettings in the Azure portal. AppSettings stored in IIS can only be invoked from this specific web app.

Azure WebJobs SDK ServiceBus connection string 'AzureWebJobsAzureSBConnection' is missing or empty

I created an Azure Function App in Visual Studio 2015. The App has a trigger for service bus queues. The app works perfectly when I run it locally. It is able to read the data from the Service Bus queue (configured via a variable named AzureSBConnection) and log it in my database.
But it gives me the following error when deployed in Azure:
Function ($ServiceBusQueueTriggerFunction) Error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.ServiceBusQueueTriggerFunction'. Microsoft.Azure.WebJobs.ServiceBus: Microsoft Azure WebJobs SDK ServiceBus connection string 'AzureWebJobsAzureSBConnection' is missing or empty.
Note that my connection is called AzureSBConnection and not AzureWebJobsAzureSBConnection. Also, the connection works locally. And finally, the deployed file looks exactly like the local file.
The Visual Studio structure looks like the following:
The function.json file has a bunch of settings as shown below:
Then in the Appsettings.json file, I have the following:
For deploying, I FTPed the files to the D:\home\site\wwwroot location for my Function App in Azure. The final structure in Kudu looks like:
And if I go inside my function folder:
Here is the deployed function.json:
And here is the deployed appsettings:
The deployed json files are exactly the same as the local files. But the deployed version is erroring out because of the missing AzureWebJobsAzureSBConnection. What am I doing wrong?
Only environment variables are supported for app settings and connection strings.
You need to make sure that the environment variable AzureWebJobsAzureSBConnection is set on your Function's app settings in the portal:
and then once there, you need to add the AzureWebJobsAzureSBConnection variable with the proper connection string:
and then you can access this via code by:
Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
This will obtain the value from either the appsettings.json or the environment variable depending on where the function is being executed from, (local debugging or deployed on Azure)
It is able to read the data from the Service Bus queue (configured via a variable named AzureSBConnection) But it gives me the following error when deployed in Azure:
After you deployed your application to Azure Function, your application will read the connection string from environment setting. Currently, connection settings in appsettings.json will not update environment setting automatically. We could click [Configure app settings] button as #flyte mentioned to check whether the connection string is configured successfully. If not, you could add it manually in app setting box.
Note that my connection is called AzureSBConnection and not AzureWebJobsAzureSBConnection
Please go to [Integrate] page to check whether the [Service Bus connection] is configured successfully. If not, you could reset it by clicking the [new] link.

How to write azure function that can get triggered when an item appears in azure service bus queue using C#?

The new azure function preview contains a few templates for C#. But there is no service bus queue template for C#. There is a trigger template for node with service bus. But on close inspection, it only supported Notification Hubs and not service bus queue. Is it even possible to write an azure function that can only be triggered when an item appears in azure service bus queue? If it is not possible now, will there be such a template in the near future?
Thanks.
Raghu/..
Update: The below steps and information still hold, however we now have a "ServiceBusQueueTrigger - C#" template live in the portal, so the workaround steps are no longer necessary :)
ServiceBus IS supported already for C#, we just need to add a template for it (we'll add very soon). In general, templates are just starting points - you can always modify templates by adding additional bindings, or start from the empty template and build up your own Function.
Until we get the template uploaded, you can get this working yourself by starting from the C# Empty template. For example, you can enter binding info like the following in the Advanced Editor on the Integrate tab:
{
"bindings": [
{
"type": "serviceBusTrigger",
"name": "message",
"direction": "in",
"queueName": "samples-input",
"connection": "myServiceBus"
}
]
}
Make sure your Function App has an AppSetting matching the name of the connection property, containing your ServiceBus connection string. It looks like we currently have some issues with the connection string picker for ServiceBus (which will also be fixed very soon), but you can use "Function app settings"/"Go to App Service Settings"/"Application Settings" to add this app setting. Then you can use the corresponding Function code:
using System;
using Microsoft.Azure.WebJobs.Host;
public static void Run(string message, TraceWriter log)
{
log.Verbose($"C# ServiceBus Queue function processed message: {message}");
}
This function will then be invoked whenever new messages are added to ServiceBus queue samples-input.
Per https://azure.microsoft.com/en-us/documentation/articles/functions-reference/, there is no binding with the SB.
The best way to do that instead of doing something that is (at least by some chance) being in work in product group is to submit your idea on the UserVoice - https://feedback.azure.com/forums/355860-azure-functions .

deploying Azure webrole to the cloud, but dont understand dataconnection string (for queues)

I have written and successfully deployed a test app to the azure cloud, but I am lost now that I have added a queue to the application.
Currently I using a configuration string:
Setting name="DataConnectionString" value="UseDevelopmentStorage=true"
then create/open the queue with the following code:
var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
var queueClient = storageAccount.CreateCloudQueueClient();
var queue = queueClient.GetQueueReference("messagequeue");
queue.CreateIfNotExist();
This works fine in local mode, however,
I do not undertsand how to change the DataConnectionString to use the cloud!
I have tried:
Setting name="DataConnectionString" value="DefaultEndpointsProtocol=http;AccountName=*XXXXX*;AccountKey=*YYYYY*"
but this does not work - it wont run locally.
Help is certainly appreciated!
Thanks
You'll need to make sure you've created a hosted azure storage service via the Windows Azure portal. When creating the storage service, you provide the account name and the system will assign two keys. Use these two values in your connection string settings. You can either manually edit the string in the service configuration, or my preferred approach is to set it via the role's property settings. Simply right click on the role in the cloud service project in visual studio, then select properties. You'll be able to access the role's settings via one of the tabs. Use the provided dialog box to modify the connection string by inputing the account name and connection string for your storage service.

Resources