How can the NuGet packages be used in Azure Functions?

+1 vote

Using Azure Functions, can I reference and use NuGet packages in my C# function?

Jun 8, 2018 in Azure by cloudie_crank
• 1,610 points
4,998 views

10 answers to this question.

+1 vote
Best answer

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.

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

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

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

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

Using SCM (Kudu) endpoint

  • Navigate to: https://<function_app_name>.scm.azurewebsites.net
  • Click on Debug Console > CMD
  • Navigate to D:\home\site\wwwroot\<function_name>
  • Drag-and-drop your Project.json file into the folder (onto the file grid)

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

Pre-compiled assemblies

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.

answered Jun 8, 2018 by club_seesharp
• 3,450 points

selected Aug 21, 2018 by Priyaj
0 votes

You can use Nuget packages in your Azure Functions. Easiest way will be to use Visual Studio 2017 15.4 where there is a template for Azure Functions. Follow below steps

1) Add Azure function Project : Right click on solution and select Add New project. Go to CLOUD option there you will find "Azure Function" project.

Azure Function

2) Now it's pretty to add any Nuget package. Expand "DEPENDENCIES" and right click on it to select option "Manage Nuget Packages". Nuget Package dialog will appear, select any Nuget package you want to install. See screenshot below

3) Now publish your Azure function, Visual Studio will take care of all settings, etc.

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.

answered Aug 21, 2018 by Priyaj
• 56,900 points
0 votes
You can follow this guide from microsoft documentation

https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp
answered Dec 10, 2018 by Nabarupa
0 votes

The current version of the Azure Functions Runtime (1.x) allows you to install NuGet packages for our C# and F# functions. The way to do it is adding the dependencies to a file named project.json. As you may be thinking this is because when the 1.x version was designed all was about project.json files.

answered Dec 10, 2018 by Shuvodip
0 votes
You can configure the NuGet package by using following CLI command

nuget config -Set <name>=[<value>] [<name>=<value> ...] [options]
nuget config -AsPath <name> [options]
answered Dec 10, 2018 by bugseeker
0 votes

Use the following command

nuget config -Set repositoryPath=c:\packages -configfile c:\my.config
nuget config -Set repositoryPath=
nuget config -Set repositoryPath=%PACKAGE_REPO% -configfile %ProgramData%\NuGet\NuGetDefaults.Config
nuget config -Set HTTP_PROXY=http://127.0.0.1 -set HTTP_PROXY.USER=domain\user
answered Dec 10, 2018 by rishav
0 votes
You can follow the documentation here to setup your NuGet package for C#

https://docs.microsoft.com/en-us/nuget/tools/cli-ref-config
answered Dec 10, 2018 by Sukesh
0 votes

You can use NuGet api 

nuget setapikey 4003d786-cc37-4004-bfdf-c4f3e8ef9b3a
nuget setapikey 4003d786-cc37-4004-bfdf-c4f3e8ef9b3a -source https://example.com/nugetfeed
answered Dec 10, 2018 by User57209
0 votes

The behavior of the nuget.exe CLI can be configured through a number of environment variables, which affect nuget.exe on computer-wide, user, or process levels. Environment variables always override any settings in NuGet.Config files, allowing build servers to change appropriate settings without modifying any files.

In general, options specified directly on the command line or in NuGet configuration files have precedence, but there are a few exceptions such as FORCE_NUGET_EXE_INTERACTIVE. If you find that nuget.exe behaves differently between different computers, an environment variable could be the cause. For example, Azure Web Apps Kudu (used during deployment) has NUGET_XMLDOC_MODE set to skip to speed up package restore performance and save disk space.

answered Dec 10, 2018 by datageek
0 votes
Follow the instruction here to setup your NuGet packages

https://docs.microsoft.com/en-us/nuget/consume-packages/configuring-nuget-behavior
answered Dec 10, 2018 by Sona

Related Questions In Azure

0 votes
1 answer

How to calculate the used database space in SQL Azure?

I don't think its possible to find ...READ MORE

answered Mar 5 in Azure by Archana
• 5,560 points
32 views
0 votes
1 answer

How can I remove/hide/disable excessive HTTP response headers in Azure/IIS7 without having to use UrlScan?

MSDN published an article on how to ...READ MORE

answered May 21, 2018 in Azure by club_seesharp
• 3,450 points
548 views
0 votes
2 answers

How can I add database connection string to Azure Functions?

The best way to do this is ...READ MORE

answered Aug 17, 2018 in Azure by Priyaj
• 56,900 points
3,375 views
0 votes
1 answer

How to determine the number of messages in an Azure Services Bus Queue?

You will get error when you try ...READ MORE

answered Jun 12, 2018 in Azure by null_void
• 3,220 points
256 views
0 votes
1 answer

How can we rewrite an URL in Azure Webapp?

You need to create a web.config file ...READ MORE

answered Jun 14, 2018 in Azure by null_void
• 3,220 points
979 views
0 votes
1 answer

How to get the list of names of Azure blob files in a container?

We can get some additional info like Size, ...READ MORE

answered Jun 27, 2018 in Azure by club_seesharp
• 3,450 points
1,321 views
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

How can I view the deployed files in Azure?

In Visual Studio, in the window "Server ...READ MORE

answered Aug 21, 2018 in Azure by Priyaj
• 56,900 points
288 views
0 votes
2 answers

How can I download a .vhd image to my local machine from azure and upload the same to a different azure account?

From the Windows Azure Portal you can ...READ MORE

answered Aug 20, 2018 in Azure by Priyaj
• 56,900 points
2,719 views