public static Task <IStorageBlob> GetBlobReferenceForArgumentTypeAsync(this IStorageBlobContainer container,
                                                                        string blobName, Type argumentType, CancellationToken cancellationToken)
 {
     if (argumentType == typeof(CloudBlockBlob))
     {
         IStorageBlob blob = container.GetBlockBlobReference(blobName);
         return(Task.FromResult(blob));
     }
     else if (argumentType == typeof(CloudPageBlob))
     {
         IStorageBlob blob = container.GetPageBlobReference(blobName);
         return(Task.FromResult(blob));
     }
     else if (argumentType == typeof(CloudAppendBlob))
     {
         IStorageBlob blob = container.GetAppendBlobReference(blobName);
         return(Task.FromResult(blob));
     }
     else
     {
         return(GetExistingOrNewBlockBlobReferenceAsync(container, blobName, cancellationToken));
     }
 }
        public async Task <FunctionResult> ExecuteAsync(IStorageQueueMessage value, CancellationToken cancellationToken)
        {
            BlobTriggerMessage message = JsonConvert.DeserializeObject <BlobTriggerMessage>(value.AsString, JsonSerialization.Settings);

            if (message == null)
            {
                throw new InvalidOperationException("Invalid blob trigger message.");
            }

            string functionId = message.FunctionId;

            if (functionId == null)
            {
                throw new InvalidOperationException("Invalid function ID.");
            }

            // Ensure that the function ID is still valid. Otherwise, ignore this message.
            FunctionResult        successResult = new FunctionResult(true);
            BlobQueueRegistration registration;

            if (!_registrations.TryGetValue(functionId, out registration))
            {
                return(successResult);
            }

            IStorageBlobContainer container = registration.BlobClient.GetContainerReference(message.ContainerName);
            string blobName = message.BlobName;

            IStorageBlob blob;

            switch (message.BlobType)
            {
            case StorageBlobType.PageBlob:
                blob = container.GetPageBlobReference(blobName);
                break;

            case StorageBlobType.AppendBlob:
                blob = container.GetAppendBlobReference(blobName);
                break;

            case StorageBlobType.BlockBlob:
            default:
                blob = container.GetBlockBlobReference(blobName);
                break;
            }

            // Ensure the blob still exists with the same ETag.
            string possibleETag = await _eTagReader.GetETagAsync(blob, cancellationToken);

            if (possibleETag == null)
            {
                // If the blob no longer exists, just ignore this message.
                return(successResult);
            }

            // If the blob still exists but the ETag is different, delete the message but do a fast path notification.
            if (!String.Equals(message.ETag, possibleETag, StringComparison.Ordinal))
            {
                _blobWrittenWatcher.Notify(blob);
                return(successResult);
            }

            // If the blob still exists and its ETag is still valid, execute.
            // Note: it's possible the blob could change/be deleted between now and when the function executes.
            Guid?parentId = await _causalityReader.GetWriterAsync(blob, cancellationToken);

            TriggeredFunctionData input = new TriggeredFunctionData
            {
                ParentId     = parentId,
                TriggerValue = blob
            };

            return(await registration.Executor.TryExecuteAsync(input, cancellationToken));
        }