private static void InitializeTableSchemaFromEntity(CloudTableClient tableStorage, string entityName, TableServiceEntity entity)
        {
            TableServiceContext context = tableStorage.GetDataServiceContext();
            DateTime            now     = DateTime.UtcNow;

            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(entityName, entity);
            context.SaveChangesWithRetries();
            context.DeleteObject(entity);
            context.SaveChangesWithRetries();
        }
 public PushServiceTokenRepository()
 {
     storage = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));
     client = storage.CreateCloudTableClient();
     client.CreateTableIfNotExist(TableName);
     context = client.GetDataServiceContext();
 }
 public FriendshipRepository()
 {
     storage = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
     client = storage.CreateCloudTableClient();
     client.CreateTableIfNotExist(TableName);
     context = client.GetDataServiceContext();
 }
 public PersistPrimeSum()
 {
     tableClient = storageAccount.CreateCloudTableClient();
     tableClient.CreateTableIfNotExist(TheTableName);
     tableServiceContext = tableClient.GetDataServiceContext();
     tableServiceContext.IgnoreResourceNotFoundException = true;
 }
 public AzureStorageClient()
 {
     CloudStorageAccount account = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
     mTableClient = account.CreateCloudTableClient();
     mTableClient.CreateTableIfNotExist(TranslationTable);
     mContext = mTableClient.GetDataServiceContext();
     mContext.IgnoreResourceNotFoundException = true;
 }
        public TableStorageSiteUrlRepository()
        {
            _storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting(_connectionStringName)
                );

            _tableClient = new CloudTableClient(_storageAccount.TableEndpoint.AbsoluteUri, _storageAccount.Credentials);
            _tableClient.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromSeconds(1));
            _tableClient.CreateTableIfNotExist(_tableName);
            _tableContext = _tableClient.GetDataServiceContext();
        }
Exemplo n.º 7
0
 public StorageWrapper()
 {
     _storageAccount = CloudStorageAccount.Parse(
         RoleEnvironment.GetConfigurationSettingValue(_connectionStringName)
         );
     _tableClient = new CloudTableClient(_storageAccount.TableEndpoint.AbsoluteUri,
         _storageAccount.Credentials);
     _tableClient.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromSeconds(1));
     _tableClient.CreateTableIfNotExist(_tableName);
     _tableContext = _tableClient.GetDataServiceContext();
 }
        public NotificationRepository()
        {
            // Retrieve storage account from connection-string
            storageAccount = CloudStorageAccount.Parse(Properties.Settings.Default.DataStorageConnectionString);

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

            // Get the data service context
            context = tableClient.GetDataServiceContext();
        }
        public NotificationRepository()
        {
            // Retrieve storage account from connection-string
            storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting(Constants.DataStorageConnectionStringKey));

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

            // Get the data service context
            context = tableClient.GetDataServiceContext();
        }
Exemplo n.º 10
0
        public AzureStorageOld(string account, string key, string table, bool nagling = true)
        {
            this._account = this.GetStorageAccount(account, key);

            EnableNagling(nagling);
            this._client = _account.CreateCloudTableClient();

            this._table = table;

            this.CreateTable(table);

            this._context = _client.GetDataServiceContext();
        }
Exemplo n.º 11
0
        private DatabaseManager()
        {
            // Retrieve the storage account from the connection string
            storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

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

            // Create the table if it doesn't exist
            tableClient.CreateTableIfNotExist(tableName);

            // Get the data service context
            serviceContext = tableClient.GetDataServiceContext();
        }
