如何 运行 Visual Studio 中不同端口上的 Azure Function 应用程序

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

我正在 local.setting.json 中设置本地主机端口。引用 Microsoft 文档 https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local

文件如下所示

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "AzureWebJobsDashboard": ""   
  },
  "Host": {
    "LocalHttpPort": 7073
  }
}

当我 run/debug 解决方案时,VS 仍然在默认端口 (7071) 上托管应用程序

我已经检查了 bin 目录,local.setting.json 文件包含上述设置。 运行 Azure Fucntion CLI (func host start) 从 bin 目录正确读取端口号。

看起来 VS 没有使用 "LocalHttpPort" 端口。设置中是否需要任何其他更改。我有 Visual Studio 2017 预览版 (2)

更新:如果您只是想更改端口,则不必通过问题中指定的文件进行设置。检查

原回答:

命令行优先于设置文件,问题是VS在命令行上传递了一个显式端口。

解决方法是通过 project -> properties -> Debug,然后在 Application arguments 下控制参数。你可以输入 host start --pause-on-error

编辑自 ravinsp:

.Net Core 2.0 函数项目更新:

您必须传递的命令行参数不同。你必须在前面传递一个dll的路径。像这样: %AppData%\..\Local\Azure.Functions.V2.Cli.0.1-beta.22\Azure.Functions.Cli.dll host start --pause-on-error 您可以通过 运行 Visual Studio 中的函数亲自查看,并使用进程浏览器查看 dotnet.exe 进程的命令行参数。

编辑:一句话

Correct answer for .NET Core 2.0 / .NET Standard 2.0 Azure Functions project in Visual Studio 2017 when you have installed Azure Functions Core Tools 2.x Runtime via NPM

我遵循了@ahmelsayed 的回答,特别是@ravinsp 对.net core 2.0 的评论。虽然非常有帮助并让我走上正轨,但如果没有关键的 non-intuitive 修改,它们对我不起作用,所以我添加了一个新的答案。

TL;DR;

如果您使用 NPM 安装 Azure Functions Core Tools 2.x 运行时,那么您可能需要将 Project/Properties/Debug/Application 参数设置为:

C:\Users\<myuserid>\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\func.dll host start --port 8888 --pause-on-error

长答案如下...

我的设置

在这个练习中,我的设置完全是最新的(在撰写本文时)如下:

  • Visual Studio 2017专业版:15.6.2
  • Azure 函数和 Web 作业工具:15.0.40215.0
  • Windows 10 10.0.16299 内部版本 16299
  • Azure Functions Core Tools(根据 Microsoft 的 Develop and run Azure functions locally 文档安装)报告(在 Git Bash 中):

    me@MYDESKTOP ~ $ func <snip/> Azure Functions Core Tools (2.0.1-beta.24) Function Runtime Version: 2.0.11587.0

首先,Azure 上此 Functions 应用程序的 Functions App 设置选项卡读取:

Runtime version: 2.0.11587.0 (beta)

我的问题

当我 运行 我的函数项目(没有应用程序参数)时,我在控制台输出中得到这个:

[17/03/2018 21:06:38] Starting Host (HostId=MYMACHINE, Version=2.0.11353.0, ProcessId=<snip/>
Listening on http://localhost:7071/

这本身可能不是问题,但我遇到了烦人的 "works on my machine, fails when publishing to azure" 类型问题,所以我想确保本地执行使用与 运行time 相同的函数天蓝色(即 2.0.11587.0,根据上面的设置说明,is/should 是,对吗?)

所以,根据@ravinsp 的说明,我 运行 在我的 C 盘上找到了 Azure.Functions.Cli.dll - 只有一个,它位于 C:\Users\<myuserid>\AppData\Local\Azure.Functions.V2.Cli.0.1-beta\Azure.Functions.Cli.dll 这看起来非常一致与@ravinsp 的回答。

因此,我添加以下 Project/Properties/Debug/Application 个参数:

C:\Users\<myuserid>\AppData\Local\Azure.Functions.V2.Cli.0.1-beta\Azure.Functions.Cli.dll host start --port 8888 --pause-on-error

然后我确实得到了端口 8888,但是 运行奇怪的是 仍然 被报告为 2.0.11353。

[17/03/2018 21:13:02] Starting Host (HostId=MYMACHINE, Version=2.0.11353.0, ProcessId=<snip/>
Listening on http://localhost:8888/

解决方案

考虑到 运行来自 Git Bash 的 运行ning func 按照上面显示 运行time of 2.0.11587.0,我尝试 which func 返回/c/Users/<myuserid>/AppData/Roaming/npm/func 。我在上面做了一只猫,长话短说,我可以看到最终它是 运行ning C:\Users\<myuserid>\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\func.exe,并且在同一个目录中有一个 func.dll.

所以,我尝试了以下 Project/Properties/Debug/Application 参数:

C:\Users\<myuserid>\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\func.dll host start --port 8888 --pause-on-error

然后我终于明白了...

[17/03/2018 21:16:29] Starting Host (HostId=MYMACHINE, Version=2.0.11587.0, ProcessId=<snip/>
Listening on http://localhost:8888/

而且,至关重要的是,我在发布到 Azure 时遇到的错误也开始在本地出现。

好的,现在 运行 时间全部同步,是时候开始修复我的实际错误了:)

我使用了接受的答案,但当调试器端口尝试绑定时我仍然遇到错误,因为两个函数应用程序都试图绑定到 5858。

为了解决这个问题,我在项目设置中向应用程序参数添加了一个属性,我的参数如下所示:

host start --pause-on-error --nodeDebugPort 5860

我使用的是 CLI 版本 1.2.1,Project Properties -> Debug 中的以下 Application arguments 设置对我有用。

host start --port 7074 --nodeDebugPort 5860

更新项目属性 -> 调试如下

host start --port 7073 --pause-on-error

如果您使用 Visual Studio for MacOS,请右键单击您的项目,单击“选项”,单击“Run -> Configurations -> Default”并在“参数”字段中输入 host start --port 7073 --pause-on-error

这样做

Select Function App Project in Visual Studio -> Hit Alt+Enter 并导航到调试设置并设置

host start --port 8085 --nodeDebugPort 6890

从这个版本开始:

Azure Functions Core Tools (3.0.2912 Commit hash: bfcbbe48ed6fdacdf9b309261ecc8093df3b83f2)
Function Runtime Version: 3.0.14287.0

您只需在“应用程序参数”框中键入 start --port 7074

这是我在 local.settings.json 文件中使用不同端口的方式,而 运行 这是在 Intellij 中使用的方式。您也可以使用任何其他 IDE,local.settings.json 无处不在。

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "java"
  },
  "Host": {
    "LocalHttpPort": 7072
  },
  "ConnectionStrings": {}
}

在 VS 2022 和 .Net 6 的“命令行参数”中使用以下内容: 开始——端口 7074

使用 Visual Studio 2022 和函数 v4,您可以在 launchSettings.json 文件中设置端口:

{
  "profiles": {
    "<functionapp project name>": {
      "commandName": "Project",
      "commandLineArgs": "--port 7137",
      "launchBrowser": false
    }
  }
}

此设置也可以通过 UI 更新:
Properties > Debug > General > Open debug launch profiles UI: