2017-06-12 77 views
0

我有一個天青函數,它接受從IoTHub並把它的消息上用於處理一個給定的隊列存儲帳戶的名稱。隊列在由傳入消息數據運行時動態地確定並放置在隊列具有短ExpirationTime如我只想消息持續幾秒鐘:指定從天青功能使用鍵入CloudQueue

#r "Microsoft.WindowsAzure.Storage" 
#r "Newtonsoft.Json" 

using System; 
using Microsoft.WindowsAzure.Storage.Queue; 
using Newtonsoft.Json; 


public static void Run(MyType myEventHubMessage, IBinder binder, TraceWriter log) 
{ 
    int TTL = 3; 
    var data = JsonConvert.SerializeObject(myEventHubMessage); 
    var msg = new CloudQueueMessage(data); 

    string outputQueueName = myEventHubMessage.DeviceId; 
    QueueAttribute queueAttribute = new QueueAttribute(outputQueueName); 
    CloudQueue outputQueue = binder.Bind<CloudQueue>(queueAttribute); 

    outputQueue.AddMessage(msg, TimeSpan.FromSeconds(TTL), null, null, null); 

} 

public class MyType 
{ 
    public string DeviceId { get; set; } 
    public double Field1 { get; set; } 
    public double Field2 { get; set; } 
    public double Field3 { get; set; } 

} 

此,運作良好,郵件是寫入我的隊列。但是,它正在寫入的隊列不是我想要使用的存儲帳戶!它似乎從其他地方獲取存儲帳戶?!?

我在function.json連接屬性:

{ 
    "bindings": [ 
    { 
     "type": "eventHubTrigger", 
     "name": "myEventHubMessage", 
     "direction": "in", 
     "path": "someiothub", 
     "connection": "IoTHubConnectionString" 
    }, 
    { 
     "type": "CloudQueue", 
     "name": "$return", 
     "queueName": "{DeviceId}", 
     "connection": "NAME_OF_CON_STRING_I_WANT_TO_USE", 
     "direction": "out" 
    } 
    ], 
    "disabled": false 
} 

但它被完全忽略。面對面,我可以從JSON中完全刪除值或鍵/值對,該功能仍在運行,並寫入似乎是某個默認存儲帳戶的地方。

我試着加入[StorageAccount("NAME_OF_CON_STR_I_WANT_TO_USE")]屬性我運行的功能,但也似乎被忽略,我也嘗試創建一個屬性陣列和傳球都QueueAttribute和StorageAccountAttributebinder.Bind<T>(attributeArray)但抱怨說,它不能接受的數組。

有誰知道首先從哪裏獲得存儲帳戶,它正在使用,更重要的是我可以如何設置存儲帳戶名稱?

感謝

回答

1

你是在正確的軌道上:你需要傳遞StorageAccountAttribute在屬性粘合劑的數組。一個原因是未知對我來說,它看起來像只有異步版本具體類Binder方法的支持傳遞數組。這樣的東西應該工作:

public static async Task Run(MyType myEventHubMessage, Binder binder, TraceWriter log) 
{ 
    // ... 
    var queueAttribute = new QueueAttribute(outputQueueName); 
    var storageAttribute = new StorageAccountAttribute("MyAccount"); 
    var attributes = new Attribute[] { queueAttribute, storageAttribute }; 
    CloudQueue outputQueue = await binder.BindAsync<CloudQueue>(attributes); 
    // ... 
} 

順便說一句,你不需要在function.json指定必須綁定任何配置:它們將被忽略。只要刪除這些以避免混淆(當然要保留觸發器)。

+0

感謝您的答覆。這是記錄在任何地方?我在數組中傳遞相同的錯誤:2017-06-13T09:43:56.381(20,65):error CS1503:Argument 1:無法從'System.Attribute []'轉換爲'System.Attribute' – LDJ

+0

我找不到文檔...你確定使用'Binder'(而不是'IBinder')和'BindAsync'(而不是'Bind')嗎?適用於我。 – Mikhail

+0

對不起,你是對的。忘了將IBinder更改爲Binder。現在就像魅力一樣。再次感謝您的幫助 :) – LDJ