I'm implementing a Azure Function to send a message to device on Iot Hub event trigger. The problem I'm getting a lot of compile errors with undefined namespaces.
The example is based on this article: https://sandervandevelde.wordpress.com/2016/06/14/closing-the-windows-iot-core-feedback-loop-using-azure-functions/
using System;
using Microsoft.Azure.Devices;
using System.Text;
using Newtonsoft.Json;
public static void Run(string myIoTHubMessage, ILogger log)
{
var connectionString = "HostName=iot-hub-teste.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey=hiden";
var serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
var message = JsonConvert.DeserializeObject<Command>(myEventHubMessage);
var commandMessage = new Message(Encoding.ASCII.GetBytes("Cloud to device message."));
serviceClient.SendAsync("MyNodeDevice", commandMessage);
}
project.json
{
"frameworks": {
"net46":{
"dependencies": {
"Microsoft.Azure.Devices": "1.4.1"
}
}
}
}
The errors:
2019-08-23T18:03:47.255 [Error] Function compilation error
Microsoft.CodeAnalysis.Scripting.CompilationErrorException : Script compilation failed.
at async Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.CreateFunctionTarget(CancellationToken cancellationToken) at C:\azure-webjobs-sdk-script\src\WebJobs.Script\Description\DotNet\DotNetFunctionInvoker.cs : 314
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.Azure.WebJobs.Script.Description.FunctionLoader`1.GetFunctionTargetAsync[T](Int32 attemptCount) at C:\azure-webjobs-sdk-script\src\WebJobs.Script\Description\FunctionLoader.cs : 55
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.GetFunctionTargetAsync(Boolean isInvocation) at C:\azure-webjobs-sdk-script\src\WebJobs.Script\Description\DotNet\DotNetFunctionInvoker.cs : 183
2019-08-23T18:03:47.427 [Error] run.csx(3,23): error CS0234: The type or namespace name 'Devices' does not exist in the namespace 'Microsoft.Azure' (are you missing an assembly reference?)
2019-08-23T18:03:47.477 [Error] run.csx(13,25): error CS0103: The name 'ServiceClient' does not exist in the current context
2019-08-23T18:03:47.541 [Error] run.csx(15,49): error CS0246: The type or namespace name 'Command' could not be found (are you missing a using directive or an assembly reference?)
2019-08-23T18:03:47.630 [Error] run.csx(15,58): error CS0103: The name 'myEventHubMessage' does not exist in the current context
2019-08-23T18:03:47.694 [Error] run.csx(17,30): error CS0246: The type or namespace name 'Message' could not be found (are you missing a using directive or an assembly reference?)
2019-08-23T18:03:47.766 [Error] Executed 'Functions.msg_to_device' (Failed, Id=de327298-1973-4bc5-82b6-0c7b05835dae)
Script compilation failed.