简体   繁体   English

如何在 Azure Functions 中使用 NuGet 包?

[英]How can I use NuGet packages in my Azure Functions?

使用Azure Functions ,我可以在我的 C# 函数中引用和使用 NuGet 包吗?

Yes!是的! Although the Azure Functions portal does not currently provide a mechanism to add and manage NuGet packages, the runtime supports NuGet references and will make sure they are correctly used when compiling and executing your functions.尽管 Azure Functions 门户目前不提供添加和管理 NuGet 包的机制,但运行时支持 NuGet 引用,并将确保在编译和执行函数时正确使用它们。

In order to define your dependencies, you need to create a Project.json file with the required NuGet package references.为了定义您的依赖项,您需要使用所需的 NuGet 包引用创建一个Project.json文件。 Here is an example that adds a reference to Microsoft.ProjectOxford.Face version 1.1.0:这是一个添加对Microsoft.ProjectOxford.Face版本 1.1.0 的引用的示例:

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "Microsoft.ProjectOxford.Face": "1.1.0"
      }
    }
   }
}

The Azure Functions portal provides a convenient way to manage your function files, which we can use to create (or upload) our project.json : Azure Functions 门户提供了一种管理函数文件的便捷方法,我们可以使用它来创建(或上传)我们的project.json

  1. In the function's develop section, click on view files在函数的开发部分,点击查看文件
  2. Click on the option to create a file (you can also click on the option to upload a file if you have a previously created project.json file on your machine点击创建文件的选项(如果你的机器上有之前创建的project.json文件,你也可以点击上传文件的选项
  3. Name the file project.json and define your package references (you can use the example above as a template).将文件命名为project.json并定义您的包引用(您可以使用上面的示例作为模板)。

The package restore process will begin and you should see output similar to the following in your log window:包还原过程将开始,您应该会在日志窗口中看到类似于以下内容的输出:

2016-04-04T19:02:48.745 Restoring packages.
2016-04-04T19:02:48.745 Starting NuGet restore
2016-04-04T19:02:50.183 MSBuild auto-detection: using msbuild version '14.0' from 'D:\Program Files (x86)\MSBuild\14.0\bin'.
2016-04-04T19:02:50.261 Feeds used:
2016-04-04T19:02:50.261 C:\DWASFiles\Sites\facavalfunctest\LocalAppData\NuGet\Cache
2016-04-04T19:02:50.261 https://api.nuget.org/v3/index.json
2016-04-04T19:02:50.261 
2016-04-04T19:02:50.511 Restoring packages for D:\home\site\wwwroot\HttpTriggerCSharp1\Project.json...
2016-04-04T19:02:52.800 Installing Newtonsoft.Json 6.0.8.
2016-04-04T19:02:52.800 Installing Microsoft.ProjectOxford.Face 1.1.0.
2016-04-04T19:02:57.095 All packages are compatible with .NETFramework,Version=v4.6.
2016-04-04T19:02:57.189 
2016-04-04T19:02:57.189 
2016-04-04T19:02:57.455 Packages restored.

As expected, the Azure Functions runtime will automatically add the references to the package assemblies, so you DO NOT need to explicitly add assembly references using #r "AssemblyName" , you can just add the required using statements to your function and use the types defined in the NuGet package you've referenced.正如预期的那样,Azure Functions 运行时将自动添加对包程序集的引用,因此您无需使用#r "AssemblyName"显式添加程序集引用,您只需将所需的using语句添加到您的函数并使用定义的类型在您引用的 NuGet 包中。

Additional deployment options其他部署选项

Since Azure Functions is built on top of App Services, as an alternative to the steps above, you also have access to all the great deployment options available to standard Azure Web Apps (Azure Websites).由于 Azure Functions 构建在应用服务之上,作为上述步骤的替代方法,你还可以访问标准 Azure Web 应用(Azure 网站)可用的所有出色部署选项。

Here are some examples:以下是一些示例:

Using App Service Editor (Monaco)使用应用服务编辑器(摩纳哥)

In order to manage your files directly from your browser by using the App Service Editor (Monaco):要使用应用服务编辑器(摩纳哥)直接从浏览器管理文件:

  • On the Azure Functions portal, click on Function app settings在 Azure Functions 门户上,单击Function app settings
  • Under the Advanced Settings section, click on Go to App Service Settings在“高级设置”部分下,单击“ Go to App Service Settings
  • Click on the Tools button单击Tools按钮
  • Under Develop , click on App Service EditorDevelop 下,单击App Service Editor
  • Turn it On if it is not already enabled and click on Go打开它On如果尚未启用它,然后单击Go
  • Once it loads, drag-and-drop your project.json file into your function's folder (the folder named after your function.加载后,将project.json文件拖放到函数的文件夹(以函数命名的文件夹)。

Using SCM (Kudu) endpoint使用 SCM (Kudu) 端点

  • Navigate to: https://<function_app_name>.scm.azurewebsites.net导航到: https://<function_app_name>.scm.azurewebsites.net
  • Click on Debug Console > CMD单击调试控制台> CMD
  • Navigate to D:\\home\\site\\wwwroot\\<function_name>导航到D:\\home\\site\\wwwroot\\<function_name>
  • Drag-and-drop your Project.json file into the folder (onto the file grid)将您的Project.json文件拖放到文件夹中(到文件网格上)

FTP FTP

Continuous Integration持续集成

If you enable continuous integration and deploy your function with a project.json file when your Function App is not running, the package restore will happen automatically once your Function App initializes.如果在函数应用未运行时启用持续集成并使用project.json文件部署函数,则函数应用初始化后将自动进行包还原。 It is recommended that you do not add your project.lock.json file to source control.建议您不要project.lock.json文件添加到源代码管理中。

Pre-compiled assemblies预编译程序集

Functions may also be deployed as pre-compiled assemblies, and in this case, all dependency management is handled in Visual Studio.函数也可以部署为预编译的程序集,在这种情况下,所有依赖项管理都在 Visual Studio 中处理。 This option may be used as standard class libraries on any version of Visual Studio or by using the Visual Studio 2017 Azure Functions Tools .此选项可用作任何版本的 Visual Studio 上的标准类库或通过使用Visual Studio 2017 Azure Functions 工具

This thread helped me a lot - but I still wasted a few hours trying to get the Project.json to work - to no avail.这个线程对我帮助很大 - 但我仍然浪费了几个小时试图让 Project.json 工作 - 无济于事。

If you make an Azure function in version 2.x you need to do this in a different way.如果在版本 2.x 中创建 Azure 函数,则需要以不同的方式执行此操作。

Create a new file as stated but name it function.proj .如上所述创建一个新文件,但将其命名为function.proj This file has an XML structure for importing libraries via Nuget.此文件具有用于通过 Nuget 导入库的 XML 结构。

Here is my example importing the Amazon S3 SDK for .Net;这是我为 .Net 导入 Amazon S3 SDK 的示例;

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="AWSSDK.S3" Version="3.3.26.3"/>
    <PackageReference Include="AWSSDK.Core" Version="3.3.29.1"/>
  </ItemGroup>

</Project>

upon saving this you should see the console update stating that the packages are getting installed.保存后,您应该会看到控制台更新,说明正在安装软件包。 This really isn't well documented and it took me a good few hours to find this out.这确实没有很好的记录,我花了好几个小时才发现这一点。 So I hope this helps someone.所以我希望这对某人有所帮助。

You can use Nuget packages in your Azure Functions.可以在 Azure Functions 中使用 Nuget 包。 Easiest way will be to use Visual Studio 2017 15.4 where there is a template for Azure Functions.最简单的方法是使用 Visual Studio 2017 15.4,其中有一个 Azure Functions 模板。 Follow below steps按照以下步骤操作

1) Add Azure function Project : Right click on solution and select Add New project. 1) 添加 Azure 函数项目:右键单击解决方案并选择添加新项目。 Go to CLOUD option there you will find "Azure Function" project.转到 CLOUD 选项,您会在那里找到“Azure Function”项目。