Exemplo n.º 12
0
        private static void QueryLogTable(CloudTableClient tableStorage)
        {
            TableServiceContext context = tableStorage.GetDataServiceContext();
            DataServiceQuery query = context.CreateQuery<LogEntry>(TableStorageTraceListener.DIAGNOSTICS_TABLE)
                                            .Where(entry => entry.PartitionKey.CompareTo(lastPartitionKey) > 0 
                                               || (entry.PartitionKey == lastPartitionKey && entry.RowKey.CompareTo(lastRowKey) > 0)) 
                                                as DataServiceQuery;

            foreach (AzureDiagnostics.LogEntry entry in query.Execute())
            {
                Console.WriteLine("{0} - {1}", entry.Timestamp, entry.Message);
                lastPartitionKey = entry.PartitionKey;
                lastRowKey = entry.RowKey;
            }
        }
        private static void CreateUserPrivilegeTable(CloudTableClient cloudTableClient)
        {
            cloudTableClient.CreateTableIfNotExist(PrivilegesTableServiceContext.UserPrivilegeTableName);

            // Execute conditionally for development storage only.
            if (cloudTableClient.BaseUri.IsLoopback)
            {
                TableServiceContext context = cloudTableClient.GetDataServiceContext();
                var entity = new UserPrivilege { UserId = "UserId", Privilege = "Privilege" };

                context.AddObject(PrivilegesTableServiceContext.UserPrivilegeTableName, entity);
                context.SaveChangesWithRetries();
                context.DeleteObject(entity);
                context.SaveChangesWithRetries();
            }
        }
Exemplo n.º 14
0
        private static void CreatePushNotificationTable(CloudTableClient cloudTableClient)
        {
            cloudTableClient.CreateTableIfNotExist(UserTablesServiceContext.PushUserTableName);

            // Execute conditionally for development storage only.
            if (cloudTableClient.BaseUri.IsLoopback)
            {
                var context = cloudTableClient.GetDataServiceContext();
                var entity = new PushUserEndpoint { UserId = "UserName", ChannelUri = "http://tempuri", TileCount = 0 };

                context.AddObject(UserTablesServiceContext.PushUserTableName, entity);
                context.SaveChangesWithRetries();
                context.DeleteObject(entity);
                context.SaveChangesWithRetries();
            }
        }
Exemplo n.º 15
0
        public static List<TableColumnsMetadataEntity> GetRdfMetadataColumnNamespace(string entitySet)
        {
            CloudTableClient tableClient = new CloudTableClient(s_account.TableEndpoint.ToString(), s_account.Credentials);

            TableServiceContext tableServiceContext = tableClient.GetDataServiceContext();

            string queryTemplate = "{0}?$filter=entityset eq '{1}'";

            string query = string.Format(queryTemplate, DataLoaderConstants.TableColumnsMetadataTableName, entitySet);
            List<TableColumnsMetadataEntity> resultsQuery = tableServiceContext.Execute<TableColumnsMetadataEntity>(new Uri(query, UriKind.Relative)).ToList();
            if (resultsQuery.Count > 0)
            {
                return resultsQuery;
            }
            else
            {
                return null;
            }
        }
Exemplo n.º 16
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());
            }
        }
        /// <summary>
        /// Creates tables from a DataServiceContext derived class.
        /// </summary>
        /// <param name="serviceContextType">A DataServiceContext derived class that defines the table schemas.</param>
        /// <param name="baseAddress">The baseAddress of the table storage endpoint.</param>
        /// <param name="credentials">Storage account credentials.</param>
        /// <remarks>
        /// For each table, the class exposes one or more IQueryable&lt;T&gt; properties, where T is a
        /// TableServiceEntity derived class with the required schema.
        /// </remarks>
        public static void CreateTablesFromModel(Type serviceContextType, string baseAddress, StorageCredentials credentials)
        {
            CloudTableClient.CreateTablesFromModel(serviceContextType, baseAddress, credentials);

            CloudTableClient tableStorage = new CloudTableClient(baseAddress, credentials);

            // Execute conditionally for development storage only
            if (tableStorage.BaseUri.IsLoopback)
            {
                TableServiceContext ctx = tableStorage.GetDataServiceContext();

                var properties = serviceContextType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                foreach (var table in properties.Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(IQueryable <>)))
                {
                    TableServiceEntity entity = Activator.CreateInstance(table.PropertyType.GetGenericArguments()[0]) as TableServiceEntity;
                    if (entity != null)
                    {
                        InitializeTableSchemaFromEntity(tableStorage, table.Name, entity);
                    }
                }
            }
        }
 /// <summary>
 /// Initializes a new <see cref="TableServiceContextRepositoryBase"/>.
 /// </summary>
 /// <param name="client">A <see cref="CloudTableClient"/> that provides access to the 
 /// backing <see cref="TableServiceContext"/></param>
 /// <param name="entitySetName">The value for <see cref="EntitySetName"/>.</param>
 protected TableServiceContextRepositoryBase(CloudTableClient client, string entitySetName)
     : this(client.GetDataServiceContext(), entitySetName)
 {
     client.CreateTableIfNotExist(entitySetName);
 }
 public TableUtilities(string connectionString)
 {
     _account = CloudStorageAccount.Parse(connectionString);
     _client = _account.CreateCloudTableClient();
     _context = _client.GetDataServiceContext();
 }
