Beispiel #1
0
        public EventStore(CloudStorageAccount account, string tableName)
        {
            if (account == null)
            {
                throw new ArgumentNullException("account");
            }
            if (tableName == null)
            {
                throw new ArgumentNullException("tableName");
            }
            if (string.IsNullOrWhiteSpace(tableName))
            {
                throw new ArgumentException("tableName");
            }

            this.account                 = account;
            this.tableName               = tableName;
            this.tableClient             = account.CreateCloudTableClient();
            this.tableClient.RetryPolicy = RetryPolicies.NoRetry();

            // TODO: This could be injected.
            var backgroundRetryStrategy = new ExponentialBackoff(10, TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(15), TimeSpan.FromSeconds(1));
            var blockingRetryStrategy   = new Incremental(3, TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));

            this.pendingEventsQueueRetryPolicy           = new RetryPolicy <StorageTransientErrorDetectionStrategy>(backgroundRetryStrategy);
            this.pendingEventsQueueRetryPolicy.Retrying += (s, e) =>
            {
                var handler = this.Retrying;
                if (handler != null)
                {
                    handler(this, EventArgs.Empty);
                }

                Trace.TraceWarning("An error occurred in attempt number {1} to access table storage (PendingEventsQueue): {0}", e.LastException.Message, e.CurrentRetryCount);
            };
            this.eventStoreRetryPolicy           = new RetryPolicy <StorageTransientErrorDetectionStrategy>(blockingRetryStrategy);
            this.eventStoreRetryPolicy.Retrying += (s, e) => Trace.TraceWarning(
                "An error occurred in attempt number {1} to access table storage (EventStore): {0}",
                e.LastException.Message,
                e.CurrentRetryCount);

            this.eventStoreRetryPolicy.ExecuteAction(() => tableClient.CreateTableIfNotExist(tableName));
        }
Beispiel #2
0
        public string GetHello()
        {
            // Retrieve storage account from connection string
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create the table client
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            // Create the table if it doesn't exist
            string tableName = "users1";

            tableClient.CreateTableIfNotExist(tableName);

            // Get the data service context
            TableServiceContext serviceContext = tableClient.GetDataServiceContext();

            return("Hello from my WCF service in Windows Azure!");
        }
Beispiel #3
0
        public void GeneratePastEventLogMessagesForConferenceManagement(
            CloudTableClient messageLogClient, string messageLogName,
            string conferenceManagementConnectionString,
            IMetadataProvider metadataProvider, ITextSerializer serializer)
        {
            retryPolicy.ExecuteAction(() => messageLogClient.CreateTableIfNotExist(messageLogName));

            // set the creation date to just before releasing V1 (previous month).
            var eventCreationDate = new DateTime(2012, 04, 01, 0, 0, 0, DateTimeKind.Utc);

            var generatedEvents = this.GenerateMissedConferenceManagementIntegrationEvents(conferenceManagementConnectionString);

            foreach (var evt in generatedEvents)
            {
                // generate events in ascending order. If there is a conflict when saving (currently silently swallowed by AzureEventLogWriter),
                // then the migration process is being run for the second time, which is wrong.
                // TODO: what happens if the process crashes middleway.
                eventCreationDate = eventCreationDate.AddSeconds(1);
                var metadata = metadataProvider.GetMetadata(evt);
                var entry    = new MessageLogEntity
                {
                    PartitionKey = eventCreationDate.ToString("yyyMM"),
                    // could have a prefix instead of suffix to be able to search
                    RowKey        = eventCreationDate.Ticks.ToString("D20") + "_Generated",
                    CreationDate  = eventCreationDate.ToString("o"),
                    MessageId     = null,
                    CorrelationId = null,
                    SourceType    = null,
                    SourceId      = evt.SourceId.ToString(),
                    AssemblyName  = metadata[StandardMetadata.AssemblyName],
                    FullName      = metadata[StandardMetadata.FullName],
                    Namespace     = metadata[StandardMetadata.Namespace],
                    TypeName      = metadata[StandardMetadata.TypeName],
                    Kind          = StandardMetadata.EventKind,
                    Payload       = serializer.Serialize(evt),
                };

                var context = messageLogClient.GetDataServiceContext();
                context.AddObject(messageLogName, entry);
                retryPolicy.ExecuteAction(() => context.SaveChanges());
            }
        }
Beispiel #4
0
        private void InitStorage(CloudStorageAccount st)
        {
            // Open blob storage.
            BlobStorageType             = st.CreateCloudBlobClient();
            BlobStorageType.RetryPolicy = RetryPolicies.Retry(2, TimeSpan.FromMilliseconds(100));
            BlobStorageType.GetContainerReference(CONTAINER_NAME).Create();

            // Open queue storage.

            QueueStorageType = st.CreateCloudQueueClient();

            QueueStorageType.RetryPolicy = RetryPolicies.Retry(2, TimeSpan.FromMilliseconds(100));
            QueueStorageType.GetQueueReference("testqueue").CreateIfNotExist();

            // Open table storage.

            TableStorageType             = st.CreateCloudTableClient();
            TableStorageType.RetryPolicy = RetryPolicies.Retry(2, TimeSpan.FromMilliseconds(100));
            TableStorageType.CreateTableIfNotExist("testtable");
        }
Beispiel #5
0
        public void FirstRun(ApiKeyEntity apiKeyEntity)
        {
            // Create the table client
            CloudTableClient tableClient = _storageAccount.CreateCloudTableClient();

            // Create the table if it doesn't exist
            const string tableName = "keys";

            tableClient.CreateTableIfNotExist(tableName);

            // Get the data service context
            TableServiceContext serviceContext = tableClient.GetDataServiceContext();

            // Add the new customer to the people table
            serviceContext.AddObject("keys", apiKeyEntity);

            // Submit the operation to the table service
            serviceContext.SaveChangesWithRetries();

            ApiKeyEntity retrieveApiKey = RetrieveApiKey(apiKeyEntity.ApiKey);
        }
Beispiel #6
0
        public void ProcessRequest(HttpContext context)
        {
            var storageAccount            = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));
            CloudTableClient tableStorage = storageAccount.CreateCloudTableClient();
            string           tableName    = "photos";

            // create container and queue if not created
            StorageRetryPolicy.ExecuteAction(() =>
            {
                tableStorage.CreateTableIfNotExist(tableName);
            });

            // retrieve all rows and return in JSON format
            var tableContext = tableStorage.GetDataServiceContext();
            var query        = (from photo in tableContext.CreateQuery <Photo>(tableName)
                                select photo).AsTableServiceQuery <Photo>();
            var items      = query.ToList();
            var serializer = new JavaScriptSerializer();

            context.Response.ContentType = "application/json";
            context.Response.Write(serializer.Serialize(items));
        }
