public async Task TableEntity_IfBoundToPocoTableEntity_CanCall()
        {
            // Arrange
            await TableClient.AddEntityAsync(new TableEntity(PartitionKey, RowKey)
            {
                { "Fruit", ("Banana") },
                { "Duration", ("\"00:00:01\"") },
                { "Value", ("Foo") }
            });

            // Act
            await CallAsync <BindTableEntityToPocoTableEntityProgram>();

            // Assert
            TableEntity entity = await TableClient.GetEntityAsync <TableEntity>(PartitionKey, RowKey);

            Assert.NotNull(entity);
            Assert.AreEqual(PartitionKey, entity.PartitionKey); // Guard
            Assert.AreEqual(RowKey, entity.RowKey);             // Guard

            // TODO: behavior change. Was 3 before
            Assert.AreEqual(7, entity.Count);
            Assert.AreEqual("Pear", entity["Fruit"]);
            Assert.AreEqual("\"00:02:00\"", entity["Duration"]);
            Assert.AreEqual("Bar", entity["Value"]);
        }
        public async Task TableEntity_IfBoundToJArray_CanCall()
        {
            await TableClient.AddEntityAsync(CreateTableEntity(PartitionKey, RowKey + "1", "Value", "x1"));

            await TableClient.AddEntityAsync(CreateTableEntity(PartitionKey, RowKey + "2", "Value", "x2"));

            await TableClient.AddEntityAsync(CreateTableEntity(PartitionKey, RowKey + "3", "Value", "x3"));

            await TableClient.AddEntityAsync(CreateTableEntity(PartitionKey, RowKey + "4", "Value", "x4"));

            // Act
            var result1 = await CallAsync <BindTableEntityToJArrayProgram>(nameof(BindTableEntityToJArrayProgram.CallTakeFilter));

            Assert.AreEqual("x1;x3;", result1.Result);

            var result2 = await CallAsync <BindTableEntityToJArrayProgram>(nameof(BindTableEntityToJArrayProgram.CallFilter));

            Assert.AreEqual("x1;x3;x4;", result2.Result);

            var result3 = await CallAsync <BindTableEntityToJArrayProgram>(nameof(BindTableEntityToJArrayProgram.CallTake));

            Assert.AreEqual("x1;x2;x3;", result3.Result);

            var result4 = await CallAsync <BindTableEntityToJArrayProgram>(nameof(BindTableEntityToJArrayProgram.Call));

            Assert.AreEqual("x1;x2;x3;x4;", result4.Result);
        }
Beispiel #3
0
        static async Task Main(string[] args)
        {
            var tableClient = new TableClient(
                new Uri("http://127.0.0.1:10002/devstoreaccount1"),
                "people",
                new TableSharedKeyCredential(
                    "devstoreaccount1",
                    "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==")
                );

            await tableClient.CreateIfNotExistsAsync();

            while (true)
            {
                var response = await tableClient.AddEntityAsync(
                    new TableEntity("jong", Guid.NewGuid().ToString())
                {
                    { "FirstName", "Jon" },
                    { "LastName", "Gallant" }
                }
                    );

                Console.WriteLine(response.Status);
                await Task.Delay(TimeSpan.FromSeconds(1));
            }
        }
Beispiel #4
0
        public async Task <bool> CreateAsync(TodoItem newTodo)
        {
            var tableClient = new TableClient(_conn, TableName);

            var result = await tableClient.AddEntityAsync(newTodo);

            return(result.Status == StatusCodes.Status204NoContent ||
                   result.Status == StatusCodes.Status201Created);
        }
 public static async Task Run(
     [HttpTrigger(AuthorizationLevel.Anonymous, "POST")] HttpRequest request,
     [Table("MyTable")] TableClient client)
 {
     await client.AddEntityAsync(new TableEntity("<PartitionKey>", "<RowKey>")
     {
         ["Text"] = request.GetEncodedPathAndQuery()
     });
 }
    public async Task <PwnedPasswordsTransaction> InsertAppendDataAsync(string subscriptionId, CancellationToken cancellationToken = default)
    {
        var transaction = new PwnedPasswordsTransaction {
            TransactionId = Guid.NewGuid().ToString()
        };
        await _transactionTable.AddEntityAsync(new AppendTransactionEntity { PartitionKey = subscriptionId, RowKey = transaction.TransactionId, Confirmed = false }, cancellationToken).ConfigureAwait(false);

        _log.LogInformation("Subscription {SubscriptionId} created a new transaction with id = {TransactionId}.", subscriptionId, transaction.TransactionId);
        return(transaction);
    }