Exemplo n.º 20
0
        private List<TableColumnsMetadataEntity> GetColumnsMetadata()
        {
            CloudTableClient clientTable = new CloudTableClient(_Account.TableEndpoint.ToString(), _Account.Credentials);
            TableServiceContext serviceContextTable = clientTable.GetDataServiceContext();

            string query = string.Format("TableColumnsMetadata?$filter=entityset eq '{0}'", EntitySet);
            List<TableColumnsMetadataEntity> resultsQuery = serviceContextTable.Execute<TableColumnsMetadataEntity>(new Uri(query, UriKind.Relative)).ToList();

            return resultsQuery;
        }
Exemplo n.º 21
0
        TableServiceContext GetTableServiceContext(CloudTableClient tables)
        {
            var context = tables.GetDataServiceContext();
            context.MergeOption = MergeOption.NoTracking;

            context.SendingRequest += (sender, args) =>
            {
                var request = args.Request as HttpWebRequest;
                request.Headers["x-ms-version"] = "2011-08-18";
                logger.Info(request.RequestUri);
                lastRequest = request;
            };

            return context;
        }
Exemplo n.º 22
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));
        }
Exemplo n.º 23
0
        TableServiceContext GetTableServiceContext(CloudTableClient tables)
        {
            var context = tables.GetDataServiceContext();

            context.SendingRequest += (sender, args) => {
                var request = args.Request as HttpWebRequest;
                request.Headers["x-ms-version"] = "2011-08-18";
            };

            return context;
        }
        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);
            _accountName = Configuration.GetStringValue(config, "accountName", null, true);
            _sharedKey = Configuration.GetStringValue(config, "sharedKey", null, true);
            _tableName = Configuration.GetStringValueWithGlobalDefault(config, "sessionTableName",
                                                Configuration.DefaultSessionTableNameConfigurationString,
                                                Configuration.DefaultSessionTableName, false);
            _tableServiceBaseUri = Configuration.GetStringValue(config, "tableServiceBaseUri", null, true);
            _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);
            }
            _blobServiceBaseUri = Configuration.GetStringValue(config, "blobServiceBaseUri", null, true);

            config.Remove("allowInsecureRemoteEndpoints");
            config.Remove("accountName");
            config.Remove("sharedKey");
            config.Remove("containerName");
            config.Remove("applicationName");
            config.Remove("blobServiceBaseUri");
            config.Remove("tableServiceBaseUri");
            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);
            }

            StorageCredentialsAccountAndKey tableInfo = null;
            StorageCredentialsAccountAndKey blobInfo = null;
            string tableBaseUri = null;
            string blobBaseUri = null;
            try
            {
                var sharedKey = Configuration.TryGetAppSetting(Configuration.DefaultAccountSharedKeyConfigurationString);
                var accountName = Configuration.TryGetAppSetting(Configuration.DefaultAccountNameConfigurationString);

                tableBaseUri = Configuration.TryGetAppSetting(Configuration.DefaultTableStorageEndpointConfigurationString);
                blobBaseUri = Configuration.TryGetAppSetting(Configuration.DefaultBlobStorageEndpointConfigurationString);

                if (_tableServiceBaseUri != null)
                {
                    tableBaseUri = _tableServiceBaseUri;
                }
                if (_blobServiceBaseUri != null)
                {
                    blobBaseUri = _blobServiceBaseUri;
                }
                if (_accountName != null)
                {
                    accountName = _accountName;
                }
                if (_sharedKey != null)
                {
                    sharedKey = _sharedKey;
                }

                if (String.IsNullOrEmpty(sharedKey) || String.IsNullOrEmpty(accountName) || String.IsNullOrEmpty(tableBaseUri) || String.IsNullOrEmpty(blobBaseUri))
                    throw new ConfigurationErrorsException("Account information incomplete!");

                tableInfo = new StorageCredentialsAccountAndKey(accountName, sharedKey);
                blobInfo = new StorageCredentialsAccountAndKey(accountName, sharedKey);
                SecUtility.CheckAllowInsecureEndpoints(allowInsecureRemoteEndpoints, tableInfo, new Uri(tableBaseUri));
                SecUtility.CheckAllowInsecureEndpoints(allowInsecureRemoteEndpoints, blobInfo, new Uri(blobBaseUri));
                _tableStorage = new CloudTableClient(tableBaseUri, tableInfo);
                _tableStorage.RetryPolicy = _tableRetry;
                if (_tableStorage.CreateTableIfNotExist(_tableName))
                {
                    var ctx = _tableStorage.GetDataServiceContext();
                    var dummyRow = new SessionRow("fake", "dummy");
                    ctx.AddObject(_tableName, dummyRow);
                    ctx.SaveChangesWithRetries();
                    ctx.DeleteObject(dummyRow);
                    ctx.SaveChangesWithRetries();
                }
                _blobProvider = new BlobProvider(blobInfo, new Uri(blobBaseUri), _containerName);
            }
            catch (SecurityException)
            {
                throw;
            }
            // catch InvalidOperationException as well as StorageException
            catch (Exception e)
            {
                string exceptionDescription = Configuration.GetInitExceptionDescription(tableInfo, new Uri(tableBaseUri), blobInfo, new Uri(blobBaseUri));
                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);
        }
        private void Initialize(CloudStorageAccount storageAccount)
        {
            // Create the table client
            _tableClient = storageAccount.CreateCloudTableClient();

            // Get the data service context
            _serviceContext = _tableClient.GetDataServiceContext();
        }
        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 = "TableStorageProfileProvider";
            }

            // 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 profile 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);

            _account =
                CloudStorageAccount.Parse(
                    RoleEnvironment.GetConfigurationSettingValue(Configuration.ConfigurationStorageConnectionStringName));

            // profile information are stored in the membership table
            _tableName = Configuration.GetStringValueWithGlobalDefault(config, "membershipTableName",
                                                           Configuration.DefaultMembershipTableNameConfigurationString,
                                                           Configuration.DefaultMembershipTableName, false);

            _containerName = Configuration.GetStringValueWithGlobalDefault(config, "containerName",
                                                           Configuration.DefaultProfileContainerNameConfigurationString,
                                                           Configuration.DefaultProfileContainerName, false);

            if (!SecUtility.IsValidContainerName(_containerName))
            {
                throw new ProviderException("The provider configuration for the TableStorageProfileProvider 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);
            }
            _blobServiceBaseUri = Configuration.GetStringValue(config, "blobServiceBaseUri", null, true);

            // remove required attributes
            config.Remove("allowInsecureRemoteEndpoints");
            config.Remove("applicationName");
            config.Remove("membershipTableName");
            config.Remove("containerName");
            config.Remove("tableServiceBaseUri");
            config.Remove("blobServiceBaseUri");

            // 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));
                }
            }

            // profiles are stored within the membership table
            _tableStorage = _account.CreateCloudTableClient();
            var blobClient = _account.CreateCloudBlobClient();
            try
            {
                if (_account == null)
                    throw new ConfigurationErrorsException("Account information incomplete!");

                _tableStorage.RetryPolicy = _tableRetry;

                SecUtility.CheckAllowInsecureEndpoints(allowInsecureRemoteEndpoints, _tableStorage.BaseUri);

                if (_tableStorage.CreateTableIfNotExist(_tableName))
                {
                    var ctx = _tableStorage.GetDataServiceContext();
                    var dummyRow = new MembershipRow("dummy", "none");
                    ctx.AddObject(_tableName, dummyRow);
                    ctx.SaveChangesWithRetries();
                    ctx.DeleteObject(dummyRow);
                    ctx.SaveChangesWithRetries();
                }

                SecUtility.CheckAllowInsecureEndpoints(allowInsecureRemoteEndpoints, blobClient.BaseUri);
                _blobProvider = new BlobProvider(blobClient, _containerName);
            }
            catch (SecurityException)
            {
                throw;
            }
            // catch InvalidOperationException and StorageException
            catch (Exception e)
            {
                string exceptionDescription = Configuration.GetInitExceptionDescription(blobClient, _tableStorage);
                string tableName = _tableName ?? "no profile table name specified";
                string containerName = _containerName ?? "no container name specified";
                Log.Write(EventKind.Error, "Initialization of data service structures (tables and/or blobs) failed!" + Environment.NewLine +
                                            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);
        }