Beispiel #7
0
        public RepositoryBase(IUnitOfWork context, string table)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (string.IsNullOrEmpty(table))
            {
                throw new ArgumentNullException("table", "Expected a table name.");
            }

            this.Context = context as TableServiceContext;
            this.Table   = table;

            // belt-and-braces code - ensure the table is there for the repository.
            if (this.Context != null)
            {
                var cloudTableClient = new CloudTableClient(this.Context.BaseUri, this.Context.StorageCredentials);
                cloudTableClient.CreateTableIfNotExist(this.Table);
            }
        }
Beispiel #8
0
        public void RunTables()
        {
            Console.WriteLine("RunTables()");
            CloudTableClient tableClient = StorageClientManager.GetTableClient();

            //Create Table
            tableClient.CreateTableIfNotExist(_tableName);

            //List Tables
            IEnumerable <string> tables = tableClient.ListTables();

            foreach (string table in tables)
            {
                Console.WriteLine(table);
            }

            //DataModel, DataContext



            tableClient.DeleteTableIfExist(_tableName);
        }
Beispiel #9
0
        private void SaveBook()
        {
            if (String.IsNullOrEmpty(bookTitle.Text))
            {
                MessageBox.Show("Please provide a book title before continuing.");
                bookTitle.Focus();
                return;
            }

            if (String.IsNullOrEmpty(bookAuthor.Text))
            {
                MessageBox.Show("Please provide a book author before continuing.");
                bookAuthor.Focus();
                return;
            }

            if (String.IsNullOrEmpty(bookCategory.Text))
            {
                MessageBox.Show("Please provide a book category before continuing.");
                bookCategory.Focus();
                return;
            }

            // Save the book...

            CloudTableClient    tableClient  = storageAccount.CreateCloudTableClient();
            TableServiceContext tableContext = tableClient.GetDataServiceContext();

            var book = new Book(bookTitle.Text, bookAuthor.Text, bookCategory.Text);

            tableClient.CreateTableIfNotExist("Book");

            tableContext.AttachTo("Book", book);
            tableContext.UpdateObject(book);
            tableContext.SaveChangesWithRetries(SaveChangesOptions.ReplaceOnUpdate);

            CreateNewBook();
        }
Beispiel #10
0
        private bool CreateIfNotExist <T>(string tableName) where T : ITableServiceEntity
        {
            var  cloudTableClient = new CloudTableClient(this.StorageAccount.TableEndpoint.ToString(), this.StorageAccount.Credentials);
            bool result           = cloudTableClient.CreateTableIfNotExist(tableName);

            if (cloudTableClient.BaseUri.IsLoopback)
            {
                TableServiceContext context = cloudTableClient.GetDataServiceContext();
                DateTime            now     = DateTime.UtcNow;
                ITableServiceEntity entity  = Activator.CreateInstance(typeof(T)) as ITableServiceEntity;
                entity.PartitionKey = Guid.NewGuid().ToString();
                entity.RowKey       = Guid.NewGuid().ToString();
                Array.ForEach(
                    entity.GetType().GetProperties(
                        BindingFlags.Public | BindingFlags.Instance),
                    p =>
                {
                    if ((p.Name != "PartitionKey") && (p.Name != "RowKey") && (p.Name != "Timestamp"))
                    {
                        if (p.PropertyType == typeof(string))
                        {
                            p.SetValue(entity, Guid.NewGuid().ToString(), null);
                        }
                        else if (p.PropertyType == typeof(DateTime))
                        {
                            p.SetValue(entity, now, null);
                        }
                    }
                });

                context.AddObject(tableName, entity);
                context.SaveChangesWithRetries();
                context.DeleteObject(entity);
                context.SaveChangesWithRetries();
            }

            return(result);
        }
Beispiel #11
0
 public void LogQuery(object state)
 {
     try
     {
         var logEntity = state as QueryLogEntity;
         if (logEntity != null)
         {
             var connectionString =
                 RoleEnvironment.GetConfigurationSettingValue(AzureConstants.DiagnosticsConnectionStringName);
             CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
             CloudTableClient    tableClient    = storageAccount.CreateCloudTableClient();
             tableClient.CreateTableIfNotExist("queries");
             TableServiceContext serviceContext = tableClient.GetDataServiceContext();
             serviceContext.AddObject("queries", logEntity);
             serviceContext.SaveChangesWithRetries();
         }
     }
     catch (Exception ex)
     {
         // Log error in logging query stats
         Trace.TraceError("StoreWorkerService.LogQuery: Error logging query to table storage: {0}", ex);
     }
 }
Beispiel #12
0
        static void CreateTableAndAddData(CloudStorageAccount storageAccount)
        {
            // Create service client for credentialed access to the Table service.
            CloudTableClient tableClient = new CloudTableClient(storageAccount.TableEndpoint.ToString(),
                                                                storageAccount.Credentials);
            string tableName = "Stations";

            try
            {
                // Create a new table.
                tableClient.CreateTableIfNotExist(tableName);
                // Get data context.
                TableServiceContext context = tableClient.GetDataServiceContext();
                // Create the new entity

                StationsInfo station = new StationsInfo();
                station.Code = "xxx";
            }
            catch (Exception x)
            {
                Console.WriteLine(x.InnerException);
            }
        }
Beispiel #13
0
        static HtmlBlockProvider()
        {
            CloudTableClient tableClient = CloudTableHelper.GetTableClient();

            tableClient.CreateTableIfNotExist <HtmlBlockEntity>(HtmlBlockTable);
        }
Beispiel #14
0
        static UserProvider()
        {
            CloudTableClient tableClient = CloudTableHelper.GetTableClient();

            tableClient.CreateTableIfNotExist <SiteUserEntity>(SiteUsersTable);
        }
        /// <summary>
        /// Create Table If Not Exists
        /// </summary>
        public void EnsureExist()
        {
            var cloudTableClient = new CloudTableClient(this.account.TableEndpoint.ToString(), this.account.Credentials);

            cloudTableClient.CreateTableIfNotExist <TEntity>(this.tableName);
        }