Beispiel #7
0
        public async Task <T> InsertEntityAsync <T>(TableClient tableClient, T entity) where T : AzureTableEntity, new()
        {
            Guard.IsNotNull(tableClient, nameof(tableClient));
            Guard.IsNotNull(entity, nameof(entity));
            entity.SetKeys();
            var response = await tableClient.AddEntityAsync(entity);

            if (response.IsError)
            {
                throw new InvalidOperationException($"Error: {response.Status} {response.ReasonPhrase}");
            }
            return(entity);
        }
Beispiel #8
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            var log = new LogEntity
            {
                EventId      = eventId.ToString(),
                LogLevel     = logLevel.ToString(),
                Message      = formatter(state, exception),//exception?.ToString(),
                PartitionKey = DateTime.Now.ToString("yyyyMMdd"),
                RowKey       = Guid.NewGuid().ToString()
            };

            _tableClient.AddEntityAsync(log);
        }
        public async Task TableEntity_IfBoundToSdkTableEntity_CanCall()
        {
            // Arrange
            await TableClient.AddEntityAsync(CreateTableEntity(PartitionKey, RowKey, "Value", "Foo"));

            // Act
            await CallAsync <BindTableEntityToSdkTableEntityProgram>();

            // Assert
            SdkTableEntity entity = await TableClient.GetEntityAsync <SdkTableEntity>(PartitionKey, RowKey);

            Assert.NotNull(entity);
            Assert.AreEqual("Bar", entity.Value);
        }
        public async Task CustomizeSerialization()
        {
            string storageUri = StorageUri;
            string tableName  = "OfficeSupplies" + _random.Next();

            #region Snippet:CustomSerialization

            // Construct a new TableClient using a TokenCredential.
            var client = new TableClient(
                new Uri(storageUri),
                tableName,
#if SNIPPET
                new DefaultAzureCredential());
#else
                new ClientSecretCredential(
                    GetVariable("TENANT_ID"),
                    GetVariable("CLIENT_ID"),
                    GetVariable("CLIENT_SECRET")));
#endif

            // Create the table if it doesn't already exist.
            client.CreateIfNotExists();

            // Create a new entity with our customization attributes.
            var entity = new CustomSerializationEntity
            {
                PartitionKey = "CustomInventory",
                RowKey       = "special stock",
                Product      = "Fancy Marker",
                Price        = 1.00,
                Quantity     = 42,
                IgnoreMe     = "nothing to see here",
                RenameMe     = "This property will be saved to the table as 'rename_me'"
            };

            // Add the entity to the table. It will be serialized according to our customizations.
            await client.AddEntityAsync(entity);

            // Fetch the entity as a TableEntity so that we can verify that things were serialized as expected.
            var fetchedEntity = await client.GetEntityAsync <TableEntity>(entity.PartitionKey, entity.RowKey);

            // Print each property name to the console.
            foreach (string propertyName in fetchedEntity.Value.Keys)
            {
                Console.WriteLine(propertyName);
            }

            #endregion
        }
        private async Task TestBindToConcurrentlyUpdatedTableEntity <T>(string parameterName)
        {
            // Arrange
            await TableClient.CreateIfNotExistsAsync();

            await TableClient.AddEntityAsync(CreateTableEntity(PartitionKey, RowKey, "Value", "Foo"));

            // Act & Assert
            var exception = Assert.CatchAsync <FunctionInvocationException>(async() => await CallAsync <T>("Call"));

            AssertInvocationETagFailure(parameterName, exception);
            SdkTableEntity entity = await TableClient.GetEntityAsync <SdkTableEntity>(PartitionKey, RowKey);

            Assert.NotNull(entity);
            Assert.AreEqual("FooBackground", entity.Value);
        }
        public async Task TableEntity_IfBoundToJObject_CanCall()
        {
            // Arrange
            await TableClient.AddEntityAsync(CreateTableEntity(PartitionKey, RowKey, "Value", "Foo"));

            await CallAsync <BindTableEntityToJObjectProgram>(arguments : new
            {
                table = TableName, // Test resolution
                pk1   = PartitionKey,
                rk1   = RowKey
            });

            // Assert
            SdkTableEntity entity = await TableClient.GetEntityAsync <SdkTableEntity>(PartitionKey, RowKey);

            Assert.NotNull(entity);
        }
