0

I want to use EventData class to read event message from iot hub but I keep getting error below regarding adding the namespace Microsoft.ServiceBus.Messaging

Appreciate any help to identify what could be wrong?

#r "Newtonsoft.Json"
#r "Microsoft.ServiceBus" 


using System;
using Newtonsoft.Json.Linq;
using Microsoft.ServiceBus.Messaging;

public static void Run(EventData myIoTHubMessage, out object outDocument, ILogger log)
{

dynamic msg = JObject.Parse(myIoTHubMessage);
outDocument = new {msg}; 
log.LogInformation($"C# IoT Hub trigger function processed a message: {myIoTHubMessage}");
}

2020-06-06T23:05:31Z [Error] Function compilation error 2020-06-06T23:05:31Z [Error] run.csx(8,24): error CS0246: The type or namespace name 'EventData' could not be found (are you missing a using directive or an assembly reference?) 2020-06-06T23:05:31Z [Error] run.csx(4,17): error CS0234: The type or namespace name 'ServiceBus' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) 2020-06-06T23:05:31Z [Error] run.csx(2,1): error CS0006: Metadata file 'Microsoft.ServiceBus' could not be found 2020-06-06T23:05:31Z [Warning] You may be referencing NuGet packages incorrectly. Learn more: https://go.microsoft.com/fwlink/?linkid=2091419

rareg3636
  • 29
  • 4
  • 1
    Please try adding this package in your Azure Function project Microsoft.Azure.WebJobs.Extensions.ServiceBus and see if this helps. – Zainu Jun 07 '20 at 08:58
  • 1
    I’m doing this in the Azure portal, is that what you are thinking? If so Could you be more specific regarding the #r and using statements that I should include? Sorry still quite new to this – rareg3636 Jun 07 '20 at 19:29

1 Answers1

0

So, managed to get this working by adding in "using Microsoft.Azure.EventHubs;"

The code below works for me at the moment but not sure about best practices etc...

To figure it out I went back and created a new EventHub Trigger Function and took the code from there since the template already includes code using the EventData class. Not sure why they wouldnt use a similar template with IoT Hub trigger as well?

#r "Newtonsoft.Json"
#r "Microsoft.Azure.EventHubs"


using System;
using System.Text;
using Newtonsoft.Json.Linq;
using Microsoft.Azure.EventHubs;

public static void Run(EventData myIoTHubMessage, out object outDocument, ILogger log)
{
   string messageBody = Encoding.UTF8.GetString(myIoTHubMessage.Body.Array, 
   myIoTHubMessage.Body.Offset, myIoTHubMessage.Body.Count);
   dynamic msg = JObject.Parse(messageBody);

   var deviceId = myIoTHubMessage.SystemProperties["iothub-connection-device-id"];
   var enqueuedtime = myIoTHubMessage.SystemProperties["iothub-enqueuedtime"];
   var messageSource = myIoTHubMessage.SystemProperties["iothub-message-source"];


   outDocument = new {msg, deviceId, enqueuedtime, messageSource}; 
   log.LogInformation($"C# IoT Hub trigger function processed a message {myIoTHubMessage}");
}
rareg3636
  • 29
  • 4