public override async Task BindAsync(BindingContext context)
        {
            string boundBlobPath = Path;

            if (context.BindingData != null)
            {
                boundBlobPath = _pathBindingTemplate.Bind(context.BindingData);
            }

            boundBlobPath = Resolve(boundBlobPath);

            var attribute = new BlobAttribute(boundBlobPath, Access);

            Attribute[] additionalAttributes = null;
            if (!string.IsNullOrEmpty(Metadata.Connection))
            {
                additionalAttributes = new Attribute[]
                {
                    new StorageAccountAttribute(Metadata.Connection)
                };
            }

            RuntimeBindingContext runtimeContext = new RuntimeBindingContext(attribute, additionalAttributes);

            await BindStreamAsync(context, Access, runtimeContext);
        }
        public override async Task BindAsync(BindingContext context)
        {
            EasyTableAttribute attribute = new EasyTableAttribute
            {
                TableName = TableName,
                Id = Id,
                MobileAppUri = MobileAppUri,
                ApiKey = ApiKey
            };

            RuntimeBindingContext runtimeContext = new RuntimeBindingContext(attribute);

            if (Access == FileAccess.Read && _bindingDirection == BindingDirection.In)
            {
                JObject input = await context.Binder.BindAsync<JObject>(runtimeContext);
                if (input != null)
                {
                    byte[] byteArray = Encoding.UTF8.GetBytes(input.ToString());
                    using (MemoryStream stream = new MemoryStream(byteArray))
                    {
                        stream.CopyTo(context.Value);
                    }
                }
            }
            else if (Access == FileAccess.Write && _bindingDirection == BindingDirection.Out)
            {
                await BindAsyncCollectorAsync<JObject>(context.Value, context.Binder, runtimeContext);
            }
        }
        public override async Task BindAsync(BindingContext context)
        {
            string boundQueueName = QueueOrTopicName;

            if (context.BindingData != null)
            {
                boundQueueName = _queueOrTopicNameBindingTemplate.Bind(context.BindingData);
            }

            boundQueueName = Resolve(boundQueueName);

            var attribute = new ServiceBusAttribute(boundQueueName);

            Attribute[] additionalAttributes = null;
            if (!string.IsNullOrEmpty(Metadata.Connection))
            {
                additionalAttributes = new Attribute[]
                {
                    new ServiceBusAccountAttribute(Metadata.Connection)
                };
            }
            RuntimeBindingContext runtimeContext = new RuntimeBindingContext(attribute, additionalAttributes);

            await BindAsyncCollectorAsync <string>(context.Value, context.Binder, runtimeContext);
        }
示例#4
0
        public override async Task BindAsync(BindingContext context)
        {
            EasyTableAttribute attribute = new EasyTableAttribute
            {
                TableName    = TableName,
                Id           = Id,
                MobileAppUri = MobileAppUri,
                ApiKey       = ApiKey
            };

            RuntimeBindingContext runtimeContext = new RuntimeBindingContext(attribute);

            if (Access == FileAccess.Read && _bindingDirection == BindingDirection.In)
            {
                JObject input = await context.Binder.BindAsync <JObject>(runtimeContext);

                if (input != null)
                {
                    byte[] byteArray = Encoding.UTF8.GetBytes(input.ToString());
                    using (MemoryStream stream = new MemoryStream(byteArray))
                    {
                        stream.CopyTo(context.Value);
                    }
                }
            }
            else if (Access == FileAccess.Write && _bindingDirection == BindingDirection.Out)
            {
                await BindAsyncCollectorAsync <JObject>(context.Value, context.Binder, runtimeContext);
            }
        }
示例#5
0
        public override async Task BindAsync(BindingContext context)
        {
            string boundBlobPath = Path;

            if (context.BindingData != null)
            {
                boundBlobPath = _pathBindingTemplate.Bind(context.BindingData);
            }

            boundBlobPath = Resolve(boundBlobPath);

            var attribute = new BlobAttribute(boundBlobPath, Access);

            Attribute[] additionalAttributes = null;
            if (!string.IsNullOrEmpty(Metadata.Connection))
            {
                additionalAttributes = new Attribute[]
                {
                    new StorageAccountAttribute(Metadata.Connection)
                };
            }

            RuntimeBindingContext runtimeContext = new RuntimeBindingContext(attribute, additionalAttributes);
            Stream blobStream = await context.Binder.BindAsync <Stream>(runtimeContext);

            if (Access == FileAccess.Write)
            {
                await context.Value.CopyToAsync(blobStream);
            }
            else
            {
                await blobStream.CopyToAsync(context.Value);
            }
        }
