Run Multiple Azure Functions Locally

Run Multiple Azure Functions Locally

In this Azure tutorial, we will discuss Run Multiple Azure Functions Locally. Along with this, we will also discuss the below topics.

  • Run Azure Functions Locally
  • Port 7071 is unavailable. Close the process using that port, or specify another port using –port [-p]
  • Run Azure Functions Locally Visual Studio Code
  • Debugging one Function app using visual studio 2019
  • Debug Azure Functions Visual Studio Code
  • Debug Azure functions visual studio 2019
  • Debugging Multiple Azure function Apps In Visual Studio 2019

Run Multiple Azure Functions Locally

Well, we can create an Azure function using Visual Studio and We can run it easily. It will not create any issue while running. It is cool to run a single Azure Function. It will by default run on the local port 7071. By default, the local HTTP triggered Azure functions host listens for HTTP requests on port 7071.

Run Azure Functions Locally

Before discussing, Run Multiple Azure Functions Locally, Now let’s discuss Run one Azure Functions locally. let’s check out this. I have created one function in my Visual Studio 2019 named HTTPAzureFunction. Below is the code for my Azure Function

[FunctionName("HTTPAzureFunction")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null )] HttpRequest req, ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            return new OkObjectResult(responseMessage);
        }
    }
Run Multiple Azure Functions Locally

Now, I am trying to run it and it ran successfully without any issue and provided us the HTTP Functions URL http://localhost:7071/api/HTTPAzureFunction. If you will look at this URL closely, you can see It is pointing to the port 7071 by default.

How to run multiple Azure Functions locally

Now, what if I have one more Azure function I am trying to run at the same time. That means the scenario is running multiple Azure Functions at the same time with Visual Studio 2019. Let’s check out what is happening. Now My First Azure Function is already running at this point in time. Now my second function is named as Function1 and contains the code as follows

 [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
          
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            return name != null
                ? (ActionResult)new OkObjectResult($"Hello, {name}")
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
        }
how to run two azure function locally

Now I am trying to run my second Azure function that is Function1. Just to inform you guys till now my first function is already running in my local machine.

You can able to see I got the error saying ” Port 7071 is unavailable. Close the process using that port, or specify another port using –port [-p] “. or you might also get the error like below with the same scenario

Cannot access a disposed object.
Object name: ‘IServiceProvider’

Port 7071 is unavailable. Close the process using that port, or specify another port using –port [-p]

run azure function locally on different port

So it is not allowing me to run my 2nd Azure function with the same port 7071. This is the default port when you are running an Azure Function in your local machine. So to over Come this type of scenario or to fix the above issue what we need to do is, we need to tell the functions host to run on a different port and you have to manually configure the other port for your 2nd Azure function to run.

Run Azure Function Locally On Different Port

To configure the other port for your 2nd Azure Function, you need to do the below changes

Right click on the 2nd Azure Function project name and click on the Properties

running 2 azure functions locally

Now On the Properties window, click on the Debug tab and add the below line of Argument in the Application arguments option like below. You can refer to the below screenshot for your reference.

host start –pause-on-error –port 5860

host start --pause-on-error --port 5860

After you have added, the above argument then click on the Save button on the top to save the changes.

run azure functions locally visual studio 2019

Now if you will try running the 2nd Azure function, you can see it ran successfully and Provided me the below URL

http://localhost:5860/api/Function1

So Now if you will closely look at the above URL of the Azure function, you can see it is running on the port 5860 instead of port 7071.

azure function port 7071 is unavailable 1

This is how you can Run Multiple Azure Functions Locally.

Run Azure Functions Locally Visual Studio Code

Like you are running your Azure Functions Locally using the Visual Studio IDE, the same way, you can also run your Azure functions using Visual Studio Code.

I have created an Azure function in Visual Studio Code. Below is my Azure function code

[FunctionName("MyNewTest")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            return new OkObjectResult(responseMessage);
        }
    }
