internal static void InitPolling(string database)
        {
            SqlCacheDependencySection sqlCacheDependency = RuntimeConfig.GetAppConfig().SqlCacheDependency;

            if (!sqlCacheDependency.Enabled)
            {
                throw new ConfigurationErrorsException(System.Web.SR.GetString("Polling_not_enabled_for_sql_cache"), sqlCacheDependency.ElementInformation.Properties["enabled"].Source, sqlCacheDependency.ElementInformation.Properties["enabled"].LineNumber);
            }
            SqlCacheDependencyDatabase databaseConfig = GetDatabaseConfig(database);

            if (databaseConfig.PollTime == 0)
            {
                throw new ConfigurationErrorsException(System.Web.SR.GetString("Polltime_zero_for_database_sql_cache", new object[] { database }), databaseConfig.ElementInformation.Properties["pollTime"].Source, databaseConfig.ElementInformation.Properties["pollTime"].LineNumber);
            }
            if (!s_DatabaseNotifStates.ContainsKey(database))
            {
                string connection = SqlConnectionHelper.GetConnectionString(databaseConfig.ConnectionStringName, true, true);
                if ((connection == null) || (connection.Length < 1))
                {
                    throw new ConfigurationErrorsException(System.Web.SR.GetString("Connection_string_not_found", new object[] { databaseConfig.ConnectionStringName }), databaseConfig.ElementInformation.Properties["connectionStringName"].Source, databaseConfig.ElementInformation.Properties["connectionStringName"].LineNumber);
                }
                lock (s_DatabaseNotifStates)
                {
                    if (!s_DatabaseNotifStates.ContainsKey(database))
                    {
                        DatabaseNotifState state;
                        state = new DatabaseNotifState(database, connection, databaseConfig.PollTime)
                        {
                            _timer = new Timer(s_timerCallback, state, 0, databaseConfig.PollTime)
                        };
                        s_DatabaseNotifStates.Add(database, state);
                    }
                }
            }
        }
        static void Main(string[] args)
        {
            string inputStr = String.Empty;

            // Define a regular expression to allow only
            // alphanumeric inputs that are at most 20 character
            // long. For instance "/iii:".
            Regex rex = new Regex(@"[^\/w]{1,20}");

            // Parse the user's input.
            if (args.Length < 1)
            {
                // No option entered.
                Console.Write("Input parameters missing.");
                return;
            }
            else
            {
                // Get the user's options.
                inputStr = args[0].ToLower();

                if (!(rex.Match(inputStr)).Success)
                {
                    // Wrong option format used.
                    Console.Write("Input format not allowed.");
                    return;
                }
            }

            // <Snippet1>

            // Get the Web application configuration.
            System.Configuration.Configuration configuration =
                WebConfigurationManager.OpenWebConfiguration(
                    "/aspnetTest");

            // Get the <caching> section group.
            SystemWebCachingSectionGroup cachingSectionGroup =
                (SystemWebCachingSectionGroup)configuration.GetSectionGroup(
                    "system.web/caching");

            // </Snippet1>

            try
            {
                switch (inputStr)
                {
                case "/cache":

                    // <Snippet2>

                    // Get the <cache> section.
                    CacheSection cache =
                        cachingSectionGroup.Cache;

                    // Display one of its properties.
                    msg = String.Format(
                        "Cache disable expiration: {0}\n",
                        cache.DisableExpiration);

                    Console.Write(msg);

                    // </Snippet2>

                    break;

                case "/outcache":

                    // <Snippet3>

                    // Get the .<outputCache> section
                    OutputCacheSection outputCache =
                        cachingSectionGroup.OutputCache;

                    // Display one of its properties.
                    msg = String.Format(
                        "Enable output cache: {0}\n",
                        outputCache.EnableOutputCache.ToString());

                    Console.Write(msg);

                    // </Snippet3>

                    break;

                case "/outcacheset":

                    // <Snippet4>

                    // Get the .<outputCacheSettings> section
                    OutputCacheSettingsSection outputCacheSettings =
                        cachingSectionGroup.OutputCacheSettings;

                    // Display the number of existing
                    // profiles.
                    int profilesCount =
                        outputCacheSettings.OutputCacheProfiles.Count;
                    msg = String.Format(
                        "Number of profiles: {0}\n",
                        profilesCount.ToString());

                    Console.Write(msg);

                    // </Snippet4>

                    break;

                case "/sql":

                    // <Snippet5>

                    // Get the .<sqlCacheDependency> section
                    SqlCacheDependencySection sqlCacheDependency =
                        cachingSectionGroup.SqlCacheDependency;

                    // Display one of its attributes.
                    msg = String.Format(
                        "Sql cache dependency enabled: {0}\n",
                        sqlCacheDependency.Enabled.ToString());

                    Console.Write(msg);

                    // </Snippet5>

                    break;

                //  case "/all":

                // <Snippet6>

                // Not in use anymore.
                // StringBuilder allSections = new StringBuilder();

                // Get the section collection.
                //  ConfigurationSectionCollection sections=
                //    cachingSectionGroup.Sections;

                // Get the number of sections.
                // int sectionsNumber = sections.Count;

                //  System.Collections.IEnumerator ienum =
                //     sections.AllKeys.GetEnumerator();

                // int i = 0;
                // allSections.AppendLine();
                // foreach (Object section in sections)
                // {
                //    msg = String.Format(
                //     "Section{0}:  {1}\n",
                //    i, section.ToString());
                //    allSections.AppendLine(msg);
                //    i++;
                // }

                // </Snippet6>

                // Console.Write(allSections.ToString());
                //     break;

                default:
                    // Option is not allowed..
                    Console.Write("Input not allowed.");
                    break;
                }
            }
            catch (ArgumentException e)
            {
                // Never display this. Use it for
                // debugging purposes.
                msg = e.ToString();
            }
        }
