gpt4 book ai didi

configuration - MEF 插件有自己的配置文件吗?

转载 作者:行者123 更新时间:2023-12-04 12:23:12 25 4
gpt4 key购买 nike

我正在尝试在运行时加载插件并访问它们的配置文件。其配置文件中的配置节映射到派生自 ConfigurationElementCollection、ConfigurationElement 和 ConfigurationSection 的类。插件及其配置文件位于名为“Plugins”的子文件夹中。

问题是我似乎无法加载插件配置数据并将其正确反序列化到各自的类中。

以下是插件 EmailPlugin.dll 的插件配置示例:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="EmailConfigurationSection" type="Foo.Plugins.EmailConfigurationSection, EmailPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true"/>
</configSections>
<EmailConfigurationSection server="192.168.0.10">
<EmailSettings>
<add keyword="ERROR"
sender="error@error.com"
recipients="foo@bar.com, wiki@waki.com"
subject = "Error occurred"
body = "An error was detected"
/>
</EmailSettings>
</EmailConfigurationSection>
</configuration>

我使用以下代码加载它:
    private static System.Configuration.Configuration config = null;
public static System.Configuration.Configuration CurrentConfiguration
{
get
{
if (config == null)
{
Assembly assembly = Assembly.GetAssembly(typeof(EmailPlugin));
string directory = Path.GetDirectoryName(assembly.CodeBase);
string filename = Path.GetFileName(assembly.CodeBase);
string assemblyPath = Path.Combine(directory, filename);
config = ConfigurationManager.OpenExeConfiguration(new Uri(assemblyPath).LocalPath);
}

return config;
}
}

这导致错误:
An error occurred creating the configuration section handler for EmailConfigurationSection: Could not load file or assembly 'EmailPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

我将此添加到配置文件的顶部:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="Plugins"/>
</assemblyBinding>
</runtime>

所以找到了 DLL,但是当我尝试检索它时它没有转换为正确的类:
EmailConfigurationSection defaults = CurrentConfiguration.Sections["EmailConfigurationSection"] as EmailConfigurationSection;

它总是返回空值。我知道它正在查看正确的位置和配置文件,因为我可以使用以下代码检索 XML:
var section = CurrentConfiguration.Sections["EmailConfigurationSection"];
string configXml = section.SectionInformation.GetRawXml();

但是,当我尝试使用以下代码反序列化它时:
var serializer = new XmlSerializer(typeof(EmailConfigurationSection));
object result;

EmailConfigurationSection defaults;
using (TextReader reader = new StringReader(configXml))
{
defaults = (EmailConfigurationSection)serializer.Deserialize(reader);
}

...我得到一个异常(exception):
There was an error reflecting type 'Foo.Plugins.EmailConfigurationSection'.

这是 InnerException 的内容:
You must implement a default accessor on System.Configuration.ConfigurationLockCollection because it inherits from ICollection.

我假设它指的是类 EmailConfigElementCollection,但是该消息没有意义,因为此类确实具有默认访问器:
    public EmailConfigElement this[int index]
{
get
{
return (EmailConfigElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}

我已经在其他项目中成功使用了此代码(即使使用单独的 DLL/配置),但这是我第一次尝试将它与 MEF 一起使用。有谁知道问题是什么,或合适的解决方法?

我正在使用 .NET 4.5

最佳答案

我通过以下修改解决了这个问题:

public static System.Configuration.Configuration CurrentConfiguration
{
get
{
if (config == null)
{
// Added the next bit
AppDomain.CurrentDomain.AssemblyResolve += (o, args) =>
{
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
return loadedAssemblies.Where(asm => asm.FullName == args.Name)
.FirstOrDefault();
};

Assembly assembly = Assembly.GetAssembly(typeof(EmailPlugin));
string directory = Path.GetDirectoryName(assembly.CodeBase);
string filename = Path.GetFileName(assembly.CodeBase);
string assemblyPath = Path.Combine(directory, filename);
config = ConfigurationManager.OpenExeConfiguration(new Uri(assemblyPath).LocalPath);
}

return config;
}
}

我从这个问题中得到了这个:
Custom configuration sections in MEF exporting assemblies .我实际上早先尝试过,但没有成功。

诀窍是我必须将运行时标记移动到 XML 配置的底部:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="EmailConfigurationSection" type="Foo.Plugins.EmailConfigurationSection, EmailPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true"/>
</configSections>
<EmailConfigurationSection server="255.255.255.1">
<EmailSettings>
<clear />
<add keyword="FOO"
sender="foo@foo.com"
recipients="me@you.com"
subject = "Foo occurred"
body = "Hello"
/>
</EmailSettings>
</EmailConfigurationSection>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="Plugins"/>
</assemblyBinding>
</runtime>
</configuration>

关于configuration - MEF 插件有自己的配置文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11954315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com