Exemplo n.º 27
0
        // Very similar to EventStore.cs
        private IEnumerable<EventTableServiceEntity> GetAllEventSourcingEntries(CloudTableClient tableClient, string tableName)
        {
            var context = tableClient.GetDataServiceContext();
            var query = context
                .CreateQuery<EventTableServiceEntity>(tableName)
                .AsTableServiceQuery();

            var result = new BlockingCollection<EventTableServiceEntity>();
            var tokenSource = new CancellationTokenSource();

            this.retryPolicy.ExecuteAction(
                ac => query.BeginExecuteSegmented(ac, null),
                ar => query.EndExecuteSegmented(ar),
                rs =>
                {
                    foreach (var key in rs.Results)
                    {
                        result.Add(key);
                    }

                    while (rs.HasMoreResults)
                    {
                        try
                        {
                            rs = this.retryPolicy.ExecuteAction(() => rs.GetNext());
                            foreach (var key in rs.Results)
                            {
                                result.Add(key);
                            }
                        }
                        catch
                        {
                            // Cancel is to force an exception being thrown in the consuming enumeration thread
                            // TODO: is there a better way to get the correct exception message instead of an OperationCancelledException in the consuming thread?
                            tokenSource.Cancel();
                            throw;
                        }
                    }
                    result.CompleteAdding();
                },
                ex =>
                {
                    tokenSource.Cancel();
                    throw ex;
                });

            return result.GetConsumingEnumerable(tokenSource.Token);
        }
        // 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);
            _accountName = Configuration.GetStringValue(config, "accountName", null, true);
            _sharedKey = Configuration.GetStringValue(config, "sharedKey", null, true);
            _tableName = Configuration.GetStringValueWithGlobalDefault(config, "roleTableName",
                                                Configuration.DefaultRoleTableNameConfigurationString,
                                                Configuration.DefaultRoleTableName, false);
            _membershipTableName = Configuration.GetStringValueWithGlobalDefault(config, "membershipTableName",
                                                Configuration.DefaultMembershipTableNameConfigurationString,
                                                Configuration.DefaultMembershipTableName, false);
            _tableServiceBaseUri = Configuration.GetStringValue(config, "tableServiceBaseUri", null, true);

            // remove required attributes
            config.Remove("allowInsecureRemoteEndpoints");
            config.Remove("applicationName");
            config.Remove("accountName");
            config.Remove("sharedKey");
            config.Remove("roleTableName");
            config.Remove("membershipTableName");
            config.Remove("tableServiceBaseUri");

            // 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));
                }
            }

            StorageCredentialsAccountAndKey info = null;
            string baseUri = null;
            try
            {
                var sharedKey = Configuration.TryGetAppSetting(Configuration.DefaultAccountSharedKeyConfigurationString);
                var accountName = Configuration.TryGetAppSetting(Configuration.DefaultAccountNameConfigurationString);

                baseUri = Configuration.TryGetAppSetting(Configuration.DefaultTableStorageEndpointConfigurationString);

                if (_tableServiceBaseUri != null)
                {
                    baseUri = _tableServiceBaseUri;
                }
                if (_accountName != null)
                {
                    accountName = _accountName;
                }
                if (_sharedKey != null)
                {
                    sharedKey = _sharedKey;
                }

                if (String.IsNullOrEmpty(sharedKey) || String.IsNullOrEmpty(accountName) || String.IsNullOrEmpty(baseUri))
                    throw new ConfigurationErrorsException("Account information incomplete!");

                info = new StorageCredentialsAccountAndKey(accountName, sharedKey);
                SecUtility.CheckAllowInsecureEndpoints(allowInsecureRemoteEndpoints, info, new Uri(baseUri));
                _tableStorage = new CloudTableClient(baseUri, info);
                _tableStorage.RetryPolicy = _tableRetry;
                if (_tableStorage.CreateTableIfNotExist(_tableName))
                {
                    var ctx = _tableStorage.GetDataServiceContext();
                    var dummyRow = new RoleRow("dummy", "fake", "none");
                    ctx.AddObject(_tableName, dummyRow);
                    ctx.SaveChangesWithRetries();
                    ctx.DeleteObject(dummyRow);
                    ctx.SaveChangesWithRetries();
                }
            }
            catch (SecurityException)
            {
                throw;
            }
            // catch InvalidOperationException as well as StorageException
            catch (Exception e)
            {
                string exceptionDescription = Configuration.GetInitExceptionDescription(info, new Uri(baseUri), "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);
            }
        }
 private TableServiceContext GetServiceContext(CloudTableClient tableClient)
 {
     var tableServiceContext = tableClient.GetDataServiceContext();
     tableServiceContext.ResolveType += ResolveEntityType;
     tableServiceContext.ReadingEntity += new EventHandler<ReadingWritingEntityEventArgs>(OnReadingEntity);
     return tableServiceContext;
 }
