我正在尝试在运行时加载插件并访问其配置文件。配置文件中的配置节映射到从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;
它总是返回null。我知道它正在查看正确的位置和配置文件,因为我可以使用以下代码检索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);
}
......我得到一个例外:
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 / configs),但这是我第一次尝试将其与MEF一起使用。有谁知道问题是什么,或者是否有合适的解决方法?
我正在使用.NET 4.5
答案 0 :(得分: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>