/* invoked from System.Web */
        internal static IInternalConfigSystem ChangeConfigurationSystem(IInternalConfigSystem newSystem)
        {
            if (newSystem == null)
            {
                throw new ArgumentNullException("newSystem");
            }

            lock (lockobj)
            {
                // KLUDGE!! We need that when an assembly loaded inside an ASP.NET
                // domain does OpenExeConfiguration ("") - we must return the path
                // to web.config in that instance.
                string t = newSystem.GetType().ToString();
                if (String.Compare(t, "System.Web.Configuration.HttpConfigurationSystem", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    systemWebInUse = true;
                }
                else
                {
                    systemWebInUse = false;
                }

                IInternalConfigSystem old = configSystem;
                configSystem = newSystem;
                return(old);
            }
        }
Example #2
0
        public override void SubstituteSystemConfiguration(IConfigurationFactory factory, IList <string> fileNames)
        {
            if (OriginalConfiguration != null)
            {
                throw new InvalidOperationException("Web system default configuration already substituted.");
            }

            IInternalConfigSystem originalConfigSystem = SubstituteConfigurationSystem(factory, fileNames);

            // Web Part (10 level black magic starts here)
            var httpConfigurationSystem = new ReflectionAccessor(originalConfigSystem.GetType());

            // Get original values.
            var configSystem = httpConfigurationSystem.GetField <IConfigSystem>("s_configSystem");

            var replacingSystem = new NConfigSystemReplacement(configSystem, factory, fileNames);

            // Substitute to decorated instances.
            httpConfigurationSystem.SetField("s_configSystem", replacingSystem);
            httpConfigurationSystem.SetField("s_configRoot", replacingSystem.Root);

            // Refill system cache with new decorated records.
            var    systemWebAss      = httpConfigurationSystem.AccessedType.Assembly;
            var    hostingEnviroment = new ReflectionAccessor(systemWebAss.GetType("System.Web.Hosting.HostingEnvironment"));
            string siteId            = hostingEnviroment.GetProperty <string>("SiteID");
            string configPath        = "dmachine/webroot/" + siteId;

            var httpRuntime = new ReflectionAccessor(systemWebAss.GetType("System.Web.HttpRuntime"));

            try
            {
                object cacheInternal = httpRuntime.GetProperty("CacheInternal");

                var rootReplacement = replacingSystem.Root as NConfigRootReplacement;

                // In case of single CPU internal caching uses CacheSingle.
                if (cacheInternal.GetType().Name == "CacheSingle")
                {
                    UpdateCacheSingle(rootReplacement, cacheInternal, configPath);
                }
                else
                {
                    UpdateCacheMultiple(rootReplacement, configPath, cacheInternal);
                }
            }
            catch (NullReferenceException)
            {
                // There is no CacheInternal in .Net 4.7.2
            }

            OriginalConfiguration = originalConfigSystem;
        }
Example #3
0
        private static Configuration OpenExeConfigurationImpl(ConfigurationFileMap fileMap, bool isMachine, ConfigurationUserLevel userLevel, string exePath)
        {
            // exePath must be specified if not running inside ClientConfigurationSystem
            if (!isMachine &&
                (((fileMap == null) && (exePath == null)) ||
                 ((fileMap != null) && ((( ExeConfigurationFileMap )fileMap).ExeConfigFilename == null))
                )
                )
            {
                if ((s_configSystem != null) &&
                    (s_configSystem.GetType() != typeof(ClientConfigurationSystem)))
                {
                    throw new ArgumentException(SR.GetString(SR.Config_configmanager_open_noexe));
                }
            }

            return(ClientConfigurationHost.OpenExeConfiguration(fileMap, isMachine, userLevel, exePath));
        }
Example #4
0
        public override void SubstituteSystemConfiguration(IConfigurationFactory factory, IList <string> fileNames)
        {
            if (OriginalConfiguration != null)
            {
                throw new InvalidOperationException("Web system default configuration already substituted.");
            }

            IInternalConfigSystem originalConfigSystem = SubstituteConfigurationSystem(factory, fileNames);

            // Web Part (10 level black magic starts here)
            var httpConfigurationSystem = new ReflectionAccessor(originalConfigSystem.GetType());

            // Get original values.
            var configSystem = httpConfigurationSystem.GetField <IConfigSystem>("s_configSystem");

            var replacingSystem = new NConfigSystemReplacement(configSystem, factory, fileNames);

            // Substitute to decorated instances.
            httpConfigurationSystem.SetField("s_configSystem", replacingSystem);
            httpConfigurationSystem.SetField("s_configRoot", replacingSystem.Root);

            // Refill system cache with new decorated records.
            var    systemWebAss      = httpConfigurationSystem.AccessedType.Assembly;
            var    hostingEnviroment = new ReflectionAccessor(systemWebAss.GetType("System.Web.Hosting.HostingEnvironment"));
            string siteId            = hostingEnviroment.GetProperty <string>("SiteID");
            string configPath        = "dmachine/webroot/" + siteId;

            var httpRuntime   = new ReflectionAccessor(systemWebAss.GetType("System.Web.HttpRuntime"));
            var internalCache = new ReflectionAccessor(httpRuntime.GetProperty("CacheInternal"));
            var caches        = internalCache.GetField("_caches") as IEnumerable ?? Enumerable.Empty <object>();

            // Get all site specific configuration records keys for internal cache.
            var rootReplacement = replacingSystem.Root as NConfigRootReplacement;

            foreach (var cache in caches)
            {
                // Caches stored in array ala hash, so there is could be gaps.
                if (cache == null)
                {
                    continue;
                }

                var cacheAcessor = new ReflectionAccessor(cache);
                lock (cacheAcessor.GetField("_lock"))
                {
                    var entries = cacheAcessor.GetField("_entries") as IEnumerable ?? Enumerable.Empty <object>();

                    // entries is HashTable, so just iterate through
                    foreach (DictionaryEntry entry in entries)
                    {
                        var keyAccessor = new ReflectionAccessor(entry.Key);

                        // Only configuration cache entries replaced.
                        if (!keyAccessor.GetProperty("Key").ToString().StartsWith(configPath))
                        {
                            continue;
                        }

                        // Key and Value is the same object in the configuration cache entry.
                        var entryValueAccesor     = new ReflectionAccessor(keyAccessor.GetField("_value"));
                        var runtimeConfigAccessor = new ReflectionAccessor(entryValueAccesor.GetField("_runtimeConfig"));

                        IInternalConfigRecord replacingRecord = rootReplacement.CreateConfigRecord(runtimeConfigAccessor.GetField <IInternalConfigRecord>("_configRecord"));
                        runtimeConfigAccessor.SetField("_configRecord", replacingRecord);
                        runtimeConfigAccessor.SetField("_runtimeConfigLKG", null);
                    }
                }
            }

            OriginalConfiguration = originalConfigSystem;
        }
 private static System.Configuration.Configuration OpenExeConfigurationImpl(ConfigurationFileMap fileMap, bool isMachine, ConfigurationUserLevel userLevel, string exePath)
 {
     if ((!isMachine && (((fileMap == null) && (exePath == null)) || ((fileMap != null) && (((ExeConfigurationFileMap)fileMap).ExeConfigFilename == null)))) && ((s_configSystem != null) && (s_configSystem.GetType() != typeof(ClientConfigurationSystem))))
     {
         throw new ArgumentException(System.Configuration.SR.GetString("Config_configmanager_open_noexe"));
     }
     return(ClientConfigurationHost.OpenExeConfiguration(fileMap, isMachine, userLevel, exePath));
 }
		/* invoked from System.Web */
		internal static IInternalConfigSystem ChangeConfigurationSystem (IInternalConfigSystem newSystem)
		{
			if (newSystem == null)
				throw new ArgumentNullException ("newSystem");

			lock (lockobj) {
				// KLUDGE!! We need that when an assembly loaded inside an ASP.NET
				// domain does OpenExeConfiguration ("") - we must return the path
				// to web.config in that instance.
				string t = newSystem.GetType ().ToString ();
				if (String.Compare (t, "System.Web.Configuration.HttpConfigurationSystem", StringComparison.OrdinalIgnoreCase) == 0)
					systemWebInUse = true;
				else
					systemWebInUse = false;

				IInternalConfigSystem old = configSystem;
				configSystem = newSystem;
				return old;
			}
		}