🔖

.NET Aspire と Azure Functions 開発時にポート番号が合わない問題への対処方法

に公開

問題の概要

2025/08/11 時点でプレビュー機能の .NET Aspire の Azure Functions サポートですが、実行すると .NET Aspire Dashboard 上のポート番号と実際のポート番号が異なることがよくあります。例えば以下の画面だと Azure Functions のポート番号は 7283 番になっています。

しかし、実際にコンソールログを確認すると以下のように 7071 番ポートで実行されています。

そのため .NET Aspire Dashboard の URL をクリックしても何も表示されませんし、Azure Functions のプロジェクトを WithReference メソッドで別プロジェクトに追加しても正しいエンドポイントが設定されません。

解決策

この問題は、.NET Aspire の Azure Functions 拡張が Azure Functions プロジェクトの launchSettings.json を読み込んでポート番号を取得しているが、実際に起動されるときのプロファイル名は異なっているために発生します。Azure Functions のプロジェクトの launchSettings.json を開くと以下のようになっています。

Azure Functions プロジェクトの launchSettings.json
{
  "profiles": {
    "DeepResearch.DurableFunctions": {
      "commandName": "Project",
      "commandLineArgs": "--port 7283"
    },
    "Container (Dockerfile)": {
      "commandName": "Docker",
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
      "containerRunArguments": "--init",
      "httpPort": 33952,
      "useSSL": false
    }
  }
}

ここに .NET Aspire Dashboard に表示されているポート番号が設定されています。しかし、実際に .NET Aspire の AppHost プロジェクトを起動するときのプロファイル名は https になります。.NET Aspire の AppHost プロジェクトの launchSettings.json を開くと以下のようになっています。

AppHost プロジェクトの launchSettings.json
{
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "profiles": {
    "https": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:17145;http://localhost:15212",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "DOTNET_ENVIRONMENT": "Development",
        "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21100",
        "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22258"
      }
    },
    "http": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "http://localhost:15212",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "DOTNET_ENVIRONMENT": "Development",
        "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19195",
        "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20013"
      }
    }
  }
}

この状態で何も考えずに実行すると https プロファイルが選択されて起動されますが、Azure Functions のプロジェクトには https プロファイルが存在しないためデフォルトのポート番号の 7071 番で起動されてしまいます。解決するには以下のように Azure Functions の launchSettings.json ファイルのプロファイル名を https に変更するのが一番簡単です。

Azure Functions プロジェクトの launchSettings.json
{
  "profiles": {
    "https": { // ここを変更
      "commandName": "Project",
      "commandLineArgs": "--port 7283"
    },
    "Container (Dockerfile)": {
      "commandName": "Docker",
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
      "containerRunArguments": "--init",
      "httpPort": 33952,
      "useSSL": false
    }
  }
}

これで .NET Aspire Dashboard 上のポート番号と実際のポート番号が一致するようになります。実際に実行してみて Azure Functions のプロジェクトの標準出力のログを確認すると、以下のようにポート番号が一致していることが確認できます。

わからないとハマるのでメモでした。

Microsoft (有志)

Discussion