How can I use NuGet packages in my Azure Functions?
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
:
- In the function's develop section, click on view files
- 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 - 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 onGo
- 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
- Follow the instructions here to get FTP configured
Once connected (following the instructions above) copy your
Project.json
file to/site/wwwroot/<function_name>
For additional deployment options, see: https://azure.microsoft.com/en-us/documentation/articles/web-sites-deploy/
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.
Including a NuGet package in an Azure function written in the Portal
There is correction in namespace
#r "Microsoft.WindowsAzure.Storage"
and not
#r "WindowsAzure.Storage"
You can refer - https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#binding-to-arguments
Azure Function Queue Trigger Adding Nuget Package
Per the documentation, for C# Script (.csx) functions, you can upload a function.proj
file that contains a PackageReference
to a nuget package. This is similar to a .csproj file. In your case, it should probably be like so:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.1.2" />
</ItemGroup>
</Project>
How do I import a Nuget package correctly for Azure Function v2?
In order to reference an external dependency in .csx files, you need to add #r <PackageName>
(#r Newtonsoft.Json
in this case) to the top of the file.
Only certain dependencies are automatically referenced in Azure Functions, and select others are available without adding them to your project.json or function.proj file, as long as you reference them with the #r
notation. For a more complete list, check out the C# developer reference for Azure Functions.
Unable to install nuget package into azure function
To use NuGet packages in a 2.x and later C# function, upload a function.proj
file to the function's folder in the function app's file system. Here is an example function.proj
file that adds a reference to HtmlAgilityPack
version 1.11.24
:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.24" />
</ItemGroup>
</Project>
And add using HtmlAgilityPack;
above the code, refer to the snapshot as below:
How do you use custom NuGet feeds with Azure Functions?
Azure functions supports the standard nuget.config approach to setup custom sources.
You can place a nuget.config file with your custom sources either in your function folder or in the Function App root (where it will be used for all functions)
You can find more information about the file format and specifying custom sources here: https://docs.nuget.org/consume/nuget-config-file#package-sources
Publish Azure Function as NuGet package (or load function from external assembly)
Adding <FunctionsInDependencies>true</FunctionsInDependencies>
in the .csproj
file seems to do the trick.
Do make sure you actually call the dependency somewhere in the code (e.g. in Startup.cs
) to make sure the assembly isn't optimized away by the compiler.
How to Use NuGet Package in VSTS Package Management with Azure Functions?
You can reference packages in a private NuGet repository using the information available here.
Once you add your private source to the config file, you can add references to your custom packages following the information outlined here.
(Additional edits from OP...)
Example project.json
{
"frameworks": {
"net46":{
"dependencies": {
"Contoso.Models": "1.2.0",
"Contoso.DAL ": "1.2.0"
}
}
}
}
project.json
is added to root folder for your function.
Example nuget.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="MyVSTSPackageManagementFeed" value="https://contoso.pkgs.visualstudio.com/_packaging/Contoso/nuget/v3/index.json" />
</packageSources>
<activePackageSource>
<add key="All" value="(Aggregate source)" />
</activePackageSource>
<packageSourceCredentials>
<MyVSTSPackageManagementFeed>
<add key="Username" value="me@contoso.com" />
<add key="ClearTextPassword" value="<MyPersonalAccessTokenHere>" />
</MyVSTSPackageManagementFeed>
</packageSourceCredentials>
</configuration>
nuget.config
is added to either the root folder for your function or if you want to use them across all of your functions, you can add it to the host level folder.
Related Topics
Reference Assemblies for Framework ".Netframework,Version=V4.7.1" Were Not Found
Reading a C/C++ Data Structure in C# from a Byte Array
Get the Generated SQL Statement from a SQLcommand Object
Creating Application Shortcut in a Directory
Make Https Call Using Httpclient
Combobox: Adding Text and Value to an Item (No Binding Source)
How to Serialize a Dictionary as Part of Its Parent Object Using JSON.Net
Are Arrays or Lists Passed by Default by Reference in C#
Why Filesystemwatcher Doesn't Work in Linux Container Watching Windows Volume
How to Get and Set Environment Variables in C#
Launching an Application (.Exe) from C#
How to Determine the Owner of a Process in C#
Open Existing File, Append a Single Line
How to Parse Huge JSON File as Stream in JSON.Net
Default Visibility for C# Classes and Members (Fields, Methods, etc.)