Azure函数v2.无法从nuget引用dll [英] Azure function v2. Cannot reference dll from nuget

查看:54
本文介绍了Azure函数v2.无法从nuget引用dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am testing how to write a simple azure function that updates an azure sql database. From VS2017 I made a new Cloud->Azure function v2 with netstandars 2.0. My code is the following:

[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中本地运行:

It compiles and can be run locally from 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

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

But if I access it through postman I get:

[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函数cli工具2.3.2.

What I find strange is that each time I start VS2017 and debug the program, a window appears that says downloading azure functions cli tools 2.3.2.

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

Can someone suggest anything? Thanks

推荐答案

@Josh是正确的.您看到的下载提示意味着VS尝试下载最新的functin CLI.但是实际上下载失败,并且退回使用旧的下载,如您在CLI输出中所见

@Josh is right. The downloading tip you saw means VS tried to download the latest functin CLI. But actually the downloading failed and it fell back to use the old one, as you can see in your CLI output

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

它表示功能主机版本,由CLI 2.0.1-beta.25使用.目前,最新主机是带有CLI 2.0.1-beta.332.0.11933.0,即VS指定的2.3.2.

It represents the function host version, which is used by CLI 2.0.1-beta.25. While for now the latest host is 2.0.11933.0 with CLI 2.0.1-beta.33, i.e.2.3.2 specified by VS.

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

Here are some details about how to consume the latest CLI.

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

  1. If you already has the tools folder %LocalAPPDATA%\AzureFunctionsTools, delete it to avoid corrupting new tools.

在VS菜单上的工具"->扩展和更新"上,找到Azure Functions and Web Jobs Tools,确保将其更新为最新版本(现在为15.0.40617.0).

On VS menus, Tools->Extensions and Updates, find Azure Functions and Web Jobs Tools, make sure it's updated to latest version(right now 15.0.40617.0).

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

If it has been the latest, just restart VS and create a new Azure Function project, wait at the create dialog for VS to download new CLI and templates.

过一会儿,我们可以看到小费更改为

After a while, we can see the tip change to

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

Then you can see Starting Host (...,Version=2.0.11933.0,...) while debugging locally and your code should work.

如果仍然看到2.0.11651.0,请检查此文件夹%LocalAPPDATA%\AzureFunctionsTools\Releases\2.3.2是否存在,其中包含cli,templates和manifest.json.如果下载失败,则只需删除%LocalAPPDATA%\AzureFunctionsTools文件夹,然后重新启动VS即可再次下载.

If you still see 2.0.11651.0, go check whether this folder%LocalAPPDATA%\AzureFunctionsTools\Releases\2.3.2exists, which contains cli, templates and manifest.json. If the downloading turns out failed, just delete %LocalAPPDATA%\AzureFunctionsTools folder and restart VS to download again.

更新-由于网络状况不佳,如何手动下载工具.

Update--how to download tools manually due to poor network.

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

Open %LocalAPPDATA%\AzureFunctionsTools\feed.json, find "2.3.2":{...} content. Download its cli, itemTemplates and projectTemplates.(Need to rename manually after download).

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

And folder structure in %LocalAPPDATA%\AzureFunctionsTools\Releases\2.3.2 is like this(same to 2.0.1-beta.25 folder)

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

manifest.json的内容

Content of 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"
}

这篇关于Azure函数v2.无法从nuget引用dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