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

- 2,017
- 15
- 22

- 12,328
- 3
- 35
- 43
-
I had to update my project.json file manually in order to restart the function service.. then it restored my nuget packages. Anyone knows a better way to restart the service to make it restore the packages? Kudu didn't help – Erik Bergstedt Mar 04 '17 at 20:06
-
1You can use the standard site restore option in the function app settings. But keep in mind that, when a function is first executed, if the project.json file contains a set of references that differs from what was previously restored, a restore operation will happen at that time, automatically. – Fabio Cavalcante Mar 06 '17 at 17:21
-
any latest update on this thread would help with my post here: https://stackoverflow.com/questions/68136717/adding-aws-kinesis-firehose-nuget-package-as-dependency-in-azure-functionsazure – coder kemp Jun 28 '21 at 21:29
5 Answers
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.

- 7,557
- 12
- 62
- 102

- 12,328
- 3
- 35
- 43
-
1What is `function script root` at your last paragraph? I understand it is where `project.json` is located. Is that correct? – justinyoo Apr 12 '16 at 13:23
-
2@JustInChronicles, that is actually one folder up from that. The `project.json` is in your function folder, which is a child folder of the script root. The script root maps to your `wwwroot` folder in Azure. – Fabio Cavalcante Apr 12 '16 at 18:18
-
-
@FabioCavalcante, Love the nuget package you put in example, so many amazing thing wth azure cognitive services !!! – Thomas Apr 25 '16 at 05:49
-
There is now a "View files link" in the UI. Files can be added through it. @FabioCavalcante, it would be helpful to update your answer. – chris Oct 07 '16 at 18:56
-
Thanks for the message, I have been meaning to do that! Updated the answer to document the current in portal experience, new runtime behavior and recommendations when using continuous deployment. – Fabio Cavalcante Oct 10 '16 at 01:01
-
This doesn't work, at all, like most of Functions in my experience. Following exactly what the docs describe, nothing happens when I check in to Git and the app updates. Sigh. – Josh Nov 04 '16 at 11:50
-
Josh, sorry to hear you're having trouble with this. Could you start a thread on MSDN forums (https://social.msdn.microsoft.com/Forums/azure/en-US/home?forum=AzureFunctions) or another question here on SO so we can look at the details of the issues you're running into and get them resolved? Thank you for your patience! – Fabio Cavalcante Nov 04 '16 at 16:45
-
Please note that 'Visual Studio Online' mentioned in the stackoverflow answer above http://stackoverflow.com/a/36411537/5288052 is available from Azure tools as 'App Service Editor' (see http://stackoverflow.com/a/38173438/5288052 ) – Stefano Spinucci Sep 18 '16 at 20:49
-
This approach seems changed now. Now all the dependencies are managed in
.deps.json under wwwroot when you open the application is KUDU – hungryMind Sep 18 '19 at 09:23 -
hungryMind that's not correct. What's in the docs and described above still applies, the deps file serves a different purpose here. – Fabio Cavalcante Sep 18 '19 at 17:20
This thread helped me a lot - but I still wasted a few hours trying to get the Project.json to work - to no avail.
If you make an Azure function in version 2.x you need to do this in a different way.
Create a new file as stated but name it function.proj. This file has an XML structure for importing libraries via Nuget.
Here is my example importing the Amazon S3 SDK for .Net;
<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.

- 1,221
- 1
- 14
- 27
-
2This was a big help, thanks for posting. [Here](https://learn.microsoft.com/en-us/dotnet/core/tools/project-json-to-csproj) is a link with more info. – May 29 '19 at 13:46
-
1Like you, I spent hours to do the same. That works. Thanks a lot. Love stackoverflow! – karrtojal May 30 '19 at 14:38
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.
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.

- 752
- 1
- 9
- 23
-
1This is the standard flow for VS/Class Libraries (which is what the Azure Functions project is) and not specific to Azure Functions. The only steps specific to Azure Functions are the ones documented below. I'll add a note to my original answer, including the details for the updated procedure in 2.0. – Fabio Cavalcante Nov 28 '17 at 18:12
Let's assume we want to use the SFTP
client, which is an external library, stored somewhere in NuGet
.
To accomplish that, according to the latest Azure Functions specification, do the following:
Open files section inside your Azure Function and add a new file called
function.proj
.For more information refer to Microsoft documentation.
Inside that
function.proj
put theNuget
package reference using theXML
structure (the same kind of structure you can find inside*.csproj
file if you create a local project with Visual Studio and install someNuGet
package into it).
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"
Finally, open the Logs section and save the code. After some time you should see the
NuGet
packages restoring log.Note: these logs could also appear when saving
function.proj
file or when running the project.
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.

- 7,904
- 4
- 42
- 42
-
Has something changed since this answer? This doesn't seem to work anymore. I am seeing a "Compilation service error" - Executed 'Functions.TimerTrigger2' (Failed, Id=a318d5b7-a02c-4ba0-bb04-037beda89b1c) Could not find file 'D:\home\site\wwwroot\bin\Google.Apis.Sheets.v4.dll'. – user Sep 03 '19 at 18:07
-
When you are installing a script via the `function.proj`, the location for the `dll` is appearing in the Logs section after some period of time. Sometimes you need to reload the page for the Azure Function once more, sometimes hit the "Run" button to see the NuGet packages installation and also the location. Please check what is printing in the Log section as location and try to put that. – Arsen Khachaturyan Sep 04 '19 at 15:37
Note that the new .csproj format in Visual studio 2017 is supported also. If you create your project as a ASPNET Web Project, the Azure Functions runtime downloads all the nuget packages necessary before building your project.

- 1,321
- 1
- 19
- 30
-
For the ASP.NET Web Project, VS 2015 works as well. For the Azure Functions Tools, that's VS 2017 only. The key difference there is that you're deploying pre-compiled bits (and dependencies), so the runtime is not handling the restore for you, but yes, that is indeed a newer way to write functions and manage dependencies. I'll update the answer to reflect the latest features. – Fabio Cavalcante May 16 '17 at 20:41