Ejemplo n.º 3
0
        public static void Main(string[] args)
        {
// <Snippet1>
            // Get the Web application configuration.
            Configuration webConfig =
                WebConfigurationManager.OpenWebConfiguration("/aspnetTest");

            // Get the section.
            string configPath = "system.web/cache/sqlCacheDependency";
            SqlCacheDependencySection sqlCacheDependencySection =
                (SqlCacheDependencySection)webConfig.GetSection(configPath);

            // Get the database element at the specified index.
            SqlCacheDependencyDatabaseCollection sqlCdds =
                sqlCacheDependencySection.Databases;

// </Snippet1>

// <Snippet2>

            object[] collectionKeys = sqlCdds.AllKeys;

// </Snippet2>

// <Snippet3>
            SqlCacheDependencyDatabase dbElement =
                new SqlCacheDependencyDatabase(
                    "dataBase", "dataBaseElement", 500);

            sqlCdds.Add(dbElement);

// </Snippet3>

// <Snippet4>
            sqlCdds.Clear();

// </Snippet4>

// <Snippet5>
            SqlCacheDependencyDatabase dbElement1 =
                sqlCdds.Get(0);

// </Snippet5>

// <Snippet6>
            SqlCacheDependencyDatabase dbElement3 =
                sqlCdds.Get("dataBaseElement");

// </Snippet6>

// <Snippet7>
            string thisKey = sqlCdds.GetKey(0).ToString();

// </Snippet7>

// <Snippet8>
            sqlCdds.Remove("dataBaseElement");

// </Snippet8>

// <Snippet9>
            sqlCdds.RemoveAt(0);

// </Snippet9>

// <Snippet10>
            SqlCacheDependencyDatabase dbElement2 =
                new SqlCacheDependencyDatabase(
                    "dataBase2", "dataBaseElement2", 500);

            sqlCdds.Set(dbElement2);

// </Snippet10>
        }