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 how did I fix 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 We can 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);
        }
    }
Run Multiple Azure Functions Locally

Now, I am trying to run it, and it ran successfully without any issues and provided us 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.

How to run multiple Azure Functions locally

Now, what if I have one more Azure function I am trying to run simultaneously? That means the scenario is running 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");
        }
how to run two azure function locally

Now, I am trying to run my second Azure function, which is Function1. I want to inform you guys that my first function is already running on 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 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. close the process using that port, or specify another port using –port [-p].

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 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

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 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 adding the above argument, click the Save button on the top to save the changes.

port 7071 is unavailable. close the process using that port

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.

port 7071 is unavailable

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);
        }
    }
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.

azure function port 7071 is unavailable

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

Run Azure Functions Locally using Visual Studio Code

This way, you can run Azure Functions 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 Run Multiple Azure Functions 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 !!!.