Beispiel #13
0
        public async Task <DishEntity> InsertAsync(DishEntity dish)
        {
            if (dish.PartitionKey == null)
            {
                dish.PartitionKey = "lye";
            }
            if (dish.RowKey == null)
            {
                dish.RowKey = Guid.NewGuid().ToString();
            }

            var response = await _dishTable.AddEntityAsync(dish);

            _logger.LogInformation("Inserted new dish with RowKey {RowKey} - Status: {HttpStatus}", dish.RowKey, response.Status);
            var entity = await _dishTable.GetEntityAsync <DishEntity>(dish.PartitionKey, dish.RowKey);

            return(entity);
        }
        public async Task TableEntity_IfBoundToExistingPoco_BindsUsingNativeTableTypes()
        {
            // Arrange
            byte[] expectedValue = new byte[] { 0x12, 0x34 };

            await TableClient.AddEntityAsync(new TableEntity(PartitionKey, RowKey)
            {
                { "Value", (expectedValue) }
            });

            // Act
            var function = await CallAsync <BindToPocoWithByteArrayValueProgram>();

            var result = function.Entity;

            // Assert
            Assert.NotNull(result);
            Assert.AreEqual(expectedValue, result.Value);
        }
        public async Task TableEntity_IfUpdatesRowKey_Throws()
        {
            // Arrange
            await TableClient.AddEntityAsync(new TableEntity(PartitionKey, RowKey));

            // Act
            var       functionException = Assert.CatchAsync <FunctionInvocationException>(async() => await CallAsync <UpdatePocoRowKeyProgram>());
            Exception exception         = functionException.InnerException;

            // Assert
            Assert.NotNull(exception);
            Assert.IsInstanceOf <InvalidOperationException>(exception);
            Assert.AreEqual("Error while handling parameter entity after function returned:", exception.Message);
            Exception innerException = exception.InnerException;

            Assert.NotNull(innerException);
            Assert.IsInstanceOf <InvalidOperationException>(innerException);
            Assert.AreEqual("When binding to a table entity, the row key must not be changed.", innerException.Message);
        }
        public async Task TableEntity_IfBoundToExistingPoco_Binds()
        {
            // Arrange
            const string expectedValue = "abc";

            await TableClient.AddEntityAsync(new TableEntity(PartitionKey, RowKey)
            {
                { "Value", (expectedValue) }
            });

            // Act
            var function = await CallAsync <BindToPocoProgram>(arguments : new
            {
                newValue = expectedValue
            });

            var result = function.Entity;

            // Assert
            Assert.NotNull(result);
            Assert.AreEqual(expectedValue, result.Value);
        }
        public async Task TableEntity_IfBoundToExistingTableEntity_Binds()
        {
            // Arrange
            const string expectedKey   = "abc";
            const int    expectedValue = 123;

            await TableClient.AddEntityAsync(new TableEntity(PartitionKey, RowKey)
            {
                { expectedKey, (expectedValue) }
            });

            // Act
            var function = await CallAsync <BindToTableEntityProgram>();

            var result = function.Entity;

            // Assert
            Assert.NotNull(result);
            Assert.AreEqual(PartitionKey, result.PartitionKey);
            Assert.AreEqual(RowKey, result.RowKey);
            Assert.AreEqual(expectedValue, result[expectedKey]);
        }
        public async Task TableEntity_IfUpdatesPoco_Persists()
        {
            // Arrange
            const string originalValue = "abc";
            const string expectedValue = "def";

            await TableClient.AddEntityAsync(new TableEntity(PartitionKey, RowKey)
            {
                { "Value", (originalValue) }
            });

            // Act
            await CallAsync <UpdatePocoProgram>(arguments : new
            {
                newValue = expectedValue
            });

            // Assert
            TableEntity entity = await TableClient.GetEntityAsync <TableEntity>(PartitionKey, RowKey);

            Assert.NotNull(entity);
            Assert.AreEqual(expectedValue, entity["Value"]);
        }
        public async Task TableEntity_IfBoundUsingRouteParameters_Binds()
        {
            // Arrange

            await TableClient.AddEntityAsync(new TableEntity(PartitionKey, RowKey)
            {
                { "Value", (123) }
            });

            // Act
            await CallAsync <BindUsingRouteParametersProgram>(arguments : new
            {
                TableName    = TableName,
                PartitionKey = PartitionKey,
                RowKey       = RowKey
            });

            // Assert
            TableEntity entity = await TableClient.GetEntityAsync <TableEntity>(PartitionKey, RowKey);

            Assert.NotNull(entity);
            Assert.AreEqual(456, entity["Value"]);
        }
        public async Task TableEntity_IfUpdatesPoco_PersistsUsingNativeTableTypes()
        {
            // Arrange
            byte[] originalValue = new byte[] { 0x12, 0x34 };
            byte[] expectedValue = new byte[] { 0x56, 0x78 };

            await TableClient.AddEntityAsync(new TableEntity(PartitionKey, RowKey)
            {
                { "Value", (originalValue) }
            });

            // Act
            await CallAsync <UpdatePocoWithByteArrayValueProgram>(arguments : new
            {
                expectedValue
            });

            // Assert
            TableEntity entity = await TableClient.GetEntityAsync <TableEntity>(PartitionKey, RowKey);

            Assert.NotNull(entity);
            Assert.AreEqual(expectedValue, entity["Value"]);
        }
 public Task <Response> CreateAsync(T entity) => _tableClient.AddEntityAsync(entity);
