Azure Function App = run single Azure Function locally to debug
Asked Answered
R

5

23

In visual studio I have created an Azure Function App with several Function.

When I launch the Function App debugger from the tool bar all Functions are triggered.

Is there a way to trigger a single function from the App within Visual Studio 2017?

Resin answered 16/10, 2018 at 13:21 Comment(1)
Are you trying to debug a Function running locally or in Azure? What language are you using for your function, C#?Supersensitive
A
39

There is no easy way to achieve this, but it is possible.

  1. Disable functions:

by modifying the function.json file:

"bindings": [
...
],
"disabled": true

or by using the [Disable] attribute:

[Disable]
[FunctionName("Function")]
[NoAutomaticTrigger]
public static void Function(string input, TraceWriter log)
{ }
  1. func run using Azure Core Tools (only v1.x)

Run function using command: func run <functionName>

  1. Specify functions in the host.json file

In your host.json file specify functions that should be run:

{ 
   "functions":[ "FunctionToRun" ]
} 
Acre answered 16/10, 2018 at 22:6 Comment(3)
Thanks for the solution. I am using VS2019 and I tried no:3 - it is working fineLeslie
func run <functionName>, the func is an unknown command errorCozza
The host.json method works perfectlyHadria
K
10

As @Pawel Maga mentioned there are three ways.

I have a little better approach for 3rd option (Specify functions in the host.json file).

Instead of messing with host.json (we might forget to undo while publishing.. i have done it many times :p).

We can override functions array by setting value in local.settings.json.

For example: Set like below code in local.settings.json

{
  "Values": {
    "AzureFunctionsJobHost__functions__0":  "FunctionToRun",
    "AzureFunctionsJobHost__functions__1":  "SecondFunctionToRun",
  }
}

Instead of writing below code in host.json

{ 
   "functions":[ "FunctionToRun", "SecondFunctionToRun" ]
} 
Kohl answered 5/12, 2020 at 10:45 Comment(2)
I would've never figured out that the right way to pass multiple values to AzureFunctionsJobHost__functions was to break them into separate lines with __x at the end, big thanks!Complement
I think it's worth pointing out here that you can only override the individual elements. Consequently, if you want to debug just one function, you must set the other function array elements to empty string, ie: "AzureFunctionsJobHost__functions__0": "FunctionToDebug", "AzureFunctionsJobHost__functions__1": "", "AzureFunctionsJobHost__functions__2": "",Cavy
B
10

As an update to the above answers: it appears that you can disable functions in the localsettings.json in this way.

{
  "Values": {
    "AzureWebJobs.MyFirstFunction.Disabled": true,
    "AzureWebJobs.MySecondFunction.Disabled": true
  }
}

This is what I'm planning to do for our team as it has the nicest syntax of the "safe" options (approaches that don't have to be undone on publish).

See here: https://learn.microsoft.com/en-us/azure/azure-functions/disable-function?tabs=portal#localsettingsjson

Bridgman answered 16/9, 2021 at 18:20 Comment(1)
This is exactly what I needed! It also allows me to move RunOnStartup to local.settings.json as well. "AzureWebJobs.MyFirstFunction.RunOnStartup": trueInglorious
N
0

This is currently the easiest way to run only a subset of the functions via the terminal:

func start --functions <space separated list of functions>

From func help

--functions A space seperated list of functions to load.

Nedrud answered 5/4 at 14:55 Comment(0)
C
0

I have found that you can't override the "Functions" array in the host.json file, but can only override individual values in the array. Consequently, this is what I had to do to disable all but one function:

local.settings.json

{
    "Values": {
        "AzureFunctionsJobHost__functions__0": "FunctionToTest",
        "AzureFunctionsJobHost__functions__1": "",
        "AzureFunctionsJobHost__functions__2": "",
        "AzureFunctionsJobHost__functions__3": "",
        "AzureFunctionsJobHost__functions__4": "",
        "AzureFunctionsJobHost__functions__5": "",
        "AzureFunctionsJobHost__functions__6": "",
        "AzureFunctionsJobHost__functions__7": "",
        "AzureFunctionsJobHost__functions__8": "",
        "AzureFunctionsJobHost__functions__10": "",
        "AzureFunctionsJobHost__functions__11": "",
        "AzureFunctionsJobHost__functions__12": "",
        "AzureFunctionsJobHost__functions__13": ""
    }
}

I know you can also use the format: "AzureWebJobs..Disabled": true

However, I'd rather not match/maintain that list of names with the function name in the code.

Cavy answered 3/6 at 18:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.