且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Azure Functions 中使用 NuGet 包?

更新时间:2023-02-08 23:19:07

是的!尽管 Azure Functions 门户目前不提供添加和管理 NuGet 包的机制,但运行时支持 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.

为了定义您的依赖项,您需要使用所需的 NuGet 包引用创建一个 Project.json 文件.这是一个添加对 Microsoft.ProjectOxford.Face 版本 1.1.0 的引用的示例:

In order to define your dependencies, you need to create a Project.json file with the required NuGet package references. Here is an example that adds a reference to Microsoft.ProjectOxford.Face version 1.1.0:

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

Azure Functions 门户提供了一种方便的方法来管理您的函数文件,我们可以使用它来创建(或上传)我们的 project.json:

The Azure Functions portal provides a convenient way to manage your function files, which we can use to create (or upload) our project.json:

  1. 在函数的开发部分,点击查看文件
  2. 点击选项创建一个文件(如果你的机器上有之前创建的project.json文件,你也可以点击上传文件的选项
  3. 将文件命名为 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
  3. Name the file project.json and define your package references (you can use the example above as a template).

包恢复过程将开始,您应该会在日志窗口中看到类似于以下内容的输出:

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)MSBuild14.0in'.
2016-04-04T19:02:50.261 Feeds used:
2016-04-04T19:02:50.261 C:DWASFilesSitesfacavalfunctestLocalAppDataNuGetCache
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:homesitewwwrootHttpTriggerCSharp1Project.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.

正如预期的那样,Azure Functions 运行时将自动添加对包程序集的引用,因此您无需使用 #r "AssemblyName" 显式添加程序集引用,您只需添加所需的using 语句到您的函数并使用您引用的 NuGet 包中定义的类型.

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 构建在应用服务之上,因此作为上述步骤的替代方法,您还可以访问标准 Azure Web 应用(Azure 网站)可用的所有出色部署选项.

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).

以下是一些示例:

为了使用应用服务编辑器(摩纳哥)直接从浏览器管理您的文件:

In order to manage your files directly from your browser by using the App Service Editor (Monaco):

  • 在 Azure Functions 门户上,点击Function app settings
  • 高级设置部分下,点击转到应用服务设置
  • 点击工具按钮
  • 开发下,点击应用服务编辑器
  • 将它On(如果它尚未启用)并点击Go
  • 加载后,将您的 project.json 文件拖放到您的函数文件夹(以您的函数命名的文件夹.
  • On the Azure Functions portal, click on Function app settings
  • Under the Advanced Settings section, click on Go to App Service Settings
  • Click on the Tools button
  • Under Develop, click on App Service Editor
  • Turn it On if it is not already enabled and click on Go
  • Once it loads, drag-and-drop your project.json file into your function's folder (the folder named after your function.
  • 导航到:https://.scm.azurewebsites.net
  • 点击调试控制台> CMD
  • 导航到 D:homesitewwwroot
  • 将您的 Project.json 文件拖放到文件夹中(到文件网格上)
  • Navigate to: https://<function_app_name>.scm.azurewebsites.net
  • Click on Debug Console > CMD
  • Navigate to D:homesitewwwroot<function_name>
  • Drag-and-drop your Project.json file into the folder (onto the file grid)
  • 按照此处的说明操作获取 FTP 配置
  • 连接后(按照上述说明)将您的 Project.json 文件复制到 /site/wwwroot/

  • Follow the instructions here to get FTP configured
  • Once connected (following the instructions above) copy your Project.json file to /site/wwwroot/<function_name>

有关其他部署选项,请参阅:https://azure.microsoft.com/en-us/documentation/articles/web-sites-deploy/

For additional deployment options, see: https://azure.microsoft.com/en-us/documentation/articles/web-sites-deploy/

如果在函数应用未运行时启用持续集成并使用 project.json 文件部署函数,则函数应用初始化后将自动进行包还原.建议您不要将您的 project.lock.json 文件添加到源代码管理中.

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. It is recommended that you do not add your project.lock.json file to source control.

函数也可以部署为预编译的程序集,在这种情况下,所有依赖项管理都在 Visual Studio 中处理.此选项可用作任何版本的 Visual Studio 上的标准类库或通过使用 Visual Studio 2017 Azure Functions 工具.

Functions may also be deployed as pre-compiled assemblies, and in this case, all dependency management is handled in 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.