【问题标题】:Azure function v2. Cannot reference dll from nugetAzure 函数 v2。无法从 nuget 引用 dll
【发布时间】:2018-12-31 20:19:21
【问题描述】:

我正在测试如何编写一个简单的 azure 函数来更新 azure sql 数据库。 从 VS2017 开始,我使用 netstandars 2.0 创建了一个新的 Cloud->Azure 函数 v2。我的代码如下:

[FunctionName("Activate")]
    public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
    {
        log.Info("C# HTTP trigger function processed a request.");

        string serialNumber = req.Query["SerialNumber"];

        try
        {
            var str = "CONNECTION_STRING_FOR AZURE SQL";
            using (SqlConnection conn = new SqlConnection(str))
            {
                conn.Open();
                var text = $"UPDATE CLIENT SET ACTIVATION_DATE = '2018-07-23 WHERE SERIAL_NUMBER='{serialNumber}'";

                using (SqlCommand cmd = new SqlCommand(text, conn))
                {
                    // Execute the command and log the # rows affected.
                    var rows = cmd.ExecuteNonQuery();
                    log.Info($"{rows} rows were updated");
                }
            }
        }
        catch (Exception ex)
        {
            return new BadRequestObjectResult(ex.Message);
        }

        return serialNumber != null
            ? (ActionResult)new OkObjectResult($"Hello, {serialNumber}")
            : new BadRequestObjectResult("error");
    }

它编译并可以从Visual Studio本地运行:

Hosting environment: Production
Content root path: C:\Projects\xxx\bin\Debug\netstandard2.0
Now listening on: http://localhost:7071
Application started. Press Ctrl+C to shut down.
[24/7/2018 8:50:48 πμ] Reading host configuration file 'C:\Projects\xxx\bin\Debug\netstandard2.0\host.json'
[24/7/2018 8:50:48 πμ] Host configuration file read:
[24/7/2018 8:50:48 πμ] {}
[24/7/2018 8:50:48 πμ] Starting Host (HostId=xxx-1133968365, InstanceId=58079dab-xxx-4bb9-aaf5-f24xxx63d3c, Version=2.0.11651.0, ProcessId=11304, AppDomainId=1, Debug=False, ConsecutiveErrors=0, StartupCount=1, FunctionsExtensionVersion=)
[24/7/2018 8:50:48 πμ] Unable to configure java worker. Could not find JAVA_HOME app setting.
[24/7/2018 8:50:48 πμ]
[24/7/2018 8:50:48 πμ] Could not configure language worker Java.
[24/7/2018 8:50:48 πμ]
[24/7/2018 8:50:49 πμ] Generating 1 job function(s)
[24/7/2018 8:50:49 πμ] Found the following functions:
[24/7/2018 8:50:49 πμ] ClientFunctions.Run
[24/7/2018 8:50:49 πμ]
[24/7/2018 8:50:49 πμ] Host initialized (1511ms)
[24/7/2018 8:50:49 πμ] Host started (1616ms)
[24/7/2018 8:50:49 πμ] Job host started
Listening on http://localhost:7071/
Hit CTRL-C to exit...

Http Functions:

        Activate: http://localhost:7071/api/Activate

但如果我通过邮递员访问它,我会得到:

[24/7/2018 8:52:58 πμ] Executing 'Activate' (Reason='This function was programmatically called via the host APIs.', Id=30cff68d-b8db-446e-bd6d-407990524198)
[24/7/2018 8:52:58 πμ] Executed 'Activate' (Failed, Id=30cff68d-b8db-446e-bd6d-407990524198)
[24/7/2018 8:52:58 πμ] System.Private.CoreLib: Exception while executing function: Activate. Mik3UpdateFunctionNew: Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib: Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

奇怪的是,每次启动VS2017调试程序,都会出现一个窗口,提示下载azure functions cli tools 2.3.2。

有人可以提出任何建议吗? 谢谢

