System.Configuration.ConfigurationErrorsException: The default value for
the property has different type than the one of the property...
System.TypeInitializationException: The type initializer for
'Testing.Configuration.ConfigManager' threw an exception. --->
System.Configuration.ConfigurationErrorsException: The default value for
the property 'logLevel' has different type than the one of the property
itself. (C:\Routing\bin\Debug\Routing.vshost.exe.Config line 14)
My application compiles successfully but when I try to debug it throws the
above exception on type and then exits. It seems that my application
configuration file has a problem under the Routers section on "link"
sub-section. Here is my code:
app.config:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="Routers"
type="Energy.Routing.Configuration.RoutingSection, Routing"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<Routers>
<links>
<link name="LAZY" trader="Energy.LAZY.LAZYTrader, Energy.LAZY"
panel="Energy.LAZY.Xaml.LAZYSettings, Energy.LAZY"/>
<link name="FAST" trader="Energy.FAST.FASTTrader, Energy.FAST"
panel="Energy.FAST.Xaml.FASTSettings, Energy.FAST"/>
</links>
</Routers>
</configuration>
RoutingSection.cs:
using System.Configuration;
namespace class Energy.Routing.Configuration
{
internal class RoutingSection : ConfigurationSection
{
private const string ConnectionsKey = "links";
[ConfigurationProperty(ConnectionsKey, IsDefaultCollection = true),
ConfigurationCollection(typeof (ConnectionElementCollection),
AddItemName = "link",
ClearItemsName = "clear", RemoveItemName = "remove")]
public ConnectionElementCollection Connections
{
get { return (ConnectionElementCollection) base["links"]; }
}
}
}
MyConfigManager.cs:
namespace Testing.Configuration
{
using Common;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Reflection;
using System.Web;
using System.Web.Configuration;
using NativeServiceLocator =
Microsoft.Practices.ServiceLocation.ServiceLocator;
public static class MyConfigManager
{
private static readonly Dictionary<Type, ConfigurationSection>
_sections = new Dictionary<Type, ConfigurationSection>();
static MyConfigManager()
{
InnerConfig = (HttpRuntime.AppDomainId == null) ?
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
:
WebConfigurationManager.OpenWebConfiguration(HttpRuntime.AppDomainAppVirtualPath);
Trace.WriteLine("ConfigManager FilePath=" + InnerConfig.FilePath);
Action<ConfigurationSectionCollection> initSections =
delegate(ConfigurationSectionCollection sections)
{
foreach (ConfigurationSection section in sections)
{
if (!_sections.ContainsKey(section.GetType()))
{
_sections.Add(section.GetType(), section);
}
}
};
var configurationSection = GetSection<UnityConfigurationSection>();
if (configurationSection != null)
{
UnityContainer.LoadConfiguration(configurationSection);
}
var locator = new UnityServiceLocator(UnityContainer);
Microsoft.Practices.ServiceLocation.ServiceLocator.SetLocatorProvider(()
=> locator);
}
public static T TryGetService<T>()
{
return IsServiceRegistered<T>() ? GetService<T>() : default(T);
}
public static T GetSection<T>() where T : ConfigurationSection
{
return (T)GetSection(typeof(T));
}
public static ConfigurationSection GetSection(string sectionName)
{
return InnerConfig.GetSection(sectionName);
}
public static T GetSection<T>(string sectionName) where T :
ConfigurationSection
{
return (T)GetSection(sectionName);
}
public static ConfigurationSection GetSection(Type sectionType)
{
return (_sections.ContainsKey(sectionType) ? _sections[sectionType] :
null);
}
public static Configuration InnerConfig
{
get; private set;
}
public static IServiceLocator ServiceLocator
{
get
{
return Microsoft.Practices.ServiceLocation.ServiceLocator.Current;
}
}
public static UnityContainer UnityContainer
{
get;
private set;
}
}
}
connectionElementCollection.cs:
using System.Configuration;
namespace Energy.Routing.Configuration
{
internal class ConnectionElementCollection :
ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ConnectionElement();
}
protected override object GetElementKey(ConfigurationElement
element)
{
var element2 = (ConnectionElement) element;
return element2.Trader;
}
}
}
No comments:
Post a Comment