Ejemplo n.º 1
0
        public async Task <TResult> FindByIdAsync <TEntity, TResult>(
            string rowKey, string partitionKey,
            Func <TEntity, TResult> onSuccess,
            Func <TResult> onNotFound,
            Func <ExtendedErrorInformationCodes, string, TResult> onFailure =
            default(Func <ExtendedErrorInformationCodes, string, TResult>),
            AzureStorageDriver.RetryDelegate onTimeout =
            default(AzureStorageDriver.RetryDelegate))
        {
            var tableName = typeof(TEntity).GetAttributesInterface <IProvideTable>()
                            .First(
                (attr, next) => typeof(TEntity).Name,     //attr.TableName,
                () => typeof(TEntity).Name);
            var propertyNames = typeof(TEntity)
                                .GetProperties()
                                .Where(propInfo => propInfo.ContainsAttributeInterface <IPersistInAzureStorageTables>())
                                .Select(propInfo => propInfo.GetAttributesInterface <IPersistInAzureStorageTables>().First().GetTablePropertyName(propInfo))
                                .Join(",");

            using (var http = new HttpClient(
                       new SharedKeySignatureStoreTablesMessageHandler(this.accountName, this.accountKey))
            {
                Timeout = new TimeSpan(0, 5, 0)
            })
            {
                var url      = $"https://{accountName}.table.core.windows.net/{tableName}(PartitionKey='{partitionKey}',RowKey='{rowKey}')?$select=propertyNames";
                var response = await http.GetAsync(url);

                return(onSuccess(default(TEntity)));
            }
        }
Ejemplo n.º 2
0
        public async Task <TResult> FindAllAsync <TEntity, TResult>(
            string filter,
            Func <TEntity[], TResult> onSuccess,
            Func <ExtendedErrorInformationCodes, string, TResult> onFailure =
            default(Func <ExtendedErrorInformationCodes, string, TResult>),
            AzureStorageDriver.RetryDelegate onTimeout =
            default(AzureStorageDriver.RetryDelegate))
        {
            var tableName = typeof(TEntity).GetAttributesInterface <IProvideTable>()
                            .First(
                (attr, next) => typeof(TEntity).Name,     //attr.TableName,
                () => typeof(TEntity).Name);
            var propertyNames = typeof(TEntity)
                                .GetProperties()
                                .Where(propInfo => propInfo.ContainsAttributeInterface <IPersistInAzureStorageTables>())
                                .Select(propInfo => propInfo.GetAttributesInterface <IPersistInAzureStorageTables>().First().GetTablePropertyName(propInfo))
                                .Join(",");

            using (var http = new HttpClient(
                       new SharedKeySignatureStoreTablesMessageHandler(this.accountName, this.accountKey))
            {
                Timeout = new TimeSpan(0, 5, 0)
            })
            {
                var filterAndParameter = filter.IsNullOrWhiteSpace(
                    () => string.Empty,
                    queryExpression => $"$filter={queryExpression}&");
                var url = $"https://{accountName}.table.core.windows.net/{tableName}()?{filterAndParameter}$select=propertyNames";
                // https://myaccount.table.core.windows.net/mytable()?$filter=<query-expression>&$select=<comma-separated-property-names>
                var response = await http.GetAsync(url);

                return(onSuccess(default(TEntity).AsArray()));
            }
        }
 public static Task <TResult> ResolveCreate <TResult>(this StorageException exception,
                                                      Microsoft.WindowsAzure.Storage.Table.CloudTable table,
                                                      Func <TResult> retry,
                                                      Func <ExtendedErrorInformationCodes, string, TResult> onFailure = default,
                                                      Func <TResult> onAlreadyExists             = default,
                                                      AzureStorageDriver.RetryDelegate onTimeout = default)
 {
     return(exception.ParseStorageException(
                onEntityAlreadyExists: (msg) => onAlreadyExists().AsTask(),
                onNotFound: async(msg) => // IsProblemTableDoesNotExist
     {
         try
         {
             await table.CreateIfNotExistsAsync();
             return retry();
         }
         catch (StorageException createEx)
         {
             // Catch bug with azure storage table client library where
             // if two resources attempt to create the table at the same
             // time one gets a precondtion failed error.
             System.Threading.Thread.Sleep(1000);
             return retry();
         }
         catch (Exception) { throw; };
     },
                onTimeout: async(msg) => // IsProblemTimeout
     {
         if (onTimeout.IsDefaultOrNull())
         {
             onTimeout = AzureStorageDriver.GetRetryDelegate();
         }
         bool shouldRetry = false;
         await onTimeout(exception.RequestInformation.HttpStatusCode, exception,
                         () =>
         {
             shouldRetry = true;
             return 1.AsTask();
         });
         if (shouldRetry)
         {
             return retry();
         }
         throw exception;
     },
                onDefaultCallback:
                (error, msg) => onFailure(error, msg).AsTask()));
 }