Beispiel #16
0
        // Write a DataTable to an AzureTable.
        // DataTable's Rows are an unstructured property bag.
        // columnTypes - type of the column, or null if column should be skipped. Length of columnTypes should be the same as number of columns.
        public static void SaveToAzureTable(DataTable table, CloudStorageAccount account, string tableName, Type[] columnTypes, Func <int, Row, PartitionRowKey> funcComputeKeys)
        {
            if (table == null)
            {
                throw new ArgumentNullException("table");
            }
            if (account == null)
            {
                throw new ArgumentNullException("account");
            }
            if (columnTypes == null)
            {
                throw new ArgumentNullException("columnTypes");
            }
            if (tableName == null)
            {
                throw new ArgumentNullException("tableName");
            }
            ValidateAzureTableName(tableName);

            // Azure tables have "special" columns.
            // We can skip these by settings columnType[i] to null, which means don't write that column
            string[] columnNames = table.ColumnNames.ToArray();
            if (columnNames.Length != columnTypes.Length)
            {
                throw new ArgumentException(string.Format("columnTypes should have {0} elements", columnNames.Length), "columnTypes");
            }

            columnTypes = columnTypes.ToArray(); // create a copy for mutation.
            for (int i = 0; i < columnNames.Length; i++)
            {
                if (IsSpecialColumnName(columnNames[i]))
                {
                    columnTypes[i] = null;
                }
            }

            if (funcComputeKeys == null)
            {
                funcComputeKeys = GetPartitionRowKeyFunc(columnNames);
            }

            // Validate columnTypes
            string [] edmTypeNames = Array.ConvertAll(columnTypes,
                                                      columnType => {
                if (columnType == null)
                {
                    return(null);
                }
                string edmTypeName;
                _edmNameMapping.TryGetValue(columnType, out edmTypeName);
                if (edmTypeName == null)
                {
                    // Unsupported type!
                    throw new InvalidOperationException(string.Format("Type '{0}' is not a supported type on azure tables", columnType.FullName));
                }
                return(edmTypeName);
            });


            CloudTableClient tableClient = account.CreateCloudTableClient();

            tableClient.DeleteTableIfExist(tableName);
            tableClient.CreateTableIfNotExist(tableName);


            GenericTableWriter w = new GenericTableWriter
            {
                _edmTypeNames = edmTypeNames,
                _columnNames  = table.ColumnNames.ToArray()
            };

            // Batch rows for performance,
            // but all rows in the batch must have the same partition key
            TableServiceContext ctx = null;
            string lastPartitionKey = null;

            HashSet <PartitionRowKey> dups = new HashSet <PartitionRowKey>();

            int rowCounter = 0;
            int batchSize  = 0;

            foreach (Row row in table.Rows)
            {
                GenericWriterEntity entity = new GenericWriterEntity {
                    _source = row
                };
                // Compute row and partition keys too.
                var partRow = funcComputeKeys(rowCounter, row);
                entity.PartitionKey = partRow.PartitionKey;
                entity.RowKey       = partRow.RowKey;
                rowCounter++;

                // but all rows in the batch must have the same partition key
                if ((ctx != null) && (lastPartitionKey != null) && (lastPartitionKey != entity.PartitionKey))
                {
                    ctx.SaveChangesWithRetries(SaveChangesOptions.Batch | SaveChangesOptions.ReplaceOnUpdate);
                    ctx = null;
                }

                if (ctx == null)
                {
                    dups.Clear();
                    lastPartitionKey = null;
                    ctx = tableClient.GetDataServiceContext();
                    ctx.WritingEntity += new EventHandler <ReadingWritingEntityEventArgs>(w.ctx_WritingEntity);
                    batchSize          = 0;
                }

                // Add enty to the current batch
                // Upsert means insert+Replace. But still need uniqueness within a batch.
                bool allowUpsert = true;

                // Check for dups within a batch.
                var key = new PartitionRowKey {
                    PartitionKey = entity.PartitionKey, RowKey = entity.RowKey
                };
                bool dupWithinBatch = dups.Contains(key);
                dups.Add(key);


                if (allowUpsert)
                {
                    // Upsert allows overwriting existing keys. But still must be unique within a batch.
                    if (!dupWithinBatch)
                    {
                        ctx.AttachTo(tableName, entity);
                        ctx.UpdateObject(entity);
                    }
                }
                else
                {
                    // AddObject requires uniquess.
                    if (dupWithinBatch)
                    {
                        // Azure REST APIs will give us a horrible cryptic error (400 with no message).
                        // Provide users a useful error instead.
                        throw new InvalidOperationException(string.Format("Table has duplicate keys: {0}", key));
                    }

                    ctx.AddObject(tableName, entity);
                }


                lastPartitionKey = entity.PartitionKey;
                batchSize++;

                if (batchSize % UploadBatchSize == 0)
                {
                    // Beware, if keys collide within a batch, we get a very cryptic error and 400.
                    // If they collide across batches, we get a more useful 409 (conflict).
                    try
                    {
                        ctx.SaveChangesWithRetries(SaveChangesOptions.Batch | SaveChangesOptions.ReplaceOnUpdate);
                    }
                    catch (DataServiceRequestException de)
                    {
                        var e = de.InnerException as DataServiceClientException;
                        if (e != null)
                        {
                            if (e.StatusCode == 409)
                            {
                                // Conflict. Duplicate keys. We don't get the specific duplicate key.
                                // Server shouldn't do this if we support upsert.
                                // (although an old emulator that doesn't yet support upsert may throw it).
                                throw new InvalidOperationException(string.Format("Table has duplicate keys. {0}", e.Message));
                            }
                        }
                    }
                    ctx = null;
                }
            }

            if (ctx != null)
            {
                ctx.SaveChangesWithRetries(SaveChangesOptions.Batch | SaveChangesOptions.ReplaceOnUpdate);
            }
        }
Beispiel #17
0
        public override void Initialize(string name, NameValueCollection config)
        {
            // Verify that config isn't null
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            // Assign the provider a default name if it doesn't have one
            if (String.IsNullOrEmpty(name))
            {
                name = "TableServiceSessionStateProvider";
            }

            // Add a default "description" attribute to config if the
            // attribute doesn't exist or is empty
            if (string.IsNullOrEmpty(config["description"]))
            {
                config.Remove("description");
                config.Add("description", "Session state provider using table storage");
            }

            // Call the base class's Initialize method
            base.Initialize(name, config);

            bool allowInsecureRemoteEndpoints = Configuration.GetBooleanValue(config, "allowInsecureRemoteEndpoints", false);

            // structure storage-related properties
            _applicationName = Configuration.GetStringValueWithGlobalDefault(config, "applicationName",
                                                                             Configuration.DefaultProviderApplicationNameConfigurationString,
                                                                             Configuration.DefaultProviderApplicationName, false);
            _tableName = Configuration.GetStringValueWithGlobalDefault(config, "sessionTableName",
                                                                       Configuration.DefaultSessionTableNameConfigurationString,
                                                                       Configuration.DefaultSessionTableName, false);
            _containerName = Configuration.GetStringValueWithGlobalDefault(config, "containerName",
                                                                           Configuration.DefaultSessionContainerNameConfigurationString,
                                                                           Configuration.DefaultSessionContainerName, false);

            if (!SecUtility.IsValidContainerName(_containerName))
            {
                throw new ProviderException("The provider configuration for the TableStorageSessionStateProvider does not contain a valid container name. " +
                                            "Please refer to the documentation for the concrete rules for valid container names." +
                                            "The current container name is: " + _containerName);
            }

            config.Remove("allowInsecureRemoteEndpoints");
            config.Remove("containerName");
            config.Remove("applicationName");
            config.Remove("sessionTableName");

            // Throw an exception if unrecognized attributes remain
            if (config.Count > 0)
            {
                string attr = config.GetKey(0);
                if (!String.IsNullOrEmpty(attr))
                {
                    throw new ProviderException
                              ("Unrecognized attribute: " + attr);
                }
            }

            CloudStorageAccount account = null;

            try
            {
                account = Configuration.GetStorageAccount(Configuration.DefaultStorageConfigurationString);
                SecUtility.CheckAllowInsecureEndpoints(allowInsecureRemoteEndpoints, account.Credentials, account.TableEndpoint);
                SecUtility.CheckAllowInsecureEndpoints(allowInsecureRemoteEndpoints, account.Credentials, account.BlobEndpoint);

                _tableStorage             = account.CreateCloudTableClient();
                _tableStorage.RetryPolicy = _tableRetry;

                lock (thisLock)
                {
                    _tableStorage.CreateTableIfNotExist <SessionRow>(_tableName);
                }

                _blobProvider = new BlobProvider(account.Credentials, account.BlobEndpoint, _containerName);
            }
            catch (SecurityException)
            {
                throw;
            }
            // catch InvalidOperationException as well as StorageException
            catch (Exception e)
            {
                string exceptionDescription = Configuration.GetInitExceptionDescription(account.Credentials, account.TableEndpoint, account.BlobEndpoint);
                string tableName            = (_tableName == null) ? "no session table name specified" : _tableName;
                string containerName        = (_containerName == null) ? "no container name specified" : _containerName;
                Log.Write(EventKind.Error, "Initialization of data service structures (tables and/or blobs) failed!" +
                          exceptionDescription + Environment.NewLine +
                          "Configured blob container: " + containerName + Environment.NewLine +
                          "Configured table name: " + tableName + Environment.NewLine +
                          e.Message + Environment.NewLine + e.StackTrace);
                throw new ProviderException("Initialization of data service structures (tables and/or blobs) failed!" +
                                            "The most probable reason for this is that " +
                                            "the storage endpoints are not configured correctly. Please look at the configuration settings " +
                                            "in your .cscfg and Web.config files. More information about this error " +
                                            "can be found in the logs when running inside the hosting environment or in the output " +
                                            "window of Visual Studio.", e);
            }
            Debug.Assert(_blobProvider != null);
        }
