Run Multiple Azure Functions Locally

In this Azure tutorial, we will discuss running multiple Azure Functions locally and how I fixed the error “port 7071 is unavailable. Close the process using that port, or specify another port using—-port [—p].” that I got while running multiple Azure Functions.

Run Multiple Azure Functions Locally

Well, we can create an Azure function using Visual Studio and run it easily. It will not create any issues 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 running multiple Azure Functions Locally, let’s discuss running 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);
        }
    }
port 7071 is unavailable. close the process using that port, or specify another port using --port [-p]

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

port 7071 is unavailable

What if I have one more Azure function I am trying to run simultaneously? That means the scenario runs multiple Azure Functions simultaneously 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 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");
        }
azure function port 7071 is unavailable

Now, I am trying to run my second Azure function, Function1. I want to inform you that my first function is already running on my local machine.

You can 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 an error like the one 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]

port 7071 is unavailable azure function

So, it is not allowing me to run my 2nd Azure function with the same port 7071. This is the default port when running an Azure Function on your local machine. So, to overcome this type of scenario or to fix the above issue, you 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

run azure function locally

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

host start –pause-on-error –port 5860

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

After adding the above argument, click the Save button on the top to save the changes.

how to run azure function locally

Now, if you 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 closely look at the above URL of the Azure function, you can see it is running on port 5860 instead of port 7071.

run azure functions locally

This is how you can Run Multiple Azure Functions Locally.

Run Azure Functions Locally Visual Studio Code

You are running your Azure Functions Locally using the Visual Studio IDE, the same way you can 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);
        }
    }
how to test azure functions locally

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

failed to run function app locally

Now you can see below that the Azure Function Project ran successfully without any issues and provided us with the below function URL

http://localhost:7071/api/MyNewTest

azure function run locally

This way, you can run Azure Function locally in Visual Studio Code.

You can also check out how to Debug Azure Functions using Visual Studio 2019 and Visual Studio Code

You may also like following the below Articles

Conclusion

Well, in this article, we discussed the steps to run Azure Function locally and the fix to the error Port 7071 is unavailable. Close the process using that port, or specify another port using—port [—p] that I encountered while running multiple Azure Functions locally. I hope you have enjoyed this article!!!

Azure Virtual Machine

DOWNLOAD FREE AZURE VIRTUAL MACHINE PDF

Download our free 25+ page Azure Virtual Machine guide and master cloud deployment today!