Exemplo n.º 30
0
        private static void CreateUserTable(CloudTableClient cloudTableClient)
        {
            cloudTableClient.CreateTableIfNotExist(UserTablesServiceContext.UserTableName);

            // Execute conditionally for development storage only.
            if (cloudTableClient.BaseUri.IsLoopback)
            {
                var context = cloudTableClient.GetDataServiceContext();
                var entity = new User { Name = "UserName", Email = "*****@*****.**" };

                context.AddObject(UserTablesServiceContext.UserTableName, entity);
                context.SaveChangesWithRetries();
                context.DeleteObject(entity);
                context.SaveChangesWithRetries();
            }
        }
Exemplo n.º 31
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="storageAccountName"></param>
        private void InitializeAzureStorage(AzureStorageOptions options)
        {
            Debug.Assert(null != options);

            // TODO: put in the logic to appropriately load configuration
            bool useRoleEnvironment = false;
            string storageConnectionStringValue = null;

            if (useRoleEnvironment)
            {
                storageConnectionStringValue = RoleEnvironment.GetConfigurationSettingValue(options.StorageAccountName);
            }
            else
            {
                storageConnectionStringValue = ConfigurationManager.ConnectionStrings[options.StorageAccountName].ConnectionString;
            }

            Debug.Assert(null != storageConnectionStringValue);

            Account = CloudStorageAccount.Parse(storageConnectionStringValue);

            // This will not work unless run in the DevFabric or in Azure
            // Trace.WriteLine("Azure account credentials:" + Account.Credentials.ToString(), "Information");

            TableClient = Account.CreateCloudTableClient();
            TableClient.RetryPolicy = RetryPolicies.Retry(4, TimeSpan.Zero);
            TableServiceContext = TableClient.GetDataServiceContext();

            BlobClient = Account.CreateCloudBlobClient();
            BlobClient.RetryPolicy = RetryPolicies.Retry(4, TimeSpan.Zero);

            QueueClient = Account.CreateCloudQueueClient();
            QueueClient.RetryPolicy = RetryPolicies.Retry(4, TimeSpan.Zero);

        }
 public BacktrackingRepository(CloudTableClient tableClient)
 {
     _dataServiceContext = tableClient.GetDataServiceContext();
 }