Beispiel #18
0
        /// <summary>
        /// Called by Windows Azure service runtime to initialize the role instance.
        /// </summary>
        /// <returns>Return true if initialization succeeds, otherwise returns false.</returns>
        public override bool OnStart()
        {
            try
            {
                StartDiagnosticMonitor();

                ServicePointManager.DefaultConnectionLimit = Environment.ProcessorCount * 12;
                ServicePointManager.UseNagleAlgorithm      = false;

                TraceManager.WorkerRoleComponent.TraceIn();

                var serviceBusSettings = ConfigurationManager.GetSection(ServiceBusConfigurationSettings.SectionName) as ServiceBusConfigurationSettings;

                var pubsubType           = ConfigurationManager.AppSettings[CommonConsts.ConfigSettingPubSubType];
                var storageAccountName   = ConfigurationManager.AppSettings[CommonConsts.ConfigSettingStorageAccount];
                var storageAccountKey    = ConfigurationManager.AppSettings[CommonConsts.ConfigSettingStorageAccountKey];
                var testResultsTableName = ConfigurationManager.AppSettings[CommonConsts.ConfigSettingTestResultsTableName];
                var testRunsTableName    = ConfigurationManager.AppSettings[CommonConsts.ConfigSettingTestRunsTableName];

                if (pubsubType.Equals(CommonConsts.ConfigSettingPubSubTypeValueTopic))
                {
                    var topicEndpoint = serviceBusSettings.Endpoints.Get(CommonConsts.TopicServiceBusEndpointName);

                    var ircComponent = new InterRoleCommunicationExtension(topicEndpoint);
                    ircComponent.Settings.RetryPolicy.RetryOccurred += (currentRetryCount, ex, delay) =>
                    {
                        TraceManager.WorkerRoleComponent.TraceWarning("******> RETRY LOGIC KICKED IN <******");
                        TraceManager.WorkerRoleComponent.TraceError(ex);
                    };

                    this.interRoleCommunicator = ircComponent;
                }
                else
                {
                    var topicEndpoint = serviceBusSettings.Endpoints.Get(CommonConsts.EventRelayServiceBusEndpointName);

                    this.interRoleCommunicator = new InterRoleEventRelayCommunicationExtension(topicEndpoint);
                }

                int  instanceIndex   = CommonFuncs.GetRoleInstanceIndex(RoleEnvironment.CurrentRoleInstance.Id);
                bool isFirstInstance = (instanceIndex == 0);

                if (isFirstInstance)
                {
                    var creds          = new StorageCredentialsAccountAndKey(storageAccountName, storageAccountKey);
                    var storageAccount = new CloudStorageAccount(creds, true);

                    resultTableStorage = storageAccount.CreateCloudTableClient();
                    resultTableStorage.CreateTableIfNotExist(testResultsTableName);
                    resultTableStorage.CreateTableIfNotExist(testRunsTableName);
                    resultTableStorage.RetryPolicy = RetryPolicies.Retry(RetryPolicies.DefaultClientRetryCount, TimeSpan.FromMilliseconds(100));

                    var ackSubscriber        = new UnicastACKSubscriber(interRoleCommunicator, resultTableStorage, testResultsTableName);
                    var startEventSubscriber = new TestRunStartEventSubscriber(interRoleCommunicator, resultTableStorage, testRunsTableName, ackSubscriber);

                    // Register the subscriber for receiving inter-role communication events.
                    this.activeSubscription.Add(this.interRoleCommunicator.Subscribe(ackSubscriber));
                    this.activeSubscription.Add(this.interRoleCommunicator.Subscribe(startEventSubscriber));
                }
                else
                {
                    var ircEventSubscriber = new MulticastEventSubscriber(instanceIndex.ToString(), interRoleCommunicator);

                    // Register the subscriber for receiving inter-role communication events.
                    this.activeSubscription.Add(this.interRoleCommunicator.Subscribe(ircEventSubscriber));
                }
            }
            catch (Exception ex)
            {
                TraceManager.WorkerRoleComponent.TraceError(ex);

                RoleEnvironment.RequestRecycle();
                return(false);
            }

            return(true);
        }
