如何为动态绑定到Blob指定多个属性?

时间:2019-04-01 19:38:29

标签: c# .net azure-functions

我在做什么错?您如何指定多个属性?

我试图像这样动态地注册到输出Blob的绑定:

        var attributes = new Attribute[]
        {
            new BlobAttribute("success/{CorrelationId}"),
            new StorageAccountAttribute("MyStorageAccount")
        };
        using (var writer = await binder.BindAsync<TextWriter>(attributes))
        {
            writer.Write(JsonConvert.SerializeObject(myQueueItem.Body));
        }

我遇到以下异常:

enter image description here

这是完整的代码:

public static class OnSchedulingToMMMQueueTriggered
    {
        [FunctionName("OnSchedulingToMMMQueueTriggered")]
        public static async System.Threading.Tasks.Task RunAsync(
            [QueueTrigger("httpqueue", Connection = "OnSchedulingToMMMQueueTriggered:SourceQueueConnection")] Payload myQueueItem,
            [Blob("processed/{CorrelationId}", FileAccess.Write, Connection = "OnSchedulingToMMMQueueTriggered:ProcessedPayloadsConnectionString")]  Stream processedPayload,
            IBinder binder,
            ILogger log)
        {
            log.LogInformation($"C# Queue trigger function processed: {myQueueItem.Body}");

            var attributes = new Attribute[]
            {
                new BlobAttribute("success/{CorrelationId}"),
                new StorageAccountAttribute("MyStorageAccount")
            };
            using (var writer = await binder.BindAsync<TextWriter>(attributes))
            {
                writer.Write(JsonConvert.SerializeObject(myQueueItem.Body));
            }

        }
    }

我在做什么错?您如何指定多个属性?

1 个答案:

答案 0 :(得分:1)

文档确实说如果要使用多个,则应使用Binder类而不是IBinder接口。

  

使用Binder参数,而不使用IBinder

例如

public static class OnSchedulingToMMMQueueTriggered {
    [FunctionName("OnSchedulingToMMMQueueTriggered")]
    public static async Task RunAsync(
        [QueueTrigger("httpqueue", Connection = "OnSchedulingToMMMQueueTriggered:SourceQueueConnection")] Payload myQueueItem,
        [Blob("processed/{CorrelationId}", FileAccess.Write, Connection = "OnSchedulingToMMMQueueTriggered:ProcessedPayloadsConnectionString")]  Stream processedPayload,
        Binder binder, //<--NOTE *Binder* not *IBinder*
        ILogger log) {

        log.LogInformation($"C# Queue trigger function processed: {myQueueItem.Body}");

        var attributes = new Attribute[] {
            new BlobAttribute("success/{CorrelationId}"),
            new StorageAccountAttribute("MyStorageAccount")
        };
        using (var writer = await binder.BindAsync<TextWriter>(attributes)) {
            writer.Write(JsonConvert.SerializeObject(myQueueItem.Body));
        }
    }
}

引用Multiple attribute example