Ejemplo n.º 1
0
        /// <summary>
        /// 从配置中获取插件工厂设置
        /// </summary>
        /// <param name="configuration"></param>
        public void ConfigFromConfigration(PluginFactoryConfigration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            IConfiguration pluginConfig = configuration.Configuration;
            string         path         = pluginConfig[DEFAULT_PLUGIN_PATH_KEY];

            if (!String.IsNullOrEmpty(path) && !String.Equals(path, PluginPath, StringComparison.OrdinalIgnoreCase))
            {
                if (!Path.IsPathRooted(path))
                {
                    path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
                }
                path       = Path.GetFullPath(path);
                PluginPath = path;

                if (FileProvider == null || FileProvider is PhysicalFileProvider)
                {
                    if (Directory.Exists(path))
                    {
                        FileProvider = new PhysicalFileProvider(path);
                    }
                    else
                    {
                        FileProvider = null;
                    }
                }
            }

            configDisablePlugin(pluginConfig);
        }
        public PluginConfigrationProvider(PluginFactoryConfigration configration)
        {
            if (configration == null)
            {
                throw new ArgumentNullException(nameof(configration));
            }

            Type   pluginType = typeof(TPlugin);
            string configKey  = typeof(TPlugin).FullName;
            var    section    = configration.Configuration.GetSection(configKey);

            if (!section.Exists())
            {
                // 内嵌类型,将+号替换为.
                configKey = configKey.Replace("+", ".");
                section   = configration.Configuration.GetSection(configKey);
            }

            var attr = pluginType.GetCustomAttributes(typeof(PluginAttribute), false).OfType <PluginAttribute>().FirstOrDefault();
            IConfigurationSection aliasSection = null;

            if (attr != null && !String.IsNullOrEmpty(attr.Alias))
            {
                configKey = attr.Alias;
                var section2 = configration.Configuration.GetSection(configKey);
                if (section2.Exists())
                {
                    aliasSection = section2;
                }
            }

            // 共享配置
            var shareSection = configration.Configuration.GetSection(DEFAULT_SHARE_KEY);


            // 合并多个配置
            IConfigurationSection[] configList = new IConfigurationSection[]
            {
                // 共享配置可以被覆盖
                shareSection, section, aliasSection
            };
            if (configList.Count(x => x != null && x.Exists()) > 1)
            {
                var cb = new ConfigurationBuilder();
                configList.All(s =>
                {
                    if (s != null && s.Exists())
                    {
                        cb.AddConfiguration(s);
                    }
                    return(true);
                });
                Configuration = cb.Build();
            }
            else
            {
                Configuration = configList.FirstOrDefault(x => x != null && x.Exists());
                if (Configuration == null)
                {
                    Configuration = section;
                }
            }
        }