12

I tried the following:

using System;
using Newtonsoft.Json
using Newtonsoft.Linq

public static void Run(string myEventHubMessage, out string document, TraceWriter log)
{
    log.Verbose($"C# Event Hub trigger function processed a message: {myEventHubMessage}");
    dynamic jsonData = JObject.Parse(myEventHubMessage);
    document = jsonData;
}

I get the following when I hit the "Save" button in Azure portal:

2016-04-05T21:28:31 Welcome, you are now connected to log-streaming service. 2016-04-05T21:28:33.443 Script for function 'ProbeEventHubTrigger' changed. Reloading. 2016-04-05T21:28:33.443 Compiling function script. 2016-04-05T21:28:33.568 (2,22): error CS1002: ; expected 2016-04-05T21:28:33.568 (3,22): error CS1002: ; expected 2016-04-05T21:28:33.568 (2,7): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?) 2016-04-05T21:28:33.568 (3,7): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?) 2016-04-05T21:28:33.568 (8,24): error CS0103: The name 'JObject' does not exist in the current context 2016-04-05T21:28:33.568 Compilation failed.

I also tried the following:

#r "Newtonsoft.Json"
#r "Newtonsoft.Linq"
using System;

public static void Run(string myEventHubMessage, out string document, TraceWriter log)
{
    log.Verbose($"C# Event Hub trigger function processed a message: {myEventHubMessage}");
    dynamic jsonData = JObject.Parse(myEventHubMessage);
    document = jsonData;
}

In this case I get the following when I hit the "Save" button in the Azure portal:

2016-04-05T21:35:36 Welcome, you are now connected to log-streaming service. 2016-04-05T21:35:38.428 Script for function 'ProbeEventHubTrigger' changed. Reloading. 2016-04-05T21:35:38.428 Compiling function script. 2016-04-05T21:35:38.571 (2,1): error CS0006: Metadata file 'Newtonsoft.Linq' could not be found 2016-04-05T21:35:38.571 (8,24): error CS0103: The name 'JObject' does not exist in the current context

It's not obvious from the documentation how to reference these assemblies. I see in one example the syntax is "using Newtonsoft.Json", but this doesn't appear to work in the portal. Any suggestions?

Steve W.
  • 413
  • 1
  • 5
  • 12
  • Here other solution https://stackoverflow.com/questions/36411536/how-can-i-use-nuget-packages-in-my-azure-functions – karrtojal May 30 '19 at 15:39

1 Answers1

23

Steve,

.NET Framework assemblies and a few "shared" assemblies may be added with the following syntax:

#r "AssemblyName"

So, for JSON.NET, you can use:

#r "Newtonsoft.Json"

Once the reference is added, then you can add your using statements as you would in a regular C# project/file:

using Newtonsoft.Json;

So, in summary, you need to add a reference to the assemblies you want to use, and import the namespaces exposed by that assembly so you can use its types. This is similar to what you'd do in Visual Studio, where you add the assembly reference and then add your using statements where you need them.

I hope this helps!

avs099
  • 10,937
  • 6
  • 60
  • 110
Fabio Cavalcante
  • 12,328
  • 3
  • 35
  • 43
  • Thanks. You might want to consider updating the examples in your documentation to show this. – Steve W. Apr 05 '16 at 22:14
  • 1
    Thank you, Steve. We are actively working on samples and documentation to cover missing things and make it more discoverable. Some of the information I shared is covered here: https://azure.microsoft.com/en-us/documentation/articles/functions-reference/#referencing-external-assemblies but feel free to keep asking questions on SO and MSDN if you're unable to find the information you're looking for in the docs. We'll get there :) – Fabio Cavalcante Apr 05 '16 at 22:32
  • Thanks Fabio. So far I'm impressed with the simplicity at which you can dequeue messages from an event hub and dump them into DocumentDB. Nice work! – Steve W. Apr 05 '16 at 22:41
  • using a wrong variable in azure function can also be cause of no compile time error but get you run time error like "internal server error". – Ravi Anand Dec 19 '16 at 20:06
  • Thanks Fabio this helped – sensei Oct 31 '17 at 16:02