示例#6
0
        public override async Task BindAsync(BindingContext context)
        {
            string boundBlobPath = Path;

            if (context.BindingData != null)
            {
                boundBlobPath = _pathBindingTemplate.Bind(context.BindingData);
            }

            boundBlobPath = Resolve(boundBlobPath);

            var attribute = new ApiHubFileAttribute(Key, boundBlobPath, Access);

            RuntimeBindingContext runtimeContext = new RuntimeBindingContext(attribute);
            Stream blobStream = await context.Binder.BindAsync <Stream>(runtimeContext);

            if (Access == FileAccess.Write)
            {
                await context.Value.CopyToAsync(blobStream);
            }
            else
            {
                await blobStream.CopyToAsync(context.Value);
            }
        }
        public override async Task BindAsync(BindingContext context)
        {
            DocumentDBAttribute attribute = new DocumentDBAttribute(DatabaseName, CollectionName)
            {
                CreateIfNotExists = CreateIfNotExists,
                ConnectionString  = ConnectionString,
                Id = Id
            };
            RuntimeBindingContext runtimeContext = new RuntimeBindingContext(attribute);

            if (Access == FileAccess.Read && _bindingDirection == BindingDirection.In)
            {
                JObject input = await context.Binder.BindAsync <JObject>(runtimeContext);

                if (input != null)
                {
                    byte[] byteArray = Encoding.UTF8.GetBytes(input.ToString());
                    using (MemoryStream stream = new MemoryStream(byteArray))
                    {
                        stream.CopyTo(context.Value);
                    }
                }
            }
            else if (Access == FileAccess.Write && _bindingDirection == BindingDirection.Out)
            {
                await BindAsyncCollectorAsync <JObject>(context.Value, context.Binder, runtimeContext);
            }
        }
        public override async Task BindAsync(BindingContext context)
        {
            string boundBlobPath = Path;
            if (context.BindingData != null)
            {
                boundBlobPath = _pathBindingTemplate.Bind(context.BindingData);
            }

            boundBlobPath = Resolve(boundBlobPath);

            var attribute = new ApiHubFileAttribute(Key, boundBlobPath, Access);

            RuntimeBindingContext runtimeContext = new RuntimeBindingContext(attribute);
            await BindStreamAsync(context.Value, Access, context.Binder, runtimeContext);
        }
        public override async Task BindAsync(BindingContext context)
        {
            string eventHubName = this.EventHubName;
            if (context.BindingData != null)
            {
                eventHubName = _eventHubNameBindingTemplate.Bind(context.BindingData);
            }

            eventHubName = Resolve(eventHubName);

            var attribute = new ServiceBus.EventHubAttribute(eventHubName);
            RuntimeBindingContext runtimeContext = new RuntimeBindingContext(attribute);

            await BindAsyncCollectorAsync<string>(context.Value, context.Binder, runtimeContext);
        }
