I would like to configure Aspire to have two different versions of the same C# Azure Durable Functions project with different settings.
builder .AddAzureFunctionsProject"db1") .WithHostStorage(storage) .WithEnvironment("MongoDbDatabaseName", "Db1"); builder .AddAzureFunctionsProject"db2") .WithHostStorage(storage) .WithEnvironment("MongoDbDatabaseName", "Db2");
However, this obviously means they share the same launch settings including ports and that causes a conflict.
Is there a way to configure Aspire to run the same Azure Functions code twice with different launch settings?
I have tried using .WithArgs(“—port xxxx”)
but that didn’t seem to work for me either.
Multiple Azure Functions in Aspire with same code but different settings
Yes, it's absolutely possible to run multiple instances of the same Azure Functions project in Aspire with different environment settings and ports. However, by default, Aspire uses the same launch configuration, which leads to port conflicts unless explicitly overridden.
To resolve this, you need to make sure each instance is configured to run on a different port, has its own distinct name in Aspire and is given specific environment settings.
Here’s what worked for me:
builder
.AddAzureFunctionsProject("db1", "../MyFunctionApp")
.WithHostStorage(storage)
.WithEnvironment("MongoDbDatabaseName", "Db1")
.WithArgs("--host", "localhost", "--port", "7071")
.WithSetting("ASPNETCORE_URLS", "http://localhost:7071");
builder
.AddAzureFunctionsProject("db2", "../MyFunctionApp")
.WithHostStorage(storage)
.WithEnvironment("MongoDbDatabaseName", "Db2")
.WithArgs("--host", "localhost", "--port", "7072")
.WithSetting("ASPNETCORE_URLS", "http://localhost:7072");
MyFunctionApp/Function1.cs
file
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
public class Function1
{
private readonly ILogger _logger;
private readonly string _dbName;
public Function1(ILoggerFactory loggerFactory, IConfiguration configuration)
{
_logger = loggerFactory.CreateLogger<Function1>();
_dbName = configuration["MongoDbDatabaseName"];
}
[Function("Function1")]
public void Run([TimerTrigger("*/15 * * *")] TimerInfo myTimer)
{
_logger.LogInformation($"[{_dbName}] Function1 triggered at: {DateTime.UtcNow}");
}
}
For more details, please refer the following documentation:Azure Functions with .Net Aspire