天蓝色函数

2) Now it's pretty to add any Nuget package. 2)现在可以添加任何 Nuget 包了。 Expand "DEPENDENCIES" and right click on it to select option "Manage Nuget Packages".展开“DEPENDENCIES”并右键单击它以选择“Manage Nuget Packages”选项。 Nuget Package dialog will appear, select any Nuget package you want to install.将出现 Nuget 包对话框,选择要安装的任何 Nuget 包。 See screenshot below看下面的截图

3) Now publish your Azure function, Visual Studio will take care of all settings, etc. 3) 现在发布您的 Azure 函数,Visual Studio 将处理所有设置等。

This method will work only if you use Visual Studio 2017 15.4 or above, if not you will have to follow other ways as explained by others.此方法仅在您使用 Visual Studio 2017 15.4 或更高版本时有效,否则您将必须遵循其他人解释的其他方式。

Let's assume we want to use the SFTP client, which is an external library, stored somewhere in NuGet .假设我们要使用SFTP客户端,它是一个外部库,存储在NuGet某处。

To accomplish that, according to the latest Azure Functions specification, do the following:为此,根据最新的Azure Functions规范,请执行以下操作:

  1. Open files section inside your Azure Function and add a new file called function.proj .打开Azure Function 中的files 部分并添加一个名为function.proj的新文件。

    在此处输入图片说明 For more information refer to Microsoft documentation .有关详细信息,请参阅 Microsoft 文档


  1. Inside that function.proj put the Nuget package reference using the XML structure (the same kind of structure you can find inside *.csproj file if you create a local project with Visual Studio and install some NuGet package into it).在该function.proj ,使用XML结构放置Nuget包引用(如果您使用Visual Studio创建本地项目并将一些NuGet包安装到其中,则可以在*.csproj文件中找到相同类型的结构)。

    在此处输入图片说明


  1. Next, let's include the library reference to the project:接下来,让我们包含对项目的库引用: 在此处输入图片说明

    What is essential here is that you should provide a full path to the library, like in the example you can see it is: "D:\\home\\site\\wwwroot\\bin\\your_custom_library.dll"这里最重要的是你应该提供库的完整路径,就像在示例中你可以看到它是: "D:\\home\\site\\wwwroot\\bin\\your_custom_library.dll"


  1. Next test some library-specific code:接下来测试一些特定于库的代码: 在此处输入图片说明

  1. Finally, open the Logs section and save the code.最后,打开日志部分并保存代码。 After some time you should see the NuGet packages restoring log.一段时间后,您应该会看到NuGet包恢复日志。 在此处输入图片说明

    Note: these logs could also appear when saving function.proj file or when running the project.注意:这些日志也可能在保存function.proj文件或运行项目时出现。


In case if the library still appears as unknown try to add it's Dll 's manually into the bin folder using Azure Cloud explorer from the Visual Studio .如果库仍然显示为未知,请尝试使用Visual Studio 中的Azure 云资源管理器将其Dll手动添加到 bin 文件夹中。

在此处输入图片说明

Note that the new .csproj format in Visual studio 2017 is supported also.请注意,也支持 Visual Studio 2017 中的新 .csproj 格式。 If you create your project as a ASPNET Web Project, the Azure Functions runtime downloads all the nuget packages necessary before building your project.如果将项目创建为 ASPNET Web 项目,Azure Functions 运行时会在生成项目之前下载所有必需的 nuget 包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM