2017-10-18 112 views
1

我正在使用Azure函數,嘗試使用輸出隊列來處理事情,但我一直運行時出現輸出綁定錯誤。我想知道這是否與我裝飾參數的方式有關?我已經看到如何使用單個隊列作爲返回值,但我需要有多個輸出隊列。Azure函數中的錯誤綁定字符串輸出隊列

函數定義:

using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Host; using Newtonsoft.Json; using Newtonsoft.Json.Linq; 

namespace FunctionApp1 { 
    public static class TestItemAddQueueTrigger 
    { 
     [StorageAccount("AzureWebJobsStorage")] 
     [FunctionName("TestItemAddQueueTrigger")] 
     public static void Run([QueueTrigger("testitem-add")] string itemAdd, 
      TraceWriter log, 
      [Queue("testitem-added", Connection = "AzureWebJobsStorage")] out string itemAddedQueue) 
     { 
      dynamic message = JObject.Parse(itemAdd); 

      log.Info($"Received message\n{JsonConvert.SerializeObject(message, Formatting.Indented)}"); 


      var itemAddedQueueMessage = new 
      { 
      }; 

      itemAddedQueue = JsonConvert.SerializeObject(itemAddedQueueMessage); 

      log.Info($"Sent message to queue \"itemAddedQueue\"\n{JsonConvert.SerializeObject(itemAddedQueueMessage, Formatting.Indented)}"); 
     } 
    } } 

錯誤消息:

A ScriptHost error has occurred 
[10/18/2017 4:31:27 PM] Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.TestItemAddQueueTrigger'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'itemAddedQueue' to type String&. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.). 

的NuGet出處:

Microsoft.NET.Sdk.Functions(1.0.6)Microsoft.Azure.WebJobs (2.1.0-beta4)Microsoft.Azure.WebJobs.Extensions(2.1.0-beta4)
Microsoft.Azure.WebJobs.Extensions.Http(1.0.0-BETA4)Newtonsoft.Json (9.0.1)System.ValueTuple(4.3.0)

項目文件:

<Project Sdk="Microsoft.NET.Sdk"> 
    <PropertyGroup> 
    <TargetFramework>net47</TargetFramework> 
    </PropertyGroup> 
    <ItemGroup>  
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.6" /> 
    </ItemGroup> 
    <ItemGroup> 
    <Reference Include="Microsoft.CSharp" /> 
    </ItemGroup> 
    <ItemGroup> 
    <None Update="host.json"> 
     <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> 
    </None> 
    <None Update="local.settings.json"> 
     <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> 
     <CopyToPublishDirectory>Never</CopyToPublishDirectory> 
    </None> 
    </ItemGroup> 
</Project> 

解決方案文件:

Microsoft Visual Studio Solution File, Format Version 12.00 
# Visual Studio 15 
VisualStudioVersion = 15.0.27004.2002 
MinimumVisualStudioVersion = 10.0.40219.1 
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunctionApp1", "FunctionApp1\FunctionApp1.csproj", "{10F86A7D-5E03-41A9-8BBB-103C8E06E059}" 
EndProject 
Global 
    GlobalSection(SolutionConfigurationPlatforms) = preSolution 
     Debug|Any CPU = Debug|Any CPU 
     Release|Any CPU = Release|Any CPU 
    EndGlobalSection 
    GlobalSection(ProjectConfigurationPlatforms) = postSolution 
     {10F86A7D-5E03-41A9-8BBB-103C8E06E059}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 
     {10F86A7D-5E03-41A9-8BBB-103C8E06E059}.Debug|Any CPU.Build.0 = Debug|Any CPU 
     {10F86A7D-5E03-41A9-8BBB-103C8E06E059}.Release|Any CPU.ActiveCfg = Release|Any CPU 
     {10F86A7D-5E03-41A9-8BBB-103C8E06E059}.Release|Any CPU.Build.0 = Release|Any CPU 
    EndGlobalSection 
    GlobalSection(SolutionProperties) = preSolution 
     HideSolutionNode = FALSE 
    EndGlobalSection 
    GlobalSection(ExtensibilityGlobals) = postSolution 
     SolutionGuid = {B0A27606-232E-4B58-9586-12AB68E9CAC0} 
    EndGlobalSection 
EndGlobal 
+0

嘗試從'string'之前刪除'out':'[Queue(「testitem-added」,Connection =「AzureWebJobsStorage」)] string itemAddedQueue' –

+1

您的代碼對我來說工作得很好。你參考哪個NuGet包? – Mikhail

+0

@AndrésNava-.NET - 'out'關鍵字很重要。這是排隊函數('[Queue] out string')和觸發隊列消息('[QueueTrigger] string')之間的區別 –

回答

2

我能夠通過重新安裝從NPM蔚藍的核心工具的1.x到解決此問題:NPM安裝-g Azure的功能核心工具

這迫使Visual Studio重新安裝Azure Cli工具,我最好的猜測是這是一個版本衝突的地方。

不幸的是,我不知道造成我的問題的根本原因或版本組合。

0

我已經看到它如何可以使用單個隊列作爲返回值來完成,但我有一個需要有多個輸出隊列。

我已經使用了與您相同的功能包,並在我身邊創建了測試演示。它運作良好。

enter image description here

因此,我建議你可以嘗試創建一個新的功能項目,並再次使用下面的代碼測試。

此代碼有多個輸出隊列。它會自動發送隊列到「輸出」和「輸出2」。

public static class Function1 
    { 
     [StorageAccount("AzureWebJobsStorage")] 
     [FunctionName("Function1")] 
     public static void Run([QueueTrigger("queue")]string myQueueItem,[Queue("output")] out string test2, [Queue("output2", Connection = "AzureWebJobsStorage")] out string itemAddedQueue, TraceWriter log) 
     { 
      log.Info($"C# Queue trigger function processed: {myQueueItem}"); 

      dynamic message = JObject.Parse(myQueueItem); 

      log.Info($"Received message\n{JsonConvert.SerializeObject(message, Formatting.Indented)}"); 


      var itemAddedQueueMessage = new test 
      { 
       name = "test" 
      }; 

      itemAddedQueue = JsonConvert.SerializeObject(itemAddedQueueMessage); 

      test2 = JsonConvert.SerializeObject(itemAddedQueueMessage); 

      log.Info($"Sent message to queue \"itemAddedQueue\"\n{JsonConvert.SerializeObject(itemAddedQueueMessage, Formatting.Indented)}"); 
     } 

     public class test 
     { 
      public string name { get; set; } 
     } 

    } 

結果如下:

enter image description here

+0

感謝您的反饋,看起來像我需要爲多個隊列設置。當我運行這段代碼時,我仍然遇到同樣的錯誤。 –