Beispiel #19
0
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                var                storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));
                CloudBlobClient    blobStorage    = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer container      = blobStorage.GetContainerReference("photos");
                CloudQueueClient   queueStorage   = storageAccount.CreateCloudQueueClient();
                CloudQueue         queue          = queueStorage.GetQueueReference("resizer");
                CloudTableClient   tableStorage   = storageAccount.CreateCloudTableClient();
                string             tableName      = "photos";

                // create container and queue if not created
                StorageRetryPolicy.ExecuteAction(() =>
                {
                    container.CreateIfNotExist();
                    var permissions          = container.GetPermissions();
                    permissions.PublicAccess = BlobContainerPublicAccessType.Container;
                    container.SetPermissions(permissions);

                    queue.CreateIfNotExist();

                    tableStorage.CreateTableIfNotExist(tableName);
                });

                string fileName = context.Request.QueryString["fileName"];
                int    fileSize = Int32.Parse(context.Request.QueryString["fileSize"]);

                // insert a row for this image into the table
                Photo photo = new Photo()
                {
                    PartitionKey = fileName[0].ToString(),
                    RowKey       = fileName,
                    Timestamp    = DateTime.Now,
                    FileSize     = fileSize,
                    Status       = (int)FileStatus.Uploading,
                    BlobUrl      = string.Empty
                };
                var tableContext = tableStorage.GetDataServiceContext();
                tableContext.AddObject(tableName, photo);
                tableContext.SaveChangesWithRetries();

                // create a blob with the image file name and upload file to blob
                var imageBlob = container.GetBlockBlobReference(fileName);
                imageBlob.Properties.ContentType = context.Request.QueryString["contentType"];
                imageBlob.UploadFromStream(context.Request.InputStream);

                // update entity as being processed
                photo.Status  = (int)FileStatus.Processing;
                photo.BlobUrl = imageBlob.Uri.AbsoluteUri;
                tableContext.UpdateObject(photo);
                tableContext.SaveChangesWithRetries();

                // add a new message to the queue
                queue.AddMessage(new CloudQueueMessage(System.Text.Encoding.UTF8.GetBytes(imageBlob.Uri.AbsoluteUri)));

                context.Response.ContentType = "text/plain";
                context.Response.Write("All good.");
            }
            catch (Exception ex)
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write("Error: " + ex.ToString());
            }
        }
        // Write a DataTable to an AzureTable.
        // DataTable's Rows are an unstructured property bag.
        // columnTypes - type of the column, or null if column should be skipped. Length of columnTypes should be the same as number of columns.
        public static void SaveToAzureTable(DataTable table, CloudStorageAccount account, string tableName, Type[] columnTypes, Func <int, Row, ParitionRowKey> funcComputeKeys)
        {
            if (table == null)
            {
                throw new ArgumentNullException("table");
            }
            if (account == null)
            {
                throw new ArgumentNullException("account");
            }
            if (columnTypes == null)
            {
                throw new ArgumentNullException("columnTypes");
            }
            if (tableName == null)
            {
                throw new ArgumentNullException("tableName");
            }
            ValidateAzureTableName(tableName);

            // Azure tables have "special" columns.
            // We can skip these by settings columnType[i] to null, which means don't write that column
            string[] columnNames = table.ColumnNames.ToArray();
            if (columnNames.Length != columnTypes.Length)
            {
                throw new ArgumentException(string.Format("columnTypes should have {0} elements", columnNames.Length), "columnTypes");
            }

            columnTypes = columnTypes.ToArray(); // create a copy for mutation.
            for (int i = 0; i < columnNames.Length; i++)
            {
                if (IsSpecialColumnName(columnNames[i]))
                {
                    columnTypes[i] = null;
                }
            }

            if (funcComputeKeys == null)
            {
                funcComputeKeys = GetPartitionRowKeyFunc(columnNames);
            }

            // Validate columnTypes
            string [] edmTypeNames = Array.ConvertAll(columnTypes,
                                                      columnType => {
                if (columnType == null)
                {
                    return(null);
                }
                string edmTypeName;
                _edmNameMapping.TryGetValue(columnType, out edmTypeName);
                if (edmTypeName == null)
                {
                    // Unsupported type!
                    throw new InvalidOperationException(string.Format("Type '{0}' is not a supported type on azure tables", columnType.FullName));
                }
                return(edmTypeName);
            });


            CloudTableClient tableClient = account.CreateCloudTableClient();

            tableClient.DeleteTableIfExist(tableName);
            tableClient.CreateTableIfNotExist(tableName);


            GenericTableWriter w = new GenericTableWriter
            {
                _edmTypeNames = edmTypeNames,
                _columnNames  = table.ColumnNames.ToArray()
            };

            // Batch rows for performance,
            // but all rows in the batch must have the same partition key
            TableServiceContext ctx = null;
            string lastPartitionKey = null;

            int rowCounter = 0;
            int batchSize  = 0;

            foreach (Row row in table.Rows)
            {
                GenericWriterEntity entity = new GenericWriterEntity {
                    _source = row
                };
                // Compute row and partition keys too.
                var partRow = funcComputeKeys(rowCounter, row);
                entity.PartitionKey = partRow.PartitionKey;
                entity.RowKey       = partRow.RowKey;
                rowCounter++;

                // but all rows in the batch must have the same partition key
                if ((ctx != null) && (lastPartitionKey != null) && (lastPartitionKey != entity.PartitionKey))
                {
                    ctx.SaveChangesWithRetries(SaveChangesOptions.Batch | SaveChangesOptions.ReplaceOnUpdate);
                    ctx = null;
                }

                if (ctx == null)
                {
                    lastPartitionKey = null;
                    ctx = tableClient.GetDataServiceContext();
                    ctx.WritingEntity += new EventHandler <ReadingWritingEntityEventArgs>(w.ctx_WritingEntity);
                    batchSize          = 0;
                }

                // Add enty to the current batch
                ctx.AddObject(tableName, entity);
                lastPartitionKey = entity.PartitionKey;
                batchSize++;

                if (batchSize % 50 == 0)
                {
                    ctx.SaveChangesWithRetries(SaveChangesOptions.Batch | SaveChangesOptions.ReplaceOnUpdate);
                    ctx = null;
                }
            }

            if (ctx != null)
            {
                ctx.SaveChangesWithRetries(SaveChangesOptions.Batch | SaveChangesOptions.ReplaceOnUpdate);
            }
        }
