代码之家  ›  专栏  ›  技术社区  ›  jinwood

Azure函数-ICollector绑定不在结果函数中。json

  •  3
  • jinwood  · 技术社区  · 8 年前

    我有以下C#函数代码:

    [FunctionName("UpdateCohortsByTenantFunction")]
    [return: Queue("my-queue", Connection = "MyStorage")]
    //note - I have tried both method decoration and parameter decoration
    public static async Task Run([TimerTrigger("* * * * * *")]TimerInfo myTimer, IAsyncCollector<AudienceMessage> output)
    {
        //some logic
        foreach (var audience in audiences)
        {
            await output.AddAsync(new AudienceMessage
            {
                AudienceId = audience.Id,
                TenantId = tenant.Id
            });
        }
    }
    

    生成以下函数。json:

    {
       "generatedBy": "Microsoft.NET.Sdk.Functions.Generator-1.0.6",
       "configurationSource": "attributes",
       "bindings": [
       {
           "type": "timerTrigger",
           "schedule": "* * * * * *",
           "useMonitor": true,
           "runOnStartup": false,
           "name": "myTimer"
       }
      ],
      "disabled": false,
      "scriptFile": "../bin/MyApp.App.Tasks.Functions.dll",
      "entryPoint": "MyApp.App.Tasks.Functions.UpdateCohortsByTenantFunction.Run"
    }
    

    根据文件 here

    {
      "type": "queue",
      "direction": "out",
      "name": "$return",
      "queueName": "outqueue",
      "connection": "MyStorageConnectionAppSetting",
    }
    

    当我试图通过npm工具运行队列时(配置描述 here ),我得到以下错误:

    运行:Microsoft。蔚蓝色的网络作业。主机:索引方法“UpdateCoortsbytentFunction”时出错。“运行”。微软蔚蓝色的网络作业。主机:无法将参数“output”绑定到类型IAsyncCollector“1”。确保绑定支持参数类型。如果您使用的是绑定扩展(例如ServiceBus、计时器等),请确保在启动代码(例如config.UseServiceBus()、config)中调用了扩展的注册方法。UseTimers()等)。

    该文档不包含通过启动代码绑定的引用。我的理解是,这是通过上面链接的Microsoft文档和示例代码中描述的属性来实现的,但错误消息表明情况并非如此。

    1 回复  |  直到 8 年前
        1
  •  7
  •   Mikhail Shilkov    8 年前
    1. 您应该使用属性而不是返回值来装饰参数:

      public static async Task Run(
          [TimerTrigger("* * * * * *")]TimerInfo myTimer,
          [Queue("my-queue", Connection = "MyStg")] IAsyncCollector<AudienceMessage> output)
      
    2. 中没有输出绑定 function.json 作用json . 不用担心,它们仍然可以工作。