Azure HTTP 函数触发器 2.x,Cosmos DB 输出本地开发主机失败

Azure HTTP function trigger 2.x with Cosmos DB Output local dev host failing

我正在使用 Azure 函数 2.x 和 dotnet 核心开发一个 http 触发器,在最新更新到 VS 2017 15.8.2 之后,当 运行 本地函数

1/9/2018 13:30:50] Stopping Host
[1/9/2018 13:31:06] Reading host configuration file 'C:\Users\MattDouhan\source\repos\NWMposTransInput\NWMposTransInput\bin\Debug\netstandard2.0\host.json'
[1/9/2018 13:31:06] Host configuration file read:
[1/9/2018 13:31:06] {}
[1/9/2018 13:31:06] Starting Host (HostId=desktop7cks1do-260439321, InstanceId=5fd41a43-b3ca-47e4-adf6-320d40fa9613, Version=2.0.11960.0, ProcessId=13156, AppDomainId=1, Debug=False, ConsecutiveErrors=5, StartupCount=6, FunctionsExtensionVersion=)
[1/9/2018 13:31:07] A ScriptHost error has occurred
[1/9/2018 13:31:07] System.Private.CoreLib: Could not load type 'Microsoft.Azure.WebJobs.Hosting.IWebJobsStartup' from assembly 'Microsoft.Azure.WebJobs.Host, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null'.
[1/9/2018 13:31:07] Stopping Host

我是运行函数核心工具2.0.1-Beta.35和运行时版本2.0.11960.0

函数如下所示

using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json;
using Microsoft.Extensions.Logging;
using System;

namespace NWMposTransInput
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
        HttpRequest req, ILogger log,
            [CosmosDB(
            databaseName: "xxxx",
            collectionName: "yyyy",
            ConnectionStringSetting = "CosmosDbConnection")]out dynamic document,
            ILogger log2)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = new StreamReader(req.Body).ReadToEnd();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            document = new { SSourceSystem = "jongelSystem",
                             id = Guid.NewGuid(),
                             SSourceSystemVersion = "1.1",
                             STransactionId = "12345"};

            log.LogInformation($"C# Queue trigger function inserted one row");
            log.LogInformation($"Description={req.Body}");

            return name != null
                ? (ActionResult)new OkObjectResult($"Hello, {name}")
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
        }
    }

    public class NWCloudOrder
    {
        public string Id { get; set; }
        public string SSourceSystem { get; set; }
        public string SSourceSystemVersion { get; set; }
        public string STransactionId { get; set; }
    }
}

--- 编辑 --- 确保我使用正确的运行时,这现在会产生以下错误

[2/9/2018 05:44:15] A ScriptHost error has occurred
[2/9/2018 05:44:15] System.Private.CoreLib: Could not load file or assembly 'Microsoft.AspNetCore.Mvc.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib: Could not load file or assembly 'Microsoft.AspNetCore.Mvc.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
[2/9/2018 05:44:15] Stopping Host

我确实安装了 2.2.0-preview1-35029

这是由于在旧函数运行时 2.0.11960.0 中引用了最新版本 3.0.1-beta1/2Microsoft.Azure.WebJobs.Extensions.CosmosDB 引起的。 Microsoft.NET.Sdk.Functions 1.0.19Microsoft.Azure.WebJobs.Extensions.CosmosDB 3.0.1-beta2 取决于运行时 2.0.12050.0.

检查此 issue 以及有关最新运行时中断更改的相关错误信息 2.0.12050.0

如果您暂时不想移动到最新的运行时,请将 Microsoft.Azure.WebJobs.Extensions.CosmosDB 降级到 3.0.0-beta7 并将 Microsoft.NET.Sdk.Functions 降级到 1.0.14

否则只需确保 VS 使用最新的函数运行时 2.0.12050.0

  1. 在 VS 菜单 -> 扩展和更新上。查找 Azure Functions 和 Web 作业工具。确保它 >= 15.8.5023。(需要先将 VS 更新到 15.8)。

  2. 删除旧的 Function Core tools(Cli) 文件夹 %localappdata%\AzureFunctionsTools

  3. 重新启动 VS 并创建一个新的 Azure 函数。等待 VS 的创建对话框下载新的 Cli 和模板,直到我们看到提示更改为更新已准备就绪。

更新

  1. 检查 CLI 输出,恐怕旧版本 2.0.11651.0 正在使用中。

    Starting Host (HostId=xxx, InstanceId=xxx, Version=2.0.11651.0, ...)
    

    转到 %localappdata%\AzureFunctionsTools\Releases,您可能还会看到一个空文件夹 2.5.1

    由于网络等原因下载失败。再次重复上述步骤。如果不起作用,请尝试手动下载 Cli。先关闭VS。

    1).打开 %localappdata%\AzureFunctionsTools\feed.json 找到 v2.

    的最新下载 url

    例如cli: https://functionscdn.azureedge.net/public/2.0.1-beta.36/Azure.Functions.Cli.win-x86.2.0.1-beta.36.zip.

    2).将解压后的文件夹重命名为cli,放在2.5.1.

    3).将 cli 文件夹中的 templates 文件夹移动到 2.5.1

    4).在 2.5.1 下创建一个 manifest.json。(更改您的用户名)

    {
     "ReleaseName": "2.5.1",
     "CliEntrypointPath": "C:\Users\UserName\AppData\Local\AzureFunctionsTools\Releases\2.5.1\cli\func.exe",
     "TemplatesDirectory": "C:\Users\UserName\AppData\Local\AzureFunctionsTools\Releases\2.5.1\templates",
     "FunctionsExtensionVersion": "~2",
     "SdkPackageVersion": "1.0.19"
    }
    
  2. 函数 SDK 在设计上引用了 Microsoft.AspNetCore.Mvc.Abstractions 2.1.0。删除 2.2.0-preview1-35029 否则我们可能会在执行时出错。

    Microsoft.Azure.WebJobs.Host: Exception binding parameter 'req'. System.Private.CoreLib: Cannot create an abstract class.