示例#10
0
        public async Task BindAsyncCollectorAsync_JObjectCollection()
        {
            JArray values = new JArray();

            for (int i = 1; i <= 3; i++)
            {
                JObject jsonObject = new JObject
                {
                    { "prop1", "value1" },
                    { "prop2", true },
                    { "prop3", 123 }
                };
                values.Add(jsonObject);
            }

            string json          = values.ToString();
            var    results       = new JArray();
            var    collectorMock = new Mock <IAsyncCollector <JObject> >(MockBehavior.Strict);

            collectorMock.Setup(p => p.AddAsync(It.IsAny <JObject>(), CancellationToken.None))
            .Callback <JObject, CancellationToken>((mockObject, mockToken) =>
            {
                results.Add(mockObject);
            }).Returns(Task.CompletedTask);

            var                   binderMock            = new Mock <IBinderEx>(MockBehavior.Strict);
            QueueAttribute        attribute             = new QueueAttribute("test");
            RuntimeBindingContext runtimeBindingContext = new RuntimeBindingContext(attribute);

            binderMock.Setup(p => p.BindAsync <IAsyncCollector <JObject> >(runtimeBindingContext, CancellationToken.None)).ReturnsAsync(collectorMock.Object);

            BindingContext bindingContext = new BindingContext
            {
                Binder = binderMock.Object,
                Value  = json
            };

            await FunctionBinding.BindAsyncCollectorAsync <JObject>(bindingContext, runtimeBindingContext);

            Assert.Equal(3, results.Count);
            for (int i = 0; i < 3; i++)
            {
                JObject jsonObject = (JObject)results[i];
                Assert.Equal("value1", (string)jsonObject["prop1"]);
                Assert.Equal(true, (bool)jsonObject["prop2"]);
                Assert.Equal(123, (int)jsonObject["prop3"]);
            }
        }
        public override async Task BindAsync(BindingContext context)
        {
            // Only output bindings are supported.
            if (Access == FileAccess.Write && _bindingDirection == BindingDirection.Out)
            {
                NotificationHubAttribute attribute = new NotificationHubAttribute
                {
                    TagExpression = TagExpression,
                    ConnectionString = ConnectionString,
                    HubName = HubName
                };

                RuntimeBindingContext runtimeContext = new RuntimeBindingContext(attribute);
                await BindAsyncCollectorAsync<string>(context.Value, context.Binder, runtimeContext);
            }
        }
        public override async Task BindAsync(BindingContext context)
        {
            // Only output bindings are supported.
            if (Access == FileAccess.Write && _bindingDirection == BindingDirection.Out)
            {
                NotificationHubAttribute attribute = new NotificationHubAttribute
                {
                    TagExpression    = TagExpression,
                    ConnectionString = ConnectionString,
                    HubName          = HubName
                };

                RuntimeBindingContext runtimeContext = new RuntimeBindingContext(attribute);
                await BindAsyncCollectorAsync <string>(context.Value, context.Binder, runtimeContext);
            }
        }
