Esempio n. 1
0
        private void Decapsulate(TableResult res, TableOperation oldOp, OperationContext operationContext)
        {
            if (res.Result != null)
            {
                if (!(res.Result is DynamicReplicatedTableEntity))
                {
                    throw new Exception("SHOULD NOT HAPPEN");
                }

                var opType = GetOpType(oldOp);
                var result = (DynamicReplicatedTableEntity)res.Result;
                if (opType == TableOperationType.Retrieve)
                {
                    // For retrieve, we need to construct a new user-defined type using reflection
                    var resolverField = oldOp.GetType().GetField("retrieveResolver", BindingFlags.Instance | BindingFlags.NonPublic);
                    var resolver      = (Func <string, string, DateTimeOffset, IDictionary <string, EntityProperty>, System.String, System.Object>)
                                        resolverField.GetValue(oldOp);

                    var newRes = (ITableEntity)resolver.Invoke(result.PartitionKey, result.RowKey, result.Timestamp, result.Properties, result.ETag);
                    res.Result = newRes;
                }
                else
                {
                    var row = GetEntityFromOperation(oldOp);

                    row.PartitionKey = result.PartitionKey;
                    row.RowKey       = result.RowKey;
                    row.ETag         = result.ETag;
                    row.Timestamp    = result.Timestamp;
                    row.ReadEntity(result.Properties, operationContext);

                    res.Result = row;
                }
            }
        }
Esempio n. 2
0
        private void SetEntityInOperation(TableOperation operation, DynamicReplicatedTableEntity encapsulatedRow)
        {
            // WARNING: We use reflection to read an internal field in OperationType.
            //          We have a dependency on TableOperation fields WindowsAzureStorage dll
            PropertyInfo entity = operation.GetType().GetProperty("Entity", System.Reflection.BindingFlags.GetProperty |
                                                                  System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);

            entity.SetValue(operation, encapsulatedRow);
        }
Esempio n. 3
0
        private string GetRowKeyFromOperation(TableOperation operation)
        {
            // WARNING: We use reflection to read an internal field in OperationType.
            //          We have a dependency on TableOperation fields WindowsAzureStorage dll
            PropertyInfo entity = operation.GetType().GetProperty("RetrieveRowKey", System.Reflection.BindingFlags.GetProperty |
                                                                  System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);

            return((string)(entity.GetValue(operation, null)));
        }
Esempio n. 4
0
        private TableOperationType GetOpType(TableOperation operation)
        {
            // WARNING: We use reflection to read an internal field in OperationType.
            //          We have a dependency on TableOperation fields in WindowsAzureStorage dll
            PropertyInfo opType = operation.GetType().GetProperty("OperationType", System.Reflection.BindingFlags.GetProperty |
                                                                  System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            TableOperationType opTypeValue = (TableOperationType)(opType.GetValue(operation, null));

            return(opTypeValue);
        }
Esempio n. 5
0
 private static void TraceTableOperationFailure(
     RegistrationEntity registration,
     TableResult result,
     TableOperation tableOperation)
 {
     Trace.TraceError(
         string.Format(
             CultureInfo.InvariantCulture,
             "Table storage operation {3} for registration {0}:{1} " + "failed. Status code is {2}, "
             + "check http://msdn.microsoft.com/en-us/library/windowsazure/dd179438.aspx "
             + "for detailed status code information.",
             registration.PartitionKey,
             registration.RowKey,
             result.HttpStatusCode,
             tableOperation.GetType().Name));
 }
Esempio n. 6
0
        private TableResult ExecuteHelper(TableOperation operation, OperationContext opContext)
        {
            var operationType = (TableOperationType)
                                operation.GetType().GetProperty("OperationType",
                                                                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).
                                GetValue(operation);
            var entity = (ITableEntity)
                         operation.GetType().GetProperty("Entity",
                                                         BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).
                         GetValue(operation);

            var result = new TableResult
            {
                Etag           = entity.ETag,
                HttpStatusCode = 200,
                Result         = null
            };

            switch (operationType)
            {
            case TableOperationType.Delete:
                DeleteEntity(entity, opContext, result);
                break;

            case TableOperationType.Insert:
                InsertEntity(entity, opContext, result);
                break;

            case TableOperationType.InsertOrMerge:
                InsertOrMergeEntity(entity, opContext, result);
                break;

            case TableOperationType.InsertOrReplace:
                InsertOrReplaceEntity(entity, opContext, result);
                break;

            case TableOperationType.Merge:
                MergeEntity(entity, opContext, result);
                break;

            case TableOperationType.Replace:
                ReplaceEntity(entity, opContext, result);
                break;

            case TableOperationType.Retrieve:
                var partitionKey = (string)
                                   operation.GetType().GetProperty("RetrievePartitionKey",
                                                                   BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).
                                   GetValue(operation);

                var rowKey = (string)
                             operation.GetType().GetProperty("RetrieveRowKey",
                                                             BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).
                             GetValue(operation);
                var values = RetrieveEntity(partitionKey, rowKey, opContext, result);
                if (values != null)
                {
                    var eTag     = ((DynamicTableEntity)result.Result).Timestamp;
                    var resolver = (Delegate)
                                   operation.GetType().GetProperty("RetrieveResolver",
                                                                   BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).
                                   GetValue(operation);
                    result.Result = resolver.DynamicInvoke(partitionKey, rowKey, DateTimeOffset.UtcNow, values, eTag);
                }
                break;
            }
            return(result);
        }
        /// <inheritdoc />
        protected override async Task <TableResult> ExecuteAsync(TableOperation operation)
        {
            var operationEntity = (TElement)operation.Entity;

            if (operationEntity == null)
            {
                var fields       = operation.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
                var partitionKey = fields.First(f => f.Name.Contains("PartitionKey"));
                var rowKey       = fields.First(f => f.Name.Contains("RowKey"));
                operationEntity = (TElement)Activator.CreateInstance(typeof(TElement));
                operationEntity.PartitionKey = (string)partitionKey.GetValue(operation);
                operationEntity.RowKey       = (string)rowKey.GetValue(operation);
            }

            operationEntity.ETag      = Guid.NewGuid().ToString();
            operationEntity.Timestamp = new DateTimeOffset(DateTime.UtcNow);

            var persistedEntity = _mock.FirstOrDefault(e =>
                                                       e.PartitionKey == operationEntity.PartitionKey &&
                                                       e.RowKey == operationEntity.RowKey);

            var result = new TableResult
            {
                Result         = operationEntity,
                HttpStatusCode = 200,
                Etag           = operationEntity.ETag
            };

            var type      = operation.OperationType;
            var persisted = persistedEntity != null;

            if (type == TableOperationType.Delete ||
                type == TableOperationType.Replace ||
                (type == TableOperationType.InsertOrReplace && persisted))
            {
                _mock.Remove(persistedEntity);
            }

            if (type == TableOperationType.Insert ||
                type == TableOperationType.Replace ||
                (type == TableOperationType.InsertOrReplace) ||
                (type == TableOperationType.InsertOrMerge && !persisted))
            {
                _mock.Add(operationEntity);
            }

            if (type == TableOperationType.Delete ||
                type == TableOperationType.Retrieve ||
                type == TableOperationType.Merge ||
                (type == TableOperationType.InsertOrMerge && persisted))
            {
                result.Result = persistedEntity;
            }

            if (type == TableOperationType.Merge ||
                (type == TableOperationType.InsertOrMerge && persisted))
            {
                persistedEntity = Merge(persistedEntity, operationEntity);
            }

            return(await Task.FromResult(result));
        }