本地开发 azure functions 'HttpRequestMessage' 不包含 'GetQueryNameValuePairs' 的定义

local development azure functions 'HttpRequestMessage' does not contain a definition for 'GetQueryNameValuePairs'

我在本地 运行 在门户中运行 azure 函数时遇到问题。我在 Azure 中创建了一个默认的 C# Http 触发器,然后将应用程序内容下载到本地 运行。

[5/22/18 9:03:21 PM] run.csx(8,23): error CS1061: 'HttpRequestMessage' does not contain a definition for 'GetQueryNameValuePairs' and no extension method 'GetQueryNameValuePairs' accepting a first argument of type 'HttpRequestMessage' could be found (are you missing a using directive or an assembly reference?)
[5/22/18 9:03:21 PM] run.csx(20,15): error CS1501: No overload for method 'CreateResponse' takes 2 arguments

该功能在门户中完美运行。

正如@Mikhail 所说,这是由错误的函数核心工具(CLI) 版本引起的。您下载的代码适用于 v1 CLI(.Net Framework),而 v2(您可能已经安装)在 .NET Core 上。

要安装所需的 CLI,请按照 CLI installation. If local OS(like MacOS) doesn't support v1 CLI we may turn to v2 CLI which can run cross-platform. With v2 CLI, v1 code created on portal can't be used, hence we need to create functions locally 或将门户功能应用程序运行时更改为 ~2,然后重新创建和下载。

注意,在我们更改运行时之前,函数应用程序应该是空的,因为之前创建的函数依赖于不同的运行时,通常在更改后会失效。

为了给出一个有效代码的具体示例,以下代码适用于 Functions v2:

public static async Task<HttpResponseMessage> Run(
       [HttpTrigger(AuthorizationLevel.Function, "get", "post", 
        Route = null)] HttpRequest req, ILogger log)
{

       int id;
       bool parsedId = int.TryParse(req.Query["id"], out id);
       ...
}