示例#13
0
        public override async Task BindAsync(BindingContext context)
        {
            string eventHubName = this.EventHubName;

            if (context.BindingData != null)
            {
                eventHubName = _eventHubNameBindingTemplate.Bind(context.BindingData);
            }

            eventHubName = Resolve(eventHubName);

            var attribute = new ServiceBus.EventHubAttribute(eventHubName);
            RuntimeBindingContext runtimeContext = new RuntimeBindingContext(attribute);

            await BindAsyncCollectorAsync <byte[]>(context, runtimeContext);
        }
        public override async Task BindAsync(BindingContext context)
        {
            string boundBlobPath = Path;

            if (context.BindingData != null)
            {
                boundBlobPath = _pathBindingTemplate.Bind(context.BindingData);
            }

            boundBlobPath = Resolve(boundBlobPath);

            var attribute = new ApiHubFileAttribute(Key, boundBlobPath, Access);

            RuntimeBindingContext runtimeContext = new RuntimeBindingContext(attribute);

            await BindStreamAsync(context, Access, runtimeContext);
        }
        public async Task BindAsyncCollectorAsync_JObjectCollection()
        {
            JArray values = new JArray();
            for (int i = 1; i <= 3; i++)
            {
                JObject jsonObject = new JObject
                {
                    { "prop1", "value1" },
                    { "prop2", true },
                    { "prop3", 123 }
                };
                values.Add(jsonObject);
            }

            string json = values.ToString();
            byte[] bytes = Encoding.UTF8.GetBytes(json);
            MemoryStream ms = new MemoryStream(bytes);

            var results = new JArray();
            var collectorMock = new Mock<IAsyncCollector<JObject>>(MockBehavior.Strict);
            collectorMock.Setup(p => p.AddAsync(It.IsAny<JObject>(), CancellationToken.None))
                .Callback<JObject, CancellationToken>((mockObject, mockToken) =>
                {
                    results.Add(mockObject);
                }).Returns(Task.CompletedTask);

            var binderMock = new Mock<IBinderEx>(MockBehavior.Strict);
            QueueAttribute attribute = new QueueAttribute("test");
            RuntimeBindingContext context = new RuntimeBindingContext(attribute);
            binderMock.Setup(p => p.BindAsync<IAsyncCollector<JObject>>(context, CancellationToken.None)).ReturnsAsync(collectorMock.Object);

            await FunctionBinding.BindAsyncCollectorAsync<JObject>(ms, binderMock.Object, context);

            Assert.Equal(3, results.Count);
            for (int i = 0; i < 3; i++)
            {
                JObject jsonObject = (JObject)results[i];
                Assert.Equal("value1", (string)jsonObject["prop1"]);
                Assert.Equal(true, (bool)jsonObject["prop2"]);
                Assert.Equal(123, (int)jsonObject["prop3"]);
            }
        }
        public override async Task BindAsync(BindingContext context)
        {
            var attribute = new ApiHubTableAttribute(Connection)
            {
                DataSetName = BindAndResolve(DataSetName, _dataSetNameBindingTemplate, context),
                TableName   = BindAndResolve(TableName, _tableNameBindingTemplate, context),
                EntityId    = BindAndResolve(EntityId, _entityIdBindingTemplate, context)
            };

            var runtimeContext = new RuntimeBindingContext(attribute);

            if (Access == FileAccess.Read && BindingDirection == BindingDirection.In)
            {
                context.Value = await context.Binder.BindAsync <JObject>(runtimeContext);
            }
            else if (Access == FileAccess.Write && BindingDirection == BindingDirection.Out)
            {
                await BindAsyncCollectorAsync <JObject>(context, runtimeContext);
            }
        }
        public override async Task BindAsync(BindingContext context)
        {
            DocumentDBAttribute attribute = new DocumentDBAttribute(DatabaseName, CollectionName)
            {
                CreateIfNotExists = CreateIfNotExists,
                ConnectionString  = ConnectionString,
                Id                   = Id,
                PartitionKey         = PartitionKey,
                CollectionThroughput = CollectionThroughput
            };
            RuntimeBindingContext runtimeContext = new RuntimeBindingContext(attribute);

            if (Access == FileAccess.Read && _bindingDirection == BindingDirection.In)
            {
                context.Value = await context.Binder.BindAsync <JObject>(runtimeContext);
            }
            else if (Access == FileAccess.Write && _bindingDirection == BindingDirection.Out)
            {
                await BindAsyncCollectorAsync <JObject>(context, runtimeContext);
            }
        }
        public override async Task BindAsync(BindingContext context)
        {
            MobileTableAttribute attribute = new MobileTableAttribute
            {
                TableName    = TableName,
                Id           = Id,
                MobileAppUri = MobileAppUri,
                ApiKey       = ApiKey
            };

            RuntimeBindingContext runtimeContext = new RuntimeBindingContext(attribute);

            if (Access == FileAccess.Read && _bindingDirection == BindingDirection.In)
            {
                context.Value = await context.Binder.BindAsync <JObject>(runtimeContext);
            }
            else if (Access == FileAccess.Write && _bindingDirection == BindingDirection.Out)
            {
                await BindAsyncCollectorAsync <JObject>(context, runtimeContext);
            }
        }
        public override async Task BindAsync(BindingContext context)
        {
            string boundBlobPath = Path;
            if (context.BindingData != null)
            {
                boundBlobPath = _pathBindingTemplate.Bind(context.BindingData);
            }

            boundBlobPath = Resolve(boundBlobPath);

            var attribute = new BlobAttribute(boundBlobPath, Access);
            Attribute[] additionalAttributes = null;
            if (!string.IsNullOrEmpty(Metadata.Connection))
            {
                additionalAttributes = new Attribute[]
                {
                    new StorageAccountAttribute(Metadata.Connection)
                };
            }

            RuntimeBindingContext runtimeContext = new RuntimeBindingContext(attribute, additionalAttributes);
            await BindStreamAsync(context.Value, Access, context.Binder, runtimeContext);
        }
        public override async Task BindAsync(BindingContext context)
        {
            string boundQueueName = QueueOrTopicName;
            if (context.BindingData != null)
            {
                boundQueueName = _queueOrTopicNameBindingTemplate.Bind(context.BindingData);
            }

            boundQueueName = Resolve(boundQueueName);

            var attribute = new ServiceBusAttribute(boundQueueName);
            Attribute[] additionalAttributes = null;
            if (!string.IsNullOrEmpty(Metadata.Connection))
            {
                additionalAttributes = new Attribute[]
                {
                    new ServiceBusAccountAttribute(Metadata.Connection)
                };
            }
            RuntimeBindingContext runtimeContext = new RuntimeBindingContext(attribute, additionalAttributes);

            await BindAsyncCollectorAsync<string>(context.Value, context.Binder, runtimeContext);
        }
        public override async Task BindAsync(BindingContext context)
        {
            string boundPartitionKey = PartitionKey;
            string boundRowKey = RowKey;
            if (context.BindingData != null)
            {
                if (_partitionKeyBindingTemplate != null)
                {
                    boundPartitionKey = _partitionKeyBindingTemplate.Bind(context.BindingData);
                }
                
                if (_rowKeyBindingTemplate != null)
                {
                    boundRowKey = _rowKeyBindingTemplate.Bind(context.BindingData);
                }
            }

            if (!string.IsNullOrEmpty(boundPartitionKey))
            {
                boundPartitionKey = Resolve(boundPartitionKey);
            }

            if (!string.IsNullOrEmpty(boundRowKey))
            {
                boundRowKey = Resolve(boundRowKey);
            }

            Attribute[] additionalAttributes = null;
            if (!string.IsNullOrEmpty(Metadata.Connection))
            {
                additionalAttributes = new Attribute[]
                {
                    new StorageAccountAttribute(Metadata.Connection)
                };
            }

            if (Access == FileAccess.Write)
            {
                RuntimeBindingContext runtimeContext = new RuntimeBindingContext(new TableAttribute(TableName), additionalAttributes);
                IAsyncCollector<DynamicTableEntity> collector = await context.Binder.BindAsync<IAsyncCollector<DynamicTableEntity>>(runtimeContext);
                ICollection<JToken> entities = ReadAsCollection(context.Value);

                foreach (JObject entity in entities)
                {
                    // any key values specified on the entity override any values
                    // specified in the binding
                    string keyValue = (string)entity["partitionKey"];
                    if (!string.IsNullOrEmpty(keyValue))
                    {
                        boundPartitionKey = Resolve(keyValue);
                        entity.Remove("partitionKey");
                    }

                    keyValue = (string)entity["rowKey"];
                    if (!string.IsNullOrEmpty(keyValue))
                    {
                        boundRowKey = Resolve(keyValue);
                        entity.Remove("rowKey");
                    }

                    DynamicTableEntity tableEntity = new DynamicTableEntity(boundPartitionKey, boundRowKey);
                    foreach (JProperty property in entity.Properties())
                    {
                        EntityProperty entityProperty = EntityProperty.CreateEntityPropertyFromObject((object)property.Value);
                        tableEntity.Properties.Add(property.Name, entityProperty);
                    }

                    await collector.AddAsync(tableEntity);
                }
            }
            else
            {
                string json = null;

                if (!string.IsNullOrEmpty(boundPartitionKey) &&
                    !string.IsNullOrEmpty(boundRowKey))
                {
                    // singleton
                    RuntimeBindingContext runtimeContext = new RuntimeBindingContext(new TableAttribute(TableName, boundPartitionKey, boundRowKey), additionalAttributes);
                    DynamicTableEntity tableEntity = await context.Binder.BindAsync<DynamicTableEntity>(runtimeContext);
                    if (tableEntity != null)
                    {
                        json = ConvertEntityToJObject(tableEntity).ToString();
                    }
                }
                else
                {
                    // binding to entire table (query multiple table entities)
                    RuntimeBindingContext runtimeContext = new RuntimeBindingContext(new TableAttribute(TableName, boundPartitionKey, boundRowKey), additionalAttributes);
                    CloudTable table = await context.Binder.BindAsync<CloudTable>(runtimeContext);
                    var entities = table.ExecuteQuery(_tableQuery);

                    JArray entityArray = new JArray();
                    foreach (var entity in entities)
                    {
                        entityArray.Add(ConvertEntityToJObject(entity));
                    }

                    json = entityArray.ToString(Formatting.None);
                }

                if (json != null)
                {
                    // We're explicitly NOT disposing the StreamWriter because
                    // we don't want to close the underlying Stream
                    StreamWriter sw = new StreamWriter(context.Value);
                    await sw.WriteAsync(json);
                    sw.Flush();
                }
            }
        }
        public override async Task BindAsync(BindingContext context)
        {
            DocumentDBAttribute attribute = new DocumentDBAttribute(DatabaseName, CollectionName)
            {
                CreateIfNotExists = CreateIfNotExists,
                ConnectionString = ConnectionString,
                Id = Id
            };
            RuntimeBindingContext runtimeContext = new RuntimeBindingContext(attribute);

            if (Access == FileAccess.Read && _bindingDirection == BindingDirection.In)
            {
                JObject input = await context.Binder.BindAsync<JObject>(runtimeContext);
                if (input != null)
                {
                    byte[] byteArray = Encoding.UTF8.GetBytes(input.ToString());
                    using (MemoryStream stream = new MemoryStream(byteArray))
                    {
                        stream.CopyTo(context.Value);
                    }
                }
            }
            else if (Access == FileAccess.Write && _bindingDirection == BindingDirection.Out)
            {
                await BindAsyncCollectorAsync<JObject>(context.Value, context.Binder, runtimeContext);
            }
        }
        public override async Task BindAsync(BindingContext context)
        {
            string boundPartitionKey = PartitionKey;
            string boundRowKey       = RowKey;
            string boundFilter       = Filter;

            if (context.BindingData != null)
            {
                if (_partitionKeyBindingTemplate != null)
                {
                    boundPartitionKey = _partitionKeyBindingTemplate.Bind(context.BindingData);
                }

                if (_rowKeyBindingTemplate != null)
                {
                    boundRowKey = _rowKeyBindingTemplate.Bind(context.BindingData);
                }

                if (_filterBindingTemplate != null)
                {
                    boundFilter = _filterBindingTemplate.Bind(context.BindingData);
                }
            }

            if (!string.IsNullOrEmpty(boundPartitionKey))
            {
                boundPartitionKey = Resolve(boundPartitionKey);
            }

            if (!string.IsNullOrEmpty(boundRowKey))
            {
                boundRowKey = Resolve(boundRowKey);
            }

            if (!string.IsNullOrEmpty(boundFilter))
            {
                boundFilter = Resolve(boundFilter);
            }

            Attribute[] additionalAttributes = null;
            if (!string.IsNullOrEmpty(Metadata.Connection))
            {
                additionalAttributes = new Attribute[]
                {
                    new StorageAccountAttribute(Metadata.Connection)
                };
            }

            if (Access == FileAccess.Write)
            {
                RuntimeBindingContext runtimeContext           = new RuntimeBindingContext(new TableAttribute(TableName), additionalAttributes);
                IAsyncCollector <DynamicTableEntity> collector = await context.Binder.BindAsync <IAsyncCollector <DynamicTableEntity> >(runtimeContext);

                ICollection entities = ReadAsCollection(context.Value);

                foreach (JObject entity in entities)
                {
                    // Here we're mapping from JObject to DynamicTableEntity because the Table binding doesn't support
                    // a JObject binding. We enable that for the core Table binding in the future, which would allow
                    // this code to go away.
                    DynamicTableEntity tableEntity = CreateTableEntityFromJObject(boundPartitionKey, boundRowKey, entity);
                    await collector.AddAsync(tableEntity);
                }
            }
            else
            {
                string json = null;
                if (!string.IsNullOrEmpty(boundPartitionKey) &&
                    !string.IsNullOrEmpty(boundRowKey))
                {
                    // singleton
                    RuntimeBindingContext runtimeContext = new RuntimeBindingContext(new TableAttribute(TableName, boundPartitionKey, boundRowKey), additionalAttributes);
                    DynamicTableEntity    tableEntity    = await context.Binder.BindAsync <DynamicTableEntity>(runtimeContext);

                    if (tableEntity != null)
                    {
                        json = ConvertEntityToJObject(tableEntity).ToString();
                    }
                }
                else
                {
                    // binding to multiple table entities
                    RuntimeBindingContext runtimeContext = new RuntimeBindingContext(new TableAttribute(TableName), additionalAttributes);
                    CloudTable            table          = await context.Binder.BindAsync <CloudTable>(runtimeContext);

                    string finalQuery = boundFilter;
                    if (!string.IsNullOrEmpty(boundPartitionKey))
                    {
                        var partitionKeyPredicate = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, boundPartitionKey);
                        if (!string.IsNullOrEmpty(boundFilter))
                        {
                            finalQuery = TableQuery.CombineFilters(boundFilter, TableOperators.And, partitionKeyPredicate);
                        }
                        else
                        {
                            finalQuery = partitionKeyPredicate;
                        }
                    }

                    TableQuery tableQuery = new TableQuery
                    {
                        TakeCount    = Take,
                        FilterString = finalQuery
                    };

                    var    entities    = table.ExecuteQuery(tableQuery);
                    JArray entityArray = new JArray();
                    foreach (var entity in entities)
                    {
                        entityArray.Add(ConvertEntityToJObject(entity));
                    }

                    json = entityArray.ToString(Formatting.None);
                }

                if (json != null)
                {
                    if (context.DataType == DataType.Stream)
                    {
                        // We're explicitly NOT disposing the StreamWriter because
                        // we don't want to close the underlying Stream
                        StreamWriter sw = new StreamWriter((Stream)context.Value);
                        await sw.WriteAsync(json);

                        sw.Flush();
                    }
                    else
                    {
                        context.Value = json;
                    }
                }
            }
        }