Beispiel #21
0
        public void MigrateEventSourcedAndGeneratePastEventLogs(
            CloudTableClient messageLogClient, string messageLogName,
            CloudTableClient originalEventStoreClient, string originalEventStoreName,
            CloudTableClient newEventStoreClient, string newEventStoreName,
            IMetadataProvider metadataProvider, ITextSerializer serializer)
        {
            retryPolicy.ExecuteAction(() => newEventStoreClient.CreateTableIfNotExist(newEventStoreName));

            var    currentEventStoreContext      = newEventStoreClient.GetDataServiceContext();
            string currentEventStorePartitionKey = null;
            int    currentEventStoreCount        = 0;

            var    currentMessageLogContext      = messageLogClient.GetDataServiceContext();
            string currentMessageLogPartitionKey = null;
            int    currentMessageLogCount        = 0;

            foreach (var esEntry in this.GetAllEventSourcingEntries(originalEventStoreClient, originalEventStoreName))
            {
                // Copies the original values from the stored entry
                var migratedEntry = Mapper.Map <EventTableServiceEntity>(esEntry);

                // get the metadata, as it was not stored in the event store
                var metadata = metadataProvider.GetMetadata(serializer.Deserialize <IVersionedEvent>(esEntry.Payload));
                migratedEntry.AssemblyName = metadata[StandardMetadata.AssemblyName];
                migratedEntry.FullName     = metadata[StandardMetadata.FullName];
                migratedEntry.Namespace    = metadata[StandardMetadata.Namespace];
                migratedEntry.TypeName     = metadata[StandardMetadata.TypeName];
                migratedEntry.CreationDate = esEntry.Timestamp.ToString("o");

                if (currentEventStorePartitionKey == null)
                {
                    currentEventStorePartitionKey = migratedEntry.PartitionKey;
                    ++currentEventStoreCount;
                }
                else if (currentEventStorePartitionKey != migratedEntry.PartitionKey || ++currentEventStoreCount == 100)
                {
                    retryPolicy.ExecuteAction(() => currentEventStoreContext.SaveChanges(SaveChangesOptions.Batch));
                    currentEventStoreContext      = newEventStoreClient.GetDataServiceContext();
                    currentEventStorePartitionKey = migratedEntry.PartitionKey;
                    currentEventStoreCount        = 0;
                }

                currentEventStoreContext.AddObject(newEventStoreName, migratedEntry);

                const string RowKeyVersionLowerLimit = "0000000000";
                const string RowKeyVersionUpperLimit = "9999999999";

                if (migratedEntry.RowKey.CompareTo(RowKeyVersionLowerLimit) >= 0 &&
                    migratedEntry.RowKey.CompareTo(RowKeyVersionUpperLimit) <= 0)
                {
                    var messageId = migratedEntry.PartitionKey + "_" + migratedEntry.RowKey; //This is the message ID used in the past (deterministic).
                    var logEntry  = Mapper.Map <MessageLogEntity>(migratedEntry);
                    logEntry.PartitionKey  = esEntry.Timestamp.ToString("yyyMM");
                    logEntry.RowKey        = esEntry.Timestamp.Ticks.ToString("D20") + "_" + messageId;
                    logEntry.MessageId     = messageId;
                    logEntry.CorrelationId = null;
                    logEntry.Kind          = StandardMetadata.EventKind;

                    if (currentMessageLogPartitionKey == null)
                    {
                        currentMessageLogPartitionKey = logEntry.PartitionKey;
                        ++currentMessageLogCount;
                    }
                    else if (currentMessageLogPartitionKey != logEntry.PartitionKey || ++currentMessageLogCount == 100)
                    {
                        retryPolicy.ExecuteAction(() => currentMessageLogContext.SaveChanges(SaveChangesOptions.Batch));
                        currentMessageLogContext      = messageLogClient.GetDataServiceContext();
                        currentMessageLogPartitionKey = logEntry.PartitionKey;
                        currentMessageLogCount        = 0;
                    }

                    currentMessageLogContext.AddObject(messageLogName, logEntry);
                }
            }

            // save any remaining entries
            retryPolicy.ExecuteAction(() => currentEventStoreContext.SaveChanges(SaveChangesOptions.Batch));
            retryPolicy.ExecuteAction(() => currentMessageLogContext.SaveChanges(SaveChangesOptions.Batch));
        }
