/// <summary>
 /// Loads the associated Assembly
 /// </summary>
 /// <returns>Assembly reference</returns>
 public Assembly LoadAssembly()
 {
     //if (!LoadedAssembly.ContainsKey(_assembly))
     //    LoadedAssembly.Add(_assembly, Assembly.Load(_assembly));
     //return LoadedAssembly[_assembly];
     return(ToolkitScriptManagerHelper.GetAssembly(_assembly));
 }
Esempio n. 2
0
        /// <summary>
        /// Get all types of controls referenced by control bundle names.
        /// If control bundle names is empty then default control bundle defined in AjaxControlToolkit.config will be use to retrieved control types.
        /// If AjaxControlToolkit.config file is not found then all standard control types of AjaxControlToolkit will be retrieved and if control bundle names is defined exception will thrown.
        /// </summary>
        /// <param name="context">Current HttpContext.</param>
        /// <param name="bundles">Control bundle names. Will be ignored if AjaxControlToolkit.config file is not found.</param>
        /// <returns></returns>
        public virtual List <Type> GetControlTypesInBundles(HttpContextBase context, string[] bundles, out ScriptReference[] addedScriptReferences, out ScriptReference[] removedScriptReferences)
        {
            var registeredControls = new List <Type>();
            var registeredBundles  = new List <string>();
            var fileName           = context.Server.MapPath(ConfigFileName);

            addedScriptReferences   = null;
            removedScriptReferences = null;

            if (!File.Exists(fileName))
            {
                // No configuration config (AjaxControlToolkit.config) is specified

                // Bundle names specified, but AjaxControlToolkit.config is not provided then exception should be thrown
                if (bundles != null && bundles.Length > 0)
                {
                    throw new Exception("Can not resolve requested control bundle since " + ConfigFileName +
                                        " file is not defined.");
                }

                // Parse all controls type name in ControlDependencyTypeMaps
                var allControlTypesName = new List <string>();
                foreach (var map in ControlDependencyTypeMaps)
                {
                    allControlTypesName.AddRange(map.Value);
                }

                // Load all AjaxControlToolkit controls if there is no bundle specified neither the config file
                registeredControls.AddRange(allControlTypesName.Select(c => Type.GetType(c))
                                            .ToList());
            }
            else
            {
                var actConfig = ParseConfiguration(fileName);
                if (actConfig.ScriptsSections != null && actConfig.ScriptsSections.Length > 0)
                {
                    var addScripts    = new List <ScriptReference>();
                    var removeScripts = new List <ScriptReference>();
                    var actAssembly   = Assembly.GetCallingAssembly().ToString();

                    foreach (var scriptsSection in actConfig.ScriptsSections)
                    {
                        if (scriptsSection.AddScripts != null)
                        {
                            foreach (var script in scriptsSection.AddScripts)
                            {
                                addScripts.Add(new ScriptReference(script.Name,
                                                                   string.IsNullOrEmpty(script.Assembly) ? actAssembly : script.Assembly));
                            }
                        }

                        if (scriptsSection.RemoveScripts != null)
                        {
                            foreach (var script in scriptsSection.RemoveScripts)
                            {
                                removeScripts.Add(new ScriptReference(script.Name,
                                                                      string.IsNullOrEmpty(script.Assembly) ? actAssembly : script.Assembly));
                            }
                        }
                    }

                    addedScriptReferences   = addScripts.ToArray();
                    removedScriptReferences = removeScripts.ToArray();
                }

                if (actConfig.ControlBundleSections != null && actConfig.ControlBundleSections.Length > 0)
                {
                    // Iterate all control bundle sections. Normaly, there will be only 1 section.
                    foreach (var bundle in actConfig.ControlBundleSections)
                    {
                        if (bundle != null && bundle.ControlBundles != null && bundle.ControlBundles.Length > 0)
                        {
                            // Iterate all control bundles in a section.
                            foreach (var controlBundle in bundle.ControlBundles)
                            {
                                // Only add control types if ...
                                if (

                                    // ... bundle contains control(s) and ...
                                    (controlBundle.Controls != null && controlBundle.Controls.Length > 0) && (

                                        // ... this is default control bundle and requested bundle not specified.
                                        (string.IsNullOrEmpty(controlBundle.Name) &&
                                         (bundles == null || bundles.Length == 0)) ||

                                        // .. or this is not default bundle and its specified in requested bundle
                                        (bundles != null && bundles.Contains(controlBundle.Name))
                                        ))
                                {
                                    // Iterate all controls registered in control bundle. Determining control types is works here.
                                    foreach (var control in controlBundle.Controls)
                                    {
                                        if (string.IsNullOrEmpty(control.Assembly) ||
                                            control.Assembly == "AjaxControlToolkit")
                                        {
                                            // Processing AjaxControlToolkit controls
                                            var controlName = "AjaxControlToolkit." + control.Name;

                                            // Verify that control is a standard AjaxControlToolkit control
                                            if (!ControlDependencyTypeMaps.ContainsKey(controlName))
                                            {
                                                throw new Exception(
                                                          string.Format(
                                                              "Could not find control '{0}'. Please make sure you entered the correct control name in AjaxControlToolkit.config file.",
                                                              control.Name));
                                            }

                                            registeredControls.AddRange(ControlDependencyTypeMaps[controlName]
                                                                        .Select(c => Type.GetType(c)));
                                        }
                                        else
                                        {
                                            // Processing custom controls
                                            registeredControls.Add(
                                                ToolkitScriptManagerHelper.GetAssembly(control.Assembly)
                                                .GetType(control.Assembly + "." + control.Name));
                                        }
                                    }

                                    // Mark that bundle is registered for future verification
                                    registeredBundles.Add(controlBundle.Name);
                                }
                            }
                        }
                    }
                }

                // Verify, is there any control in bundle that not registered yet
                if (bundles != null)
                {
                    foreach (var bundle in bundles)
                    {
                        if (!registeredBundles.Contains(bundle))
                        {
                            throw new Exception(string.Format("Could not resolve bundle {0}.", bundle));
                        }
                    }
                }
            }

            // Return unique types
            return(registeredControls.Distinct().ToList());
        }
        /// <summary>
        /// Get all types of controls referenced by control bundle names.
        /// If control bundle names is empty then default control bundle defined in AjaxControlToolkit.config will be use to retrieved control types.
        /// If AjaxControlToolkit.config file is not found then all standard control types of AjaxControlToolkit will be retrieved and if control bundle names is defined exception will thrown.
        /// </summary>
        /// <param name="context">Current HttpContext.</param>
        /// <param name="bundles">Control bundle names. Will be ignored if AjaxControlToolkit.config file is not found.</param>
        /// <returns></returns>
        public virtual List <Type> GetControlTypesInBundles(HttpContextBase context, string[] bundles)
        {
            var registeredControls = new List <Type>();
            var registeredBundles  = new List <string>();
            var fileName           = context.Server.MapPath(ConfigFileName);

            if (!File.Exists(fileName))
            {
                // No configuration config (AjaxControlToolkit.config) is specified

                // Bundle names specified, but AjaxControlToolkit.config is not provided then exception should be thrown
                if (bundles != null && bundles.Length > 0)
                {
                    throw new Exception("Can not resolve requested control bundle since " + ConfigFileName +
                                        " file is not defined.");
                }

                // Parse all controls type name in ControlDependencyTypeMaps
                var allControlTypesName = new List <string>();
                foreach (var map in ControlDependencyTypeMaps)
                {
                    allControlTypesName.AddRange(map.Value);
                }

                // Load all AjaxControlToolkit controls if there is no bundle specified neither the config file
                registeredControls.AddRange(allControlTypesName.Select(c => Type.GetType(c))
                                            .ToList());
            }
            else
            {
                // Bundle configuration (AjaxControlToolkit.config) specified
                // Try read config content from cache
                var configContent = _cacheProvider.Get <string>(CacheConfigName);
                if (string.IsNullOrEmpty(configContent))
                {
                    using (StreamReader sr = File.OpenText(fileName)) {
                        // Retrieve config content from file and caching it
                        configContent = sr.ReadToEnd();
                        _cacheProvider.Set(CacheConfigName, configContent, fileName);
                    }
                }

                // Deserialize bundle configuration
                var bundleConfig = (Config.Settings)(new XmlSerializer(typeof(Config.Settings)))
                                   .Deserialize(new StringReader(configContent));

                // Iterate all control bundle sections. Normaly, there will be only 1 section.
                foreach (var bundle in bundleConfig.ControlBundleSections)
                {
                    // Iterate all control bundles in a section.
                    foreach (var controlBundle in bundle.ControlBundles)
                    {
                        // Only add control types if ...
                        if (
                            // ... this is default control bundle and requested bundle not specified.
                            (string.IsNullOrEmpty(controlBundle.Name) && (bundles == null || bundles.Length == 0)) ||

                            // .. or this is not default bundle and its specified in requested bundle
                            (bundles != null && bundles.Contains(controlBundle.Name)))
                        {
                            // Iterate all controls registered in control bundle. Determining control types is works here.
                            foreach (var control in controlBundle.Controls)
                            {
                                if (string.IsNullOrEmpty(control.Assembly) || control.Assembly == "AjaxControlToolkit")
                                {
                                    // Processing AjaxControlToolkit controls
                                    var controlName = "AjaxControlToolkit." + control.Name;

                                    // Verify that control is a standard AjaxControlToolkit control
                                    if (!ControlDependencyTypeMaps.ContainsKey(controlName))
                                    {
                                        throw new Exception(
                                                  string.Format(
                                                      "Could not find control '{0}'. Please make sure you entered the correct control name in AjaxControlToolkit.config file.",
                                                      control.Name));
                                    }

                                    registeredControls.AddRange(ControlDependencyTypeMaps[controlName]
                                                                .Select(c => Type.GetType(c)));
                                }
                                else
                                {
                                    // Processing custom controls
                                    registeredControls.Add(
                                        ToolkitScriptManagerHelper.GetAssembly(control.Assembly)
                                        .GetType(control.Assembly + "." + control.Name));
                                }
                            }

                            // Mark that bundle is registered for future verification
                            registeredBundles.Add(controlBundle.Name);
                        }
                    }
                }

                // Verify, is there any control in bundle that not registered yet
                if (bundles != null)
                {
                    foreach (var bundle in bundles)
                    {
                        if (!registeredBundles.Contains(bundle))
                        {
                            throw new Exception(string.Format("Could not resolve bundle {0}.", bundle));
                        }
                    }
                }
            }

            // Return unique types
            return(registeredControls.Distinct().ToList());
        }