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 = ProviderConfiguration.GetBooleanValue(config, "allowInsecureRemoteEndpoints", false);

            // structure storage-related properties
            applicationName = ProviderConfiguration.GetStringValueWithGlobalDefault(
                config,
                "applicationName",
                ProviderConfiguration.DefaultProviderApplicationNameConfigurationString,
                ProviderConfiguration.DefaultProviderApplicationName,
                false);

            tableName = ProviderConfiguration.GetStringValueWithGlobalDefault(
                config,
                "sessionTableName",
                ProviderConfiguration.DefaultSessionTableNameConfigurationString,
                ProviderConfiguration.DefaultSessionTableName,
                false);

            containerName = ProviderConfiguration.GetStringValueWithGlobalDefault(
                config,
                "containerName",
                ProviderConfiguration.DefaultSessionContainerNameConfigurationString,
                ProviderConfiguration.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 = ProviderConfiguration.GetStorageAccount(ProviderConfiguration.DefaultStorageConfigurationString);
                SecUtility.CheckAllowInsecureEndpoints(allowInsecureRemoteEndpoints, account.Credentials, account.TableEndpoint);
                SecUtility.CheckAllowInsecureEndpoints(allowInsecureRemoteEndpoints, account.Credentials, account.BlobEndpoint);

                tableStorage = account.CreateCloudTableClient();
                tableStorage.RetryPolicy = tableRetry;

                lock (ThisLock)
                {
                    TableStorageExtensionMethods.CreateTableIfNotExist<SessionRow>(tableStorage, tableName);
                }

                blobProvider = new BlobProvider(account.Credentials, account.BlobEndpoint, containerName);
            }
            catch (SecurityException)
            {
                throw;
            }
            catch (Exception e)
            {
                string exceptionDescription = ProviderConfiguration.GetInitExceptionDescription(
                    account.Credentials,
                    account.TableEndpoint,
                    account.BlobEndpoint);
                string table = tableName ?? "no session table name specified";
                string container = containerName ?? "no container name specified";
                Log.Write(
                    EventKind.Error,
                    "Initialization of data service structures (tables and/or blobs) failed!" +
                    exceptionDescription + Environment.NewLine +
                    "Configured blob container: " + container + Environment.NewLine +
                    "Configured table name: " + table + 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);
            }
        }
Exemple #2
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 = ProviderConfiguration.GetBooleanValue(config, "allowInsecureRemoteEndpoints", false);

            // structure storage-related properties
            applicationName = ProviderConfiguration.GetStringValueWithGlobalDefault(
                config,
                "applicationName",
                ProviderConfiguration.DefaultProviderApplicationNameConfigurationString,
                ProviderConfiguration.DefaultProviderApplicationName,
                false);

            tableName = ProviderConfiguration.GetStringValueWithGlobalDefault(
                config,
                "sessionTableName",
                ProviderConfiguration.DefaultSessionTableNameConfigurationString,
                ProviderConfiguration.DefaultSessionTableName,
                false);

            containerName = ProviderConfiguration.GetStringValueWithGlobalDefault(
                config,
                "containerName",
                ProviderConfiguration.DefaultSessionContainerNameConfigurationString,
                ProviderConfiguration.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 = ProviderConfiguration.GetStorageAccount(ProviderConfiguration.DefaultStorageConfigurationString);
                SecUtility.CheckAllowInsecureEndpoints(allowInsecureRemoteEndpoints, account.Credentials, account.TableEndpoint);
                SecUtility.CheckAllowInsecureEndpoints(allowInsecureRemoteEndpoints, account.Credentials, account.BlobEndpoint);

                tableStorage             = account.CreateCloudTableClient();
                tableStorage.RetryPolicy = tableRetry;

                lock (ThisLock)
                {
                    TableStorageExtensionMethods.CreateTableIfNotExist <SessionRow>(tableStorage, tableName);
                }

                blobProvider = new BlobProvider(account.Credentials, account.BlobEndpoint, containerName);
            }
            catch (SecurityException)
            {
                throw;
            }
            catch (Exception e)
            {
                string exceptionDescription = ProviderConfiguration.GetInitExceptionDescription(
                    account.Credentials,
                    account.TableEndpoint,
                    account.BlobEndpoint);
                string table     = tableName ?? "no session table name specified";
                string container = containerName ?? "no container name specified";
                Log.Write(
                    EventKind.Error,
                    "Initialization of data service structures (tables and/or blobs) failed!" +
                    exceptionDescription + Environment.NewLine +
                    "Configured blob container: " + container + Environment.NewLine +
                    "Configured table name: " + table + 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);
            }
        }