Using Azure Functions, can I reference and use NuGet packages in my C# function?
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
:
project.json
file on your machineproject.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.
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:
In order to manage your files directly from your browser by using the App Service Editor (Monaco):
Function app settings
Go to App Service Settings
Tools
buttonOn
if it is not already enabled and click on Go
project.json
file into your function's folder (the folder named after your function. https://<function_app_name>.scm.azurewebsites.net
D:\home\site\wwwroot\<function_name>
Project.json
file into the folder (onto the file grid)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/
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.
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.