Beispiel #22
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="loginUrl">Login url</param>
        /// <param name="userName">User's login name</param>
        /// <param name="password">User's password</param>
        /// <param name="formParams">Optional string which contains any additional form params</param>
        /// <returns></returns>
        public bool LoginToSite(String userName, String password)
        {
            //////////////////////////////////////////////////////////////////////////////
            //
            // local variables
            // TODO: Review if there is a better solution to hard-coding these
            //
            string           formParams = string.Format("language=en&affiliateName=espn&parentLocation=&registrationFormId=espn");
            string           loginUrl   = "https://r.espn.go.com/members/util/loginUser";
            CookieCollection cookies;
            string           leagueId    = "";
            string           teamId      = "";
            string           season      = "";
            string           userTable   = "ausers00";
            string           teamTable   = "ateams00";
            string           cookieTable = "acookies00";
            bool             newUser     = true;


            /////////////////////////////////////////////////////////////////////////////
            //
            // Lookup / Store user in storage.
            //
            // Encrypt the user password for safe persistence
            // TODO: Store the key somewhere safe. Also, it may be better to store password on the device?
            //
            string passwordHash = Crypto.EncryptStringAES(password, "foobar");

            // Retrieve storage account from connection string
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("StorageConnectionString"));

            //
            // Create the table client
            //
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            //
            // Create the user/team/cookie tables if they don't exist
            //
            tableClient.CreateTableIfNotExist(userTable);
            tableClient.CreateTableIfNotExist(teamTable);
            tableClient.CreateTableIfNotExist(cookieTable);

            // Get the data service context
            TableServiceContext serviceContext = tableClient.GetDataServiceContext();


            //
            // Look up the user in the storage tables
            //
            UserEntity specificEntity =
                (from e in serviceContext.CreateQuery <UserEntity>(userTable)
                 where e.RowKey == userName
                 select e).FirstOrDefault();


            //
            // Check to see if password is correct
            //
            if (specificEntity != null)
            {
                string userPassword = Crypto.DecryptStringAES(specificEntity.PasswordHash, "foobar");

                if (userPassword == password)
                {
                    newUser = false;
                }
            }


            //
            // If this is a new user add them to the users databse
            //
            if (newUser)
            {
                //
                // Setup formUrl string
                //
                string formUrl = "";

                if (!String.IsNullOrEmpty(formParams))
                {
                    formUrl = formParams + "&";
                }
                formUrl += string.Format("username={0}&password={1}", userName, password);


                //
                // Now setup the POST request
                //
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(loginUrl);
                req.CookieContainer = new CookieContainer();
                req.ContentType     = "application/x-www-form-urlencoded";
                req.Method          = "POST";

                byte[] bytes = Encoding.ASCII.GetBytes(formUrl);
                req.ContentLength = bytes.Length;

                using (Stream os = req.GetRequestStream())
                {
                    os.Write(bytes, 0, bytes.Length);
                }


                //
                // Read the response
                //
                string respString = "";

                using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
                {
                    //
                    // Gather cookie info
                    //
                    cookies = resp.Cookies;


                    //
                    // Read the response stream to determine if login was successful.
                    //
                    using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
                    {
                        Char[] buffer = new Char[256];
                        int    count  = reader.Read(buffer, 0, 256);
                        while (count > 0)
                        {
                            respString += new String(buffer, 0, count);
                            count       = reader.Read(buffer, 0, 256);
                        }
                    } // reader is disposed here
                }

                //writeLog(respString);

                //
                // If response string contains "login":"******" this indicates success
                //
                if (!respString.Contains("{\"login\":\"true\"}"))
                {
                    throw new InvalidOperationException("Failed to login with given credentials");
                }


                //
                // Login success - Now save user to databse
                // TODO: Figure out a better partition key other than "key"
                //
                UserEntity user = new UserEntity("key", userName);
                user.PasswordHash = passwordHash;

                // Add the new team to the teams table
                serviceContext.AddObject(userTable, user);


                //////////////////////////////////////////////////////////////////////////////////
                //
                // Now that we have logged in, extract user info from the page
                //

                string url2 = "http://games.espn.go.com/frontpage/football";

                HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create(url2);
                req2.CookieContainer = new CookieContainer();
                req2.ContentType     = "application/x-www-form-urlencoded";
                req2.Method          = "POST";

                //
                // Add the login cookies
                //
                foreach (Cookie cook in cookies)
                {
                    //writeLog("**name = " + cook.Name + " value = " + cook.Value);
                    CookieEntity newCook = new CookieEntity(userName, cook.Name);
                    newCook.Value  = cook.Value;
                    newCook.Path   = cook.Path;
                    newCook.Domain = cook.Domain;

                    // Add the new cookie to the cookies table
                    serviceContext.AddObject(cookieTable, newCook);

                    req2.CookieContainer.Add(cook);
                }


                //
                // Read the response
                //
                using (HttpWebResponse resp = (HttpWebResponse)req2.GetResponse())
                {
                    //
                    // Do something with the response stream. As an example, we'll
                    // stream the response to the console via a 256 character buffer
                    using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
                    {
                        HtmlDocument doc = new HtmlDocument();
                        doc.Load(reader);

                        //
                        // Build regEx for extracting legue info from link address
                        //
                        string pattern = @"(clubhouse\?leagueId=)(\d+)(&teamId=)(\d+)(&seasonId=)(\d+)";
                        Regex  rgx     = new Regex(pattern, RegexOptions.IgnoreCase);

                        foreach (HtmlNode hNode in doc.DocumentNode.SelectNodes("//a[@href]"))
                        {
                            HtmlAttribute att   = hNode.Attributes["href"];
                            Match         match = rgx.Match(att.Value);

                            //
                            // If our regEx finds a match then we have leagueId, teamId, and seasonId
                            //
                            if (match.Success)
                            {
                                //writeLog("NODE> Name: " + hNode.Name + ", ID: " + hNode.Id + "attr: " + att.Value);

                                GroupCollection groups = match.Groups;

                                leagueId = groups[2].Value;
                                teamId   = groups[4].Value;
                                season   = groups[6].Value;

                                //
                                // Create a new team entity
                                //
                                TeamEntity team = new TeamEntity(leagueId, teamId);

                                team.Season = season;
                                team.UserId = userName;

                                // Add the new team to the teams table
                                serviceContext.AddObject(teamTable, team);
                            }
                        }
                    } // reader is disposed here
                }

                // Submit the operation to the table service
                serviceContext.SaveChangesWithRetries();
            }

            return(true);
        }
        private HttpWebRequest GetRequest(TableStorageFieldCollection parameters, out string entryId)
        {
            if (m_Connection.State != ConnectionState.Open)
            {
                throw new TableStorageException(Resources.ConnectionNotOpen);
            }

            if (string.IsNullOrWhiteSpace(m_Connection.ConnectionString))
            {
                throw new InvalidOperationException("The ConnectionString property has not been initialized.");
            }

            Regex  queryExpression      = new Regex(@"^(?<verb>GET|POST|PUT|DELETE)\s*(?<path>/(?<tablename>[a-z][a-z0-9]{2,63}).*)$", RegexOptions.IgnoreCase);
            Match  queryMatch           = queryExpression.Match(CommandText);
            string verb                 = queryMatch.Groups["verb"].Value;
            string tableName            = queryMatch.Groups["tablename"].Value;
            string path                 = queryMatch.Groups["path"].Value;
            Regex  parametersExpression = new Regex(@"@(?<param>[\w]{1}[\w\d]*)", RegexOptions.IgnoreCase);

            MatchEvaluator evaluator = delegate(Match match)
            {
                string parameterName = match.Groups["param"].Value;

                if (parameters.Contains(parameterName))
                {
                    TableStorageField parameter = parameters[parameterName];
                    object            value     = parameter.Value;

                    if ((value == null) || DBNull.Value.Equals(value))
                    {
                        return(string.Empty);
                    }

                    return(string.Format(parameter.FormatString, parameter.Value));
                }

                throw new TableStorageException(string.Format(Resources.ParameterNotFound, parameterName));
            };

            path = parametersExpression.Replace(path, evaluator);

            CloudTableClient tableClient = m_Connection.StorageAccount.CreateCloudTableClient();

            tableClient.Timeout = TimeSpan.FromSeconds(CommandTimeout);
            tableClient.CreateTableIfNotExist(tableName);

            // TODO (Matt Magurany 8/14/2012): If a transaction exists, add to existing batch

            Uri url = new Uri(tableClient.BaseUri.AbsoluteUri.Trim('/') + path);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Accept = "application/atom+xml,application/xml";
            request.Headers["x-ms-version"]          = m_Connection.ServerVersion;
            request.Headers["Accept-Charset"]        = "UTF-8";
            request.Headers["DataServiceVersion"]    = "2.0;NetFx";
            request.Headers["MaxDataServiceVersion"] = "2.0;NetFx";
            request.Method  = verb;
            request.Timeout = CommandTimeout * 1000;

            if (StringComparer.OrdinalIgnoreCase.Equals("PUT", verb) || StringComparer.OrdinalIgnoreCase.Equals("DELETE", verb))
            {
                request.Headers["If-Match"] = "*";

                entryId = url.AbsoluteUri;
            }
            else
            {
                entryId = null;
            }

            m_Connection.StorageAccount.Credentials.SignRequestLite(request);

            return(request);
        }
        public bool CreateIfNotExist()
        {
            var cloudTableClient = new CloudTableClient(this.account.TableEndpoint.ToString(), this.account.Credentials);

            return(cloudTableClient.CreateTableIfNotExist <T>(this.tableName));
        }
Beispiel #25
0
        // RoleProvider methods
        public override void Initialize(string name, NameValueCollection config)
        {
            // Verify that config isn't null
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            // Assign the provider a default name if it doesn't have one
            if (String.IsNullOrEmpty(name))
            {
                name = "TableStorageRoleProvider";
            }

            // Add a default "description" attribute to config if the
            // attribute doesn't exist or is empty
            if (string.IsNullOrEmpty(config["description"]))
            {
                config.Remove("description");
                config.Add("description", "Table storage-based role provider");
            }

            // Call the base class's Initialize method
            base.Initialize(name, config);

            bool allowInsecureRemoteEndpoints = Configuration.GetBooleanValue(config, "allowInsecureRemoteEndpoints", false);

            // structure storage-related properties
            ApplicationName = Configuration.GetStringValueWithGlobalDefault(config, "applicationName",
                                                                            Configuration.DefaultProviderApplicationNameConfigurationString,
                                                                            Configuration.DefaultProviderApplicationName, false);
            _tableName = Configuration.GetStringValueWithGlobalDefault(config, "roleTableName",
                                                                       Configuration.DefaultRoleTableNameConfigurationString,
                                                                       Configuration.DefaultRoleTableName, false);

            // remove required attributes
            config.Remove("allowInsecureRemoteEndpoints");
            config.Remove("applicationName");
            config.Remove("roleTableName");

            // Throw an exception if unrecognized attributes remain
            if (config.Count > 0)
            {
                string attr = config.GetKey(0);
                if (!String.IsNullOrEmpty(attr))
                {
                    throw new ProviderException(string.Format(CultureInfo.InstalledUICulture, "Unrecognized attribute: {0}", attr));
                }
            }

            CloudStorageAccount account = null;

            try
            {
                account = Configuration.GetStorageAccount(Configuration.DefaultStorageConfigurationString);
                SecUtility.CheckAllowInsecureEndpoints(allowInsecureRemoteEndpoints, account.Credentials, account.TableEndpoint);
                _tableStorage             = account.CreateCloudTableClient();
                _tableStorage.RetryPolicy = _tableRetry;
                _tableStorage.CreateTableIfNotExist <RoleRow>(_tableName);
            }
            catch (SecurityException)
            {
                throw;
            }
            // catch InvalidOperationException as well as StorageException
            catch (Exception e)
            {
                string exceptionDescription = Configuration.GetInitExceptionDescription(account.Credentials, account.TableEndpoint, "table storage configuration");
                string tableName            = (_tableName == null) ? "no role table name specified" : _tableName;
                Log.Write(EventKind.Error, "Could not create or find role table: " + tableName + "!" + Environment.NewLine +
                          exceptionDescription + Environment.NewLine +
                          e.Message + Environment.NewLine + e.StackTrace);
                throw new ProviderException("Could not create or find role table. The most probable reason for this is that " +
                                            "the storage endpoints are not configured correctly. Please look at the configuration settings " +
                                            "in your .cscfg and Web.config files. More information about this error " +
                                            "can be found in the logs when running inside the hosting environment or in the output " +
                                            "window of Visual Studio.", e);
            }
        }
