How to add reference to directives in Azure function via Portal

Nandan Hegde 32,026 Reputation points MVP
2020-09-25T06:53:15.447+00:00

Hello All,

I have created a function via Portal

28209-af.png

When I execute the code, i am getting the below error:
The namespace Newtonsoft not exits.

Similarly getting the error for all the above directives.

So wanted to know how do we add reference of the directives to the function via Azure Portal?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,882 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Krish G 2,331 Reputation points
    2020-09-25T07:51:56.543+00:00

    Since you are using C# script (.csx), and not the regular C# here, to load external assembly like "Newtonsoft.Json", "Microsoft.WindowsAzure.Storage", add below on top of your script.

    #r "Newtonsoft.Json"  
    #r "Microsoft.WindowsAzure.Storage"  
    

    The following assemblies are automatically added by the Azure Functions hosting environment:

    mscorlib
    System
    System.Core
    System.Xml
    System.Net.Http
    Microsoft.Azure.WebJobs
    Microsoft.Azure.WebJobs.Host
    Microsoft.Azure.WebJobs.Extensions
    System.Web.Http
    System.Net.Http.Formatting

    The following assemblies may be referenced by simple-name (for example, #r "AssemblyName"):

    Newtonsoft.Json
    Microsoft.WindowsAzure.Storage
    Microsoft.ServiceBus
    Microsoft.AspNet.WebHooks.Receivers
    Microsoft.AspNet.WebHooks.Common
    Microsoft.Azure.NotificationHubs

    For any other outside the above list, consider adding nuget reference as mentioned in Using NuGet packages.

    For anything which does not have nuget packages. follow Referencing custom assemblies.

    NOTE: There are specific ways of loading assembly on .csx script in Azure Function. Luckily, in this case, 'Newtonsoft.Json' is available out of the box. Refer this for details. Also, you won't be able to upload dll from portal if WEBSITE_RUN_FROM_PACKAGE=1, you need to go via publish route.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.