示例#24
0
        public override async Task BindAsync(BindingContext context)
        {
            string boundPartitionKey = PartitionKey;
            string boundRowKey       = RowKey;

            if (context.BindingData != null)
            {
                if (_partitionKeyBindingTemplate != null)
                {
                    boundPartitionKey = _partitionKeyBindingTemplate.Bind(context.BindingData);
                }

                if (_rowKeyBindingTemplate != null)
                {
                    boundRowKey = _rowKeyBindingTemplate.Bind(context.BindingData);
                }
            }

            if (!string.IsNullOrEmpty(boundPartitionKey))
            {
                boundPartitionKey = Resolve(boundPartitionKey);
            }

            if (!string.IsNullOrEmpty(boundRowKey))
            {
                boundRowKey = Resolve(boundRowKey);
            }

            Attribute[] additionalAttributes = null;
            if (!string.IsNullOrEmpty(Metadata.Connection))
            {
                additionalAttributes = new Attribute[]
                {
                    new StorageAccountAttribute(Metadata.Connection)
                };
            }

            if (Access == FileAccess.Write)
            {
                RuntimeBindingContext runtimeContext           = new RuntimeBindingContext(new TableAttribute(TableName), additionalAttributes);
                IAsyncCollector <DynamicTableEntity> collector = await context.Binder.BindAsync <IAsyncCollector <DynamicTableEntity> >(runtimeContext);

                ICollection <JToken> entities = ReadAsCollection(context.Value);

                foreach (JObject entity in entities)
                {
                    // any key values specified on the entity override any values
                    // specified in the binding
                    string keyValue = (string)entity["partitionKey"];
                    if (!string.IsNullOrEmpty(keyValue))
                    {
                        boundPartitionKey = Resolve(keyValue);
                        entity.Remove("partitionKey");
                    }

                    keyValue = (string)entity["rowKey"];
                    if (!string.IsNullOrEmpty(keyValue))
                    {
                        boundRowKey = Resolve(keyValue);
                        entity.Remove("rowKey");
                    }

                    DynamicTableEntity tableEntity = new DynamicTableEntity(boundPartitionKey, boundRowKey);
                    foreach (JProperty property in entity.Properties())
                    {
                        EntityProperty entityProperty = EntityProperty.CreateEntityPropertyFromObject((object)property.Value);
                        tableEntity.Properties.Add(property.Name, entityProperty);
                    }

                    await collector.AddAsync(tableEntity);
                }
            }
            else
            {
                string json = null;

                if (!string.IsNullOrEmpty(boundPartitionKey) &&
                    !string.IsNullOrEmpty(boundRowKey))
                {
                    // singleton
                    RuntimeBindingContext runtimeContext = new RuntimeBindingContext(new TableAttribute(TableName, boundPartitionKey, boundRowKey), additionalAttributes);
                    DynamicTableEntity    tableEntity    = await context.Binder.BindAsync <DynamicTableEntity>(runtimeContext);

                    if (tableEntity != null)
                    {
                        json = ConvertEntityToJObject(tableEntity).ToString();
                    }
                }
                else
                {
                    // binding to entire table (query multiple table entities)
                    RuntimeBindingContext runtimeContext = new RuntimeBindingContext(new TableAttribute(TableName, boundPartitionKey, boundRowKey), additionalAttributes);
                    CloudTable            table          = await context.Binder.BindAsync <CloudTable>(runtimeContext);

                    var entities = table.ExecuteQuery(_tableQuery);

                    JArray entityArray = new JArray();
                    foreach (var entity in entities)
                    {
                        entityArray.Add(ConvertEntityToJObject(entity));
                    }

                    json = entityArray.ToString(Formatting.None);
                }

                if (json != null)
                {
                    // We're explicitly NOT disposing the StreamWriter because
                    // we don't want to close the underlying Stream
                    StreamWriter sw = new StreamWriter(context.Value);
                    await sw.WriteAsync(json);

                    sw.Flush();
                }
            }
        }
        internal static async Task BindAsyncCollectorAsync <T>(BindingContext context, RuntimeBindingContext runtimeContext)
        {
            IAsyncCollector <T> collector = await context.Binder.BindAsync <IAsyncCollector <T> >(runtimeContext);

            IEnumerable values = ReadAsCollection(context.Value);

            // convert values as necessary and add to the collector
            foreach (var value in values)
            {
                object converted = null;
                if (typeof(T) == typeof(string))
                {
                    converted = value.ToString();
                }
                else if (typeof(T) == typeof(JObject))
                {
                    converted = (JObject)value;
                }
                else if (typeof(T) == typeof(byte[]))
                {
                    byte[] bytes = value as byte[];
                    if (bytes == null)
                    {
                        string stringValue = value.ToString();
                        bytes = Encoding.UTF8.GetBytes(stringValue);
                    }
                    converted = bytes;
                }
                else
                {
                    throw new ArgumentException("Unsupported collection type.");
                }

                await collector.AddAsync((T)converted);
            }
        }
        internal static async Task BindStreamAsync(BindingContext context, FileAccess access, RuntimeBindingContext runtimeContext)
        {
            Stream stream = await context.Binder.BindAsync <Stream>(runtimeContext);

            if (access == FileAccess.Write)
            {
                ConvertValueToStream(context.Value, stream);
            }
            else
            {
                // Read the value into the context Value converting based on data type
                object converted = null;
                ConvertStreamToValue(stream, context.DataType, ref converted);
                context.Value = converted;
            }
        }
示例#27
0
        internal static async Task BindStreamAsync(Stream stream, FileAccess access, IBinderEx binder, RuntimeBindingContext runtimeContext)
        {
            Stream boundStream = await binder.BindAsync <Stream>(runtimeContext);

            if (access == FileAccess.Write)
            {
                await stream.CopyToAsync(boundStream);
            }
            else
            {
                await boundStream.CopyToAsync(stream);
            }
        }
示例#28
0
        internal static async Task BindAsyncCollectorAsync <T>(Stream stream, IBinderEx binder, RuntimeBindingContext runtimeContext)
        {
            IAsyncCollector <T> collector = await binder.BindAsync <IAsyncCollector <T> >(runtimeContext);

            // first read the input stream as a collection
            ICollection <JToken> values = ReadAsCollection(stream);

            // convert values as necessary and add to the collector
            foreach (var value in values)
            {
                object converted = null;
                if (typeof(T) == typeof(string))
                {
                    converted = value.ToString();
                }
                else if (typeof(T) == typeof(JObject))
                {
                    converted = (JObject)value;
                }
                else
                {
                    throw new ArgumentException("Unsupported collection type.");
                }

                await collector.AddAsync((T)converted);
            }
        }