Ejemplo n.º 1
0
        private void Scan()
        {
            _logger.Debug("开始搜索扩展和扩展点...");

            foreach (IBundle bundle in _bundleService.Bundles)
            {
                _logger.DebugFormat("   从程序序集 '{0}' 中搜索 id 为 '{1}' 名为  '{2}' 的Bundle.", bundle.AssemblyLocation, bundle.Id, bundle.Name);


                foreach (IExtensionConfiguration extCfg in bundle.ContributedExtensions)
                {
                    IExtension ext = new Extension(extCfg, _extensionBuilder);
                    IExtension old = null;
                    if (_extensions.TryGetValue(ext.Id, out old))
                    {
                        throw new InvalidOperationException(string.Format(
                                                                "扩展Id重复,'{0}' 已存在于 '{1}'中,在 '{2}' 又发现了.", ext.Id, old.BundleId, ext.BundleId));
                    }
                    else
                    {
                        _extensions[ext.Id] = ext;
                        _logger.DebugFormat("     添加扩展 id='{0}'  name='{1}' .", ext.Id, ext.Name);
                    }
                }
                foreach (IExtensionPointConfiguration pointCfg in bundle.ContributedExtensionPoints)
                {
                    IExtensionPoint point = new ExtensionPoint(pointCfg);
                    IExtensionPoint old   = null;
                    if (_extensionPoints.TryGetValue(point.Id, out old))
                    {
                        throw new InvalidOperationException(string.Format(
                                                                "扩展点Id重复,'{0}' 已存在于 '{1}'中,在 '{2}' 又发现了.", point.Id, old.BundleId, point.BundleId));
                    }
                    else
                    {
                        _extensionPoints[point.Id] = point;

                        _logger.DebugFormat("     添加扩展点 id='{0}'  name='{1}' .", point.Id, point.Name);
                    }
                }
            }

            foreach (IExtension ext in _extensions.Values)
            {
                string point = ext.Point;
                if (string.IsNullOrEmpty(point))
                {
                    _logger.WarnFormat("扩展[ id='{0}'  name='{1}']的扩展点属性是空白的.", ext.Id, ext.Name);
                    continue;
                }

                IExtensionPoint ep;
                if (_extensionPoints.TryGetValue(point, out ep))
                {
                    ((ExtensionPoint)ep).AddExtension(ext);
                }
                else
                {
                    _logger.WarnFormat("没有为扩展[ id='{0}'  name='{1}']找到扩展点 '{2}'.", ext.Id, ext.Name, ext.Point);
                }
            }

            _logger.Debug("搜索扩展和扩展点完成." + Environment.NewLine + Environment.NewLine + ToString());
        }
            void IXmlSerializable.ReadXml(XmlReader reader)
            {
                if (!reader.IsStartElement("extensionRegistry"))
                {
                    throw new InvalidOperationException(
                              string.Concat("无效配置.期望节点是 'extensionRegistry 便实际是 '", reader.LocalName, "'."));
                }

                reader.ReadStartElement("extensionRegistry");

                if (!reader.IsStartElement("extensionPoints"))
                {
                    throw new InvalidOperationException(
                              string.Concat("无效配置.期望节点是 'extensionRegistry 便实际是 '", reader.LocalName, "'."));
                }

                if (reader.IsEmptyElement)
                {
                    reader.ReadStartElement("extensionPoints");
                }
                else
                {
                    reader.ReadStartElement("extensionPoints");

                    while (reader.IsStartElement("extensionPoint"))
                    {
                        ExtensionPointConfiguration cfg = new ExtensionPointConfiguration();
                        cfg.ReadXml(reader);
                        List <ExtensionConfiguration> extensionConfigs = new List <ExtensionConfiguration>();
                        XmlUtils.ReadElementsIntoList <ExtensionConfiguration>(reader, "extensions", "extension", extensionConfigs);

                        IExtensionPoint point = new ExtensionPoint(cfg);
                        _registry._extensionPoints.Add(point.Id, point);

                        for (int i = 0; i < extensionConfigs.Count; i++)
                        {
                            IExtension ext = new Extension(extensionConfigs[i], _extensionBuilder);
                            _registry._extensions.Add(ext.Id, ext);
                            ((ExtensionPoint)point).AddExtension(ext);
                        }
                    }

                    reader.ReadEndElement();
                }



                if (reader.IsStartElement("extensions"))
                {
                    if (reader.IsEmptyElement)
                    {
                        reader.ReadStartElement("extensions");
                    }
                    else
                    {
                        // read the orphans
                        reader.ReadStartElement("extensions");

                        while (reader.IsStartElement("extension"))
                        {
                            ExtensionConfiguration extCfg = ExtensionConfiguration.FromXml(reader);
                            if (!_registry._extensions.ContainsKey(extCfg.Id))
                            {
                                IExtension ext = new Extension(extCfg, _extensionBuilder);
                                _registry._extensions.Add(ext.Id, ext);
                            }
                        }

                        reader.ReadEndElement();
                    }
                }

                reader.ReadEndElement(); //</extensionRegistry>
            }