2017-10-28 154 views
0

我有一个Azure功能,我需要通过IoT Hub将消息发送回我的设备。 每次我尝试添加语句:无法将Microsoft.Azure.Devices命名空间添加到Azure功能run.csx

using Microsoft.Azure.Devices; 

我得到下面的错误:

run.csx(11,23): error CS0234: The type or namespace name 'Devices' does not exist in the namespace 'Microsoft.Azure' (are you missing an assembly reference?)

但奇怪的错误提示,如果我只是去为using Microsoft.Azure,没有编译错误。

所以我尝试使用设备与我的代码,如static Devices.ServiceClient client;,但同样的错误。

我也尝试过使用#r "Microsoft.Azure.Devices,但那也没用。

我试着完全创建一个新的函数应用程序服务,但同样的错误只是不断弹出。

我project.json文件看起来是这样的:

{ 
    "frameworks": { 
    "net47":{ 
     "dependencies": { 
     "Microsoft.Azure.Devices": "1.4.1" 
     } 
    } 
    } 
} 

我尝试使用net46也。

我的应用程序的小能解密:

所以我的职责应该由ServiceBusTopic触发,具有BlobStorage作为输入。

不知道如何将我的数据发送回我的设备,因为此名称空间未被添加。

+0

我正在调查此问题。我能够repro(应该是“net46”,但你尝试过,就像我一样)。与此同时,如果我为Visual Studio 2017使用Azure函数工具,我可以很好地工作。他们使用。net [类库](https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library)比.csx更容易处理。看看在这期间是否有效,我会继续探讨。 – jeffhollan

回答

0

以下是使用Microsoft.Azure.Devices nuget软件包的工作示例。触发器是ServiceBusQueueblobName是BrokeredMessage的应用程序属性。

run.csx:

using System; 
using System.Text; 
using System.Configuration; 
using System.Threading.Tasks; 
using Microsoft.Azure.Devices; 

public static async Task Run(string inputMessage, string inputBlob, TraceWriter log) 
{ 
    log.Info($"C# ServiceBus queue trigger function processed message: {inputMessage}"); 
    log.Info($"Blob:\n{inputBlob}"); 


    string deviceId = "Device10"; 
    var c2dmsg = new Microsoft.Azure.Devices.Message(Encoding.ASCII.GetBytes(inputMessage)); 

    // create proxy 
    string connectionString = ConfigurationManager.AppSettings["myIoTHub2"]; 
    var client = ServiceClient.CreateFromConnectionString(connectionString); 

    // send AMQP message 
    await client.SendAsync(deviceId, c2dmsg); 

    await client.CloseAsync(); 
} 

function.json:

{ 
     "bindings": [ 
     { 
     "name": "inputMessage", 
     "type": "serviceBusTrigger", 
     "direction": "in", 
     "queueName": "function", 
     "connection": "myServiceBus", 
     "accessRights": "Manage" 
     }, 
     { 
     "type": "blob", 
     "name": "inputBlob", 
     "path": "afstates/{Properties.blobName}", 
     "connection": "myStorage", 
     "direction": "in" 
     } 
     ], 
     "disabled": false 
    } 

project.json:

{ 
    "frameworks": { 
    "net46":{ 
    "dependencies": { 
     "Microsoft.Azure.Devices": "1.4.1" 
     } 
    } 
    } 
} 

尝试以下步骤:

  • 删除文件project.lock.json
  • 使用net46依赖
  • 恢复NuGet包Save按钮。

观察日志进度。

+0

我知道代码,但不幸的是,程序集没有在我的天青门户功能应用程序上编译。它承认直到Microsoft.Azure,但它的类似设备从未存在于Microsoft.Azure内部。 :( – SunainaDG

+0

我可以使用net47依赖关系重现你的错误,所以再试一次:删除文件project.lock.json,使用net46依赖关系并且按下保存按钮恢复nuget包。观察日志进度。 –

+0

这工作得很好,非常感谢,如果你只是更新这个信息,我会将其标记为我的答案。 – SunainaDG