【问题讨论】:

    标签: azure azure-functions


    【解决方案1】:

    @Josh 是对的。您看到的下载提示意味着 VS 尝试下载最新的 functin CLI。但实际上下载失败并回退到使用旧版本,正如您在 CLI 输出中看到的那样

    Starting Host (...,Version=2.0.11651.0,...)
    

    表示函数宿主版本,CLI 2.0.1-beta.25使用。而目前最新的主机是2.0.11933.0,CLI 2.0.1-beta.33,即2.3.2由VS指定。

    以下是有关如何使用最新 CLI 的一些详细信息。

    1. 如果您已有工具文件夹%LocalAPPDATA%\AzureFunctionsTools,请将其删除以避免损坏新工具。

    2. 在 VS 菜单,Tools->Extensions and Updates,找到Azure Functions and Web Jobs Tools,确保它已更新到最新版本(现在15.0.40617.0)。

    3. 如果是最新的,只需重新启动 VS 并创建一个新的 Azure Function 项目,在创建对话框中等待 VS 下载新的 CLI 和模板。

    一段时间后,我们可以看到提示变为

    然后您可以在本地调试时看到Starting Host (...,Version=2.0.11933.0,...),并且您的代码应该可以工作。

    如果还是看到2.0.11651.0,去检查一下这个文件夹%LocalAPPDATA%\AzureFunctionsTools\Releases\2.3.2是否存在,里面包含cli、templates和manifest.json。如果下载失败,只需删除%LocalAPPDATA%\AzureFunctionsTools文件夹并重新启动VS重新下载。

    更新--网络不好如何手动下载工具。

    打开%LocalAPPDATA%\AzureFunctionsTools\feed.json,找到“2.3.2”:{...} 内容。下载它的cli、itemTemplates和projectTemplates。(下载后需要手动重命名)。

    %LocalAPPDATA%\AzureFunctionsTools\Releases\2.3.2中的文件夹结构是这样的(与2.0.1-beta.25文件夹相同)

    cli
    --...
    templates
    --ItemTemplates.nupkg
    --ProjectTemplates.nupkg
    manifest.json
    

    manifest.json 的内容

    {
      "ReleaseName": "2.3.2",
      "CliEntrypointPath":"C:\\Users\\UserName\\AppData\\Local\\AzureFunctionsTools\\Releases\\2.3.2\\cli\\func.exe",
      "TemplatesDirectory": "C:\\Users\\UserName\\AppData\\Local\\AzureFunctionsTools\\Releases\\2.3.2\\templates",
      "FunctionsExtensionVersion": "beta",
      "SdkPackageVersion": "1.0.14"
    }
    

    【讨论】:

    • 这条路径是否正确 %LocalAPPDATA%\AzureFunctionsTools?我没有 AzureFunctionsTools 文件夹。问题是每次都会出现下载Azure函数CLI工具对话框。所以我的猜测是它不能被下载。
    • ok 抱歉,再次调试应用后,该文件夹出现在那里。但是在名为:2.2.2 2.3.1 2.3.2 的文件夹内是空的,而其他像 1.3.0 有一些文件夹。
    • @gong 尝试重启VS并新建一个函数项目,在创建对话框中等待,直到看到Updates are ready
    • 我做到了。但它并没有说更新准备好了,只是:开始使用 azure 函数
    • @gong 再次检查 tools 文件夹,如果 2.3.2 文件夹中充满了提到的内容,它应该可以工作。或者您可以尝试删除此工具文件夹并重复该过程再次下载。
    【解决方案2】:

    您看到的提示是 Visual Studio 的 Azure Functions 扩展将您的本地 Azure Functions CLI 更新到最新版本。 Azure Functions 2.0 预览版最近有很多更新,尤其是解决程序集的方式(属于您的错误类别)。我鼓励您允许更新完成。随着最近 2.0 发生的所有更改,很容易在本地开发人员和部署到 azure 的内容之间获得一些不匹配的版本和引用,从而导致各种问题。

    如果错误仍然存​​在,我建议在 Github 上打开一个带有错误详细信息的问题,因为他们正在寻找有关人们仍然遇到的任何程序集绑定错误的反馈。

    【讨论】:

    • FWIW 我使用最新版本的 Azure Functions 运行时、更新的 VS2017 等在实验室环境中重现了您的确切解决方案,并且能够在 Azure 中运行该函数而没有任何错误。所以乔希的建议很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多