如何使用输出绑定而不必分配值?

时间:2019-04-01 18:29:53

标签: c# .net azure-functions

如何指定输出绑定,而不必强制为其指定值?

我能够成功使用以下属性绑定到2个输出Blob位置:

        [Blob("processed/{CorrelationId}", FileAccess.Write, Connection = "OnSchedulingToMMMQueueTriggered:ProcessedPayloadsConnectionString")] out string processedPayload,
        [Blob("success/{CorrelationId}", FileAccess.Write, Connection = "OnSchedulingToMMMQueueTriggered:ProcessedPayloadsConnectionString")] out string success,

但是,由于我没有为该值out string success分配任何内容,因此出现以下异常:

enter image description here

如何指定输出绑定,而不必强制为其指定值? -在某些情况下,我不希望分配任何值,因为我根本不想写入该Blob。

我的完整功能如下:

public static class OnSchedulingToMMMQueueTriggered
{
    [FunctionName("OnSchedulingToMMMQueueTriggered")]
    public static void Run(
        [QueueTrigger("httpqueue", Connection = "OnSchedulingToMMMQueueTriggered:SourceQueueConnection")] Payload myQueueItem,
        [Blob("processed/{CorrelationId}", FileAccess.Write, Connection = "OnSchedulingToMMMQueueTriggered:ProcessedPayloadsConnectionString")] out string processedPayload,
        [Blob("success/{CorrelationId}", FileAccess.Write, Connection = "OnSchedulingToMMMQueueTriggered:ProcessedPayloadsConnectionString")] out string success,
        ILogger log)
    {
        log.LogInformation($"C# Queue trigger function processed: {myQueueItem.Body}");

        processedPayload = "this shoudl be the body of the string";
    }
}

1 个答案:

答案 0 :(得分:1)

如果使用特定的“ out”参数,则将需要它。如果输出是有条件的,请考虑只创建一个BlobContainer绑定,仅在需要时使用它。这将需要您使用GetBlockBlobReference自己创建Blob,但这只是解决问题的多余代码。我还没有机会在下面测试此代码,因此您可能需要对其进行一些微调。

public static class OnSchedulingToMMMQueueTriggered
{
    [FunctionName("OnSchedulingToMMMQueueTriggered")]
    public static void Run(
        [QueueTrigger("httpqueue", Connection = "OnSchedulingToMMMQueueTriggered:SourceQueueConnection")] Payload myQueueItem,
        [Blob("processed/{CorrelationId}", FileAccess.Write, Connection = "OnSchedulingToMMMQueueTriggered:ProcessedPayloadsConnectionString")] out string processedPayload,
        [Blob("success", FileAccess.Write, Connection = "OnSchedulingToMMMQueueTriggered:ProcessedPayloadsConnectionString"))] CloudBlobContainer outputSuccessContainer,
        ILogger log)
    {
        log.LogInformation($"C# Queue trigger function processed: {myQueueItem.Body}");

        processedPayload = "this shoudl be the body of the string";

        if (outputNeeded) {
            var blockBlob = outputSuccessContainer.GetBlockBlobReference(CorrelationId + ".txt");
            await blockBlob.UploadText(processedPayload);
            blockBlob.Properties.ContentType = "text/plain";
            blockBlob.SetProperties();
        }
    }
}