Run Azure Functions Locally Visual Studio Code

Now To run the Azure function in Visual Studio code, you can press the F5 button or You need to click on the Run –> Start Debugging option as shown below.

how to run azure function in visual studio code

Now you can see below, the Azure Function Project ran successfully with out any issue and Provided us with the below function URL

http://localhost:7071/api/MyNewTest

Run Azure Functions Locally using Visual Studio Code

In this way, you can Run Azure Functions Locally in Visual Studio Code.

Debug Azure Functions Visual Studio Code

Like we can able to debug our Azure Functions quickly using the Visual Studio 2019. The same way we can able to debug Azure Functions using Visual Studio Code.

Debug Azure Functions Visual Studio 2019

One excellent feature that the Azure Functions provides is that we can able to debug our Azure functions with the help of Visual Studio IDE or with Visual Studio Code.

Now we will discuss How to debug a function app using the Visual Studio 2019.

You can easily debug your Azure function by putting a breakpoint in your Azure function code and then run your Azure Function project by pressing F5. Now you will get the Azure Function URL, you try accessing the URL in any of your favorites Browsers. Once you will put the URL in the Browser and hit the Enter button. You can able to see it will hit the debugger in your code. Let’s check this complete debugging scenario

Now put the debugger in your Azure Function code as shown below

Debugging one Function app using visual studio 2019

The next step is to run the Azure Function Project by pressing the F5 button and copy the Function URL that you will get after the successful execution of the project. You can able to see below I got the function URL as below

http://localhost:7071/api/HTTPAzureFunction. Copy the this URL.

azure function change localhost port

Now Open your Browser and and try accessing the URL

Debugging multiple Azure Functions apps at the same time

Now you can able to see the, After your try accessing the Azure Function URL, it hit the breakpoint in the Azure Function code.

How to run Azure Function app on a different port in Visual Studio

So we saw, it is really cool to debug the Azure function using the Visual Studio Code 2019. Even it is more interesting to debug multiple function apps in Visual Studio 2019. Let’s check that scenario.

Debugging Multiple Azure function Apps In Visual Studio 2019

So, We have discussed above, Running Multiple Azure Functions Locally, and found it interesting. Now we will discuss Debugging Multiple Azure Function Apps in Visual Studio 2019 that will be more interesting.

The Visual Studio 2019 can’t launch multiple apps to debug at the same time, But as workaround what you can try out is, you can launch one then attach to the others. Let’s do that following the below steps

First, Put the break point in both the Functions like below

Here is my first function, where i have put the breakpoint inside the function

debug azure functions visual studio 2019

Below is my second function and I have put the breakpoint there.

how to debug azure function locally 1

Now, the next step is to start the first function app without debugging. Click on the Debug –> Start Without Debugging or press Ctrl + F5.

unable to debug azure function locally

Then, start the second function app with the debugger. Click on the Debug –> Start Debugging or press the F5 button.

debug azure functions locally visual studio 2019

Now you need to attach the debugger to the First Azure Function App. For that You need to click the Debug –> Attach to Process.

How do I debug Azure function locally?

Now on the Attach to Process window, search for the func.exe. You can see two entries. Select the one which is not greyed out and then click on the Attach button.

How do you debug a function app

Now if you will try accessing both the URLs i.e http://localhost:7071/api/HTTPAzureFunction and http://localhost:5860/api/Function1, you can see it will hit the respective breakpoints inside the Azure Functions.

You may also like following the below Articles

Conclusion

Well, In this article, we discussed Run Multiple Azure Functions Locally, Run Azure Functions Locally, the fix to the error Port 7071 is unavailable. Close the process using that port, or specify another port using –port [-p], Run Azure Functions Locally Visual Studio Code, Debug Azure Functions Visual Studio Code, and then we also discussed Debug Azure functions visual studio 2019 and Debugging Multiple Azure function Apps In Visual Studio 2019. I hope, you have enjoyed this article !!!.

Leave a Comment