Beispiel #22
0
 protected Task <Azure.Response> InsertDataAsync <T>(TableClient table, T data) where T : class, ITableEntity, new()
 {
     return(table.AddEntityAsync(data));
 }
Beispiel #23
0
 public static async Task InsertAsync(string account, string key, string tableName, string data)
 {
     TableClient table  = new TableClient(Client.GetConnectionString(account, key), tableName);
     TableEntity entity = TableEntityWrapper.Get(data);
     await table.AddEntityAsync <TableEntity>(entity);
 }
Beispiel #24
0
        public Task AddNewTask(Entities.Task task)
        {
            var entity = task.ToTableEntity();

            return(_client.AddEntityAsync(entity));
        }
        public async Task CreateDeleteEntitiesAsync()
        {
            string storageUri        = StorageUri;
            string accountName       = StorageAccountName;
            string storageAccountKey = PrimaryStorageAccountKey;
            string tableName         = "OfficeSupplies2p2";
            string partitionKey      = "Stationery";
            string rowKey            = "A1";
            string rowKeyStrong      = "B1";

            #region Snippet:TablesSample2CreateTableClientAsync
            // Construct a new <see cref="TableClient" /> using a <see cref="TableSharedKeyCredential" />.
            var client = new TableClient(
                new Uri(storageUri),
                tableName,
                new TableSharedKeyCredential(accountName, storageAccountKey));

            // Create the table in the service.
            await client.CreateAsync();

            #endregion

            #region Snippet:TablesSample2CreateEntityAsync
            // Make a dictionary entity by defining a <see cref="TableEntity">.
            var entity = new TableEntity(partitionKey, rowKey)
            {
                { "Product", "Marker Set" },
                { "Price", 5.00 },
                { "Quantity", 21 }
            };

            Console.WriteLine($"{entity.RowKey}: {entity["Product"]} costs ${entity.GetDouble("Price")}.");
            #endregion

            #region Snippet:TablesSample2AddEntityAsync
            // Insert the newly created entity.
            await client.AddEntityAsync(entity);

            #endregion

            #region Snippet:TablesSample2CreateStronglyTypedEntityAsync
            // Create an instance of the strongly-typed entity and set their properties.
            var strongEntity = new OfficeSupplyEntity
            {
                PartitionKey = partitionKey,
                RowKey       = rowKeyStrong,
                Product      = "Notebook",
                Price        = 3.00,
                Quantity     = 50
            };

            Console.WriteLine($"{entity.RowKey}: {strongEntity.Product} costs ${strongEntity.Price}.");
            #endregion

            // Add the newly created entity.
            await client.AddEntityAsync(strongEntity);

            #region Snippet:TablesSample2DeleteEntityAsync
            // Delete the entity given the partition and row key.
            await client.DeleteEntityAsync(partitionKey, rowKey);

            #endregion

            #region Snippet:TablesSample2DeleteTableWithTableClientAsync
            await client.DeleteAsync();

            #endregion
        }