Beispiel #26
0
        public override void Run()
        {
            var                storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));
            CloudBlobClient    blobStorage    = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container      = blobStorage.GetContainerReference("photos");
            CloudQueueClient   queueStorage   = storageAccount.CreateCloudQueueClient();
            CloudQueue         queue          = queueStorage.GetQueueReference("resizer");
            CloudTableClient   tableStorage   = storageAccount.CreateCloudTableClient();
            string             tableName      = "photos";

            // create container and queue if not created
            StorageRetryPolicy.ExecuteAction(() =>
            {
                container.CreateIfNotExist();
                var permissions          = container.GetPermissions();
                permissions.PublicAccess = BlobContainerPublicAccessType.Container;
                container.SetPermissions(permissions);

                queue.CreateIfNotExist();

                tableStorage.CreateTableIfNotExist(tableName);
            });

            var tableContext = tableStorage.GetDataServiceContext();

            // dequeue and process messages from the queue
            while (true)
            {
                try
                {
                    var msg = queue.GetMessage();
                    if (msg != null)
                    {
                        // get the base name of the image
                        string path     = msg.AsString;
                        var    baseName = Path.GetFileNameWithoutExtension(path);

                        // download image from blob into memory
                        var blob = container.GetBlockBlobReference(path);
                        using (var sourceImageStream = new MemoryStream())
                        {
                            blob.DownloadToStream(sourceImageStream);

                            // build resized images for each image size and upload to blob
                            foreach (var size in _imageSizes)
                            {
                                sourceImageStream.Seek(0, SeekOrigin.Begin);
                                using (var targetImageStream = ResizeImage(sourceImageStream, size.Width, size.Height))
                                {
                                    // upload to blob
                                    var imageName   = String.Format("{0}-{1}x{2}.jpg", baseName, size.Width, size.Height);
                                    var resizedBlob = container.GetBlockBlobReference(imageName);
                                    resizedBlob.Properties.ContentType = "image/jpeg";
                                    targetImageStream.Seek(0, SeekOrigin.Begin);
                                    resizedBlob.UploadFromStream(targetImageStream);
                                }
                            }
                        }

                        // get the entity from the table based on file name and upate status
                        var rowKey       = Path.GetFileName(path);
                        var partitionKey = rowKey[0].ToString();
                        var query        = (from photo in tableContext.CreateQuery <Photo>(tableName)
                                            where photo.PartitionKey == partitionKey && photo.RowKey == rowKey
                                            select photo).AsTableServiceQuery <Photo>();
                        var photoEntity = query.First();
                        photoEntity.Status = (int)FileStatus.Processed;
                        tableContext.UpdateObject(photoEntity);
                        tableContext.SaveChangesWithRetries();

                        queue.DeleteMessage(msg);
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                }
                catch (StorageException ex)
                {
                    System.Threading.Thread.Sleep(5000);
                    Trace.TraceError(string.Format("Exception when processing queue item. Message: '{0}'", ex.Message));
                }
            }
        }
Beispiel #27
0
 private void LogJob(string jobId)
 {
     try
     {
         JobInfo jobInfo = null;
         using (var conn = new SqlConnection(_connectionString))
         {
             conn.Open();
             try
             {
                 using (var cmd = conn.CreateCommand())
                 {
                     cmd.CommandType = CommandType.StoredProcedure;
                     cmd.CommandText = "GetJobDetail";
                     cmd.Parameters.AddWithValue("jobId", jobId);
                     using (var reader = cmd.ExecuteReader())
                     {
                         if (reader.Read())
                         {
                             jobInfo                  = new JobInfo(reader.GetString(0), reader.GetString(1));
                             jobInfo.JobType          = (JobType)reader.GetInt32(2);
                             jobInfo.Status           = (JobStatus)reader.GetInt32(3);
                             jobInfo.StatusMessage    = reader.IsDBNull(4) ? String.Empty : reader.GetString(4);
                             jobInfo.ScheduledRunTime =
                                 reader.IsDBNull(5) ? (DateTime?)null : reader.GetDateTime(5);
                             jobInfo.StartTime           = reader.IsDBNull(6) ? (DateTime?)null : reader.GetDateTime(6);
                             jobInfo.ProcessorId         = reader.IsDBNull(7) ? String.Empty : reader.GetString(7);
                             jobInfo.ProcessingCompleted =
                                 reader.IsDBNull(8) ? (DateTime?)null : reader.GetDateTime(8);
                             jobInfo.ProcessingException =
                                 reader.IsDBNull(9) ? String.Empty : reader.GetString(9);
                             jobInfo.RetryCount = reader.GetInt32(10);
                         }
                     }
                 }
             }
             catch (Exception ex)
             {
                 Trace.TraceError("Error retrieving job detail for job {0}. Cause: {1}", jobId, ex);
             }
             finally
             {
                 conn.Close();
             }
         }
         if (jobInfo != null)
         {
             var connectionString =
                 RoleEnvironment.GetConfigurationSettingValue(AzureConstants.DiagnosticsConnectionStringName);
             CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
             CloudTableClient    tableClient    = storageAccount.CreateCloudTableClient();
             tableClient.CreateTableIfNotExist("jobs");
             TableServiceContext serviceContext = tableClient.GetDataServiceContext();
             JobLogEntity        jobLogEntity   = new JobLogEntity(jobInfo);
             serviceContext.AddObject("jobs", jobLogEntity);
             serviceContext.SaveChangesWithRetries();
         }
     }
     catch (Exception ex)
     {
         Trace.TraceError("Error logging detail for job {0}. Cause: {1}", jobId, ex);
     }
 }