创建配置节处理程序时发生错误
我有一个dot.NET 4.0 Web应用程序,其中定义了自定义部分:
....
在web.config文件的末尾我有相应的部分:
.... .....
每次我调用System.Configuration.ConfigurationManager.GetSection("registrations");
我得到以下exception:
为注册创建配置节处理程序时发生错误:给定的程序集名称或代码库无效。 (来自HRESULT的exception:0x80131047)(C:\ … \ web.config第13行)
我也在使用Unity,但不知道这是否与错误有关。
你以前遇到过这个错误吗? 我该如何解决? 我是否需要用其他东西替换IgnoreSectionHandler
?
您缺少App.Config中部分的type属性中的命名空间。 事实上,你也不需要完整的assembly信息。 只有命名空间就足够了
更新1
yourcustomconfigclass config =(yourcustomconfigclass)System.Configuration.ConfigurationManager.GetSection( "registrations");
并且在配置文件中只写
鉴于此app.config:
然后尝试使用:
namespace MyApp { class Program { static void Main(string[] args) { var config = ConfigurationManager.GetSection(MyConfigurationSection.SectionName) as MyConfigurationSection ?? new MyConfigurationSection(); Console.WriteLine(config.MyValue); Console.ReadLine(); } } public class MyConfigurationSection : ConfigurationSection { public const String SectionName = "registrations"; [ConfigurationProperty("myValue")] public String MyValue { get { return (String)this["myValue"]; } set { this["myValue"] = value; } } } }