无法在 Azure Iot Hub 中将云编译为设备功能
Unable to compile cloud to device Function in Azure Iot Hub
我正在实施 Azure 函数以在 Iot Hub 事件触发器上向设备发送消息。问题是我遇到了很多未定义命名空间的编译错误。
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"
}
}
}
}
错误:
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.
1) 请在 project.json 中添加对 Microsoft.Azure.Devices(1.18.1) 和 Microsoft.Azure.Devices.Client(1.21.0) 的引用,然后将其包含在函数代码中
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Client;
2)缺少博文中的命令class:
public class Command
{
public string devicename {get; set;}
public int action {get; set;}
}
3) myEventHubMessage 没有任何参考。您正在使用 myIoTHubMessage 代替它。
4) 使用 Microsoft.Azure.Devices。客户端应该会收到消息 class。
这就是我在普通 C# 中所做的 class。您可以根据需要在 Azure Functions 中调整此代码。
CloudToDeviceMethod cloudtoDeviceMethod = new
CloudToDeviceMethod(DeviceMethod,
TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30));
ServiceClient serviceClient = null;
serviceClient =
ServiceClient.CreateFromConnectionString("HostName=myhub.azure-
devices.net;SharedAccessKeyName=service;SharedAccessKey=xxxxxxxxxxxxxxxx");
await serviceClient.OpenAsync();
var response=await serviceClient.InvokeDeviceMethodAsync(DeviceId,
cloudtoDeviceMethod);
var json = @"{""Response:" +
response.GetPayloadAsJson()+@""",Status:""" +
response.Status + @"}";
if (serviceClient != null)
{
await serviceClient.CloseAsync();
}
测试表明 Microsoft.Azure.Devices 依赖于 Newtonsoft.Json。好的部分是在博客中依赖是 added.Could 你请尝试在 project.json 中添加对 Newtonsoft.Json(9.0.1) 的依赖并尝试。无论如何,看起来这些包 Microsoft.Azure.Devices 和 Microsoft.Azure.Devices.Client 没有被安装,这就是代码抱怨名称空间不存在的原因。
另一种选择是先在 Visual Studio 中编写函数,然后将其部署到 Azure.That,这样您就可以在将其放入云之前测试机器中的所有依赖项。希望这有帮助。
我正在实施 Azure 函数以在 Iot Hub 事件触发器上向设备发送消息。问题是我遇到了很多未定义命名空间的编译错误。
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"
}
}
}
}
错误:
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.
1) 请在 project.json 中添加对 Microsoft.Azure.Devices(1.18.1) 和 Microsoft.Azure.Devices.Client(1.21.0) 的引用,然后将其包含在函数代码中
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Client;
2)缺少博文中的命令class:
public class Command
{
public string devicename {get; set;}
public int action {get; set;}
}
3) myEventHubMessage 没有任何参考。您正在使用 myIoTHubMessage 代替它。
4) 使用 Microsoft.Azure.Devices。客户端应该会收到消息 class。
这就是我在普通 C# 中所做的 class。您可以根据需要在 Azure Functions 中调整此代码。
CloudToDeviceMethod cloudtoDeviceMethod = new
CloudToDeviceMethod(DeviceMethod,
TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30));
ServiceClient serviceClient = null;
serviceClient =
ServiceClient.CreateFromConnectionString("HostName=myhub.azure-
devices.net;SharedAccessKeyName=service;SharedAccessKey=xxxxxxxxxxxxxxxx");
await serviceClient.OpenAsync();
var response=await serviceClient.InvokeDeviceMethodAsync(DeviceId,
cloudtoDeviceMethod);
var json = @"{""Response:" +
response.GetPayloadAsJson()+@""",Status:""" +
response.Status + @"}";
if (serviceClient != null)
{
await serviceClient.CloseAsync();
}
测试表明 Microsoft.Azure.Devices 依赖于 Newtonsoft.Json。好的部分是在博客中依赖是 added.Could 你请尝试在 project.json 中添加对 Newtonsoft.Json(9.0.1) 的依赖并尝试。无论如何,看起来这些包 Microsoft.Azure.Devices 和 Microsoft.Azure.Devices.Client 没有被安装,这就是代码抱怨名称空间不存在的原因。
另一种选择是先在 Visual Studio 中编写函数,然后将其部署到 Azure.That,这样您就可以在将其放入云之前测试机器中的所有依赖项。希望这有帮助。