Things I Learned #21: Running specific Azure Functions in a solution that has multiple

2 minute read til learning   dotnet   azure Comments

Day to day, I work in a Visual Studio solution that has a dozen or so projects. One of the projects holds six or seven Azure Functions. Some of the functions are triggered by messages showing up in a ServiceBus queue, some are triggered by HTTP POSTs, and some are triggered by timers.

There are times when I’m debugging that I don’t necessarily want all of the functions running, especially those that are on a timer. Sure, I could modify the cron string to not have them fire as often, but sometimes I just want to debug a single function in isolation without having to worry about the others being executed.

TIL

In the host.json file within the project, it’s as simple as adding a new section for “functions” and listing the specific functions you want to execute when running from Visual Studio. If the section doesn’t exist, or the array is empty, all functions will be executed. This is intended for use when running the functions locally.

I created an example project to illustrate what I’m talking about. The functions in this project are WebhookFunction, PollingFunction, and QueueProcessingTrigger.

By default, the host.json will look something like this:

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            }
        }
    }
}

As you can see, there is no section for functions, so all functions will execute when the project is run.

If I only want to run the WebhookFunction when I hit F5, I can add it to the “functions” array. Anything else will be ignored.

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            }
        }
    },
    "functions": [ "WebhookFunction" ],
}

When I’m done with my focused testing, I can revert the changes to this file, clear the array, or delete the entire “functions” section.

Bonus Tip

There is another way to disable specific functions from running in Visual Studio. You can add a line per function to your local.settings.json to disable the function. Where the solution above won’t attempt to run them, this method seems to be a bit different in that it shows the function is disabled when running the project.

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
        "AzureWebJobs.PollingFunction.Disabled":  true  
    }
}

In this case, I set the PollingFunction to disabled. Notice in the Azure console, it shows it’s disabled versus the earlier screenshot of the console where it didn’t even mention the PollingFunction.

I hope you found this to be informative and useful, and I hope it makes your debugging sessions with Azure Functions a bit simpler!


A seal indicating this page was written by a human

Updated:

Comments