我正在实现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.
答案 0 :(得分:0)
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)博客文章中缺少命令类:
public class Command
{
public string devicename {get; set;}
public int action {get; set;}
}
3)没有有关myEventHubMessage的引用。您正在使用myIoTHubMessage代替它。
4)使用Microsoft.Azure.Devices.Client应该可以获取Message类。
这就是我在普通C#类中所做的。也许您可以根据需要在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();
}
答案 1 :(得分:0)
测试表明Microsoft.Azure.Devices对Newtonsoft.Json有依赖性。好的部分是在博客中添加了依赖关系。您可以尝试在project.json中的Newtonsoft.Json(9.0.1)中添加依赖关系并尝试。一定看起来这些软件包Microsoft.Azure.Devices和Microsoft.Azure.Devices.Client尚未安装,这就是为什么代码抱怨名称空间不存在的原因。
另一种替代方法是先在Visual Studio中编写函数,然后将其部署到Azure。这样一来,您就可以在将计算机上的所有依赖项放入云之前进行大量测试。希望这会有所帮助。