Using Azure Functions, can I reference and use NuGet packages in my C# function?
相关问题
- running headless chrome in an microsoft azure web
- running headless chrome in an microsoft azure web
- Docker task in Azure devops won't accept "$(pw
- Register MicroServices in Azure Active Directory (
- Removing VHD's from Azure Resource Manager aft
相关文章
- SQL Azure Reset autoincrement
- How to cast Azure DocumentDB Document class to my
- Can't get azure web role to run locally using
- Azure WebApp - Unable to auto-detect the runtime s
- How to change region for Azure WebSite
- Azure webjob vs cloud service
- Azure data transfer Identity Column Seed Jumped by
- Download Azure web app?
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.
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.
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 toMicrosoft.ProjectOxford.Face
version 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:
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 requiredusing
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):
Function app settings
Go to App Service Settings
Tools
buttonOn
if it is not already enabled and click onGo
project.json
file into your function's folder (the folder named after your function.Using SCM (Kudu) endpoint
https://<function_app_name>.scm.azurewebsites.net
D:\home\site\wwwroot\<function_name>
Project.json
file into the folder (onto the file grid)FTP
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 yourproject.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.
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;
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.