In a WCF application I have some custom configuration classes for use in app.config
. However, I'm getting the following Stack Trace from the WCF Service Host (It attempts to retrieve the custom configuration in the constructor of the WCF service):
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Configuration.ConfigurationErrorsException: Unrecognized element 'ManagedService'. (Service.dll.config line 8) at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult) at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject) at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) at System.Configuration.BaseConfigurationRecord.GetSection(String configKey) at System.Configuration.ConfigurationManager.GetSection(String sectionName) at ManagementService..ctor() in ManagementService.cs:line 42 --- End of inner exception stack trace --- at System.RuntimeMethodHandle._InvokeConstructor(IRuntimeMethodInfo method, Object[] args, SignatureStruct& signature, RuntimeType declaringType) at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.ServiceModel.Description.ServiceDescription.CreateImplementation(Type serviceType) at System.ServiceModel.Description.ServiceDescription.GetService(Type serviceType) at System.ServiceModel.ServiceHost.CreateDescription(IDictionary`2& implementedContracts) at System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection baseAddresses) at System.ServiceModel.ServiceHost..ctor(Type serviceType, Uri[] baseAddresses) at Microsoft.Tools.SvcHost.ServiceHostHelper.CreateServiceHost(Type type, ServiceKind kind) at Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info) System.Configuration.ConfigurationErrorsException: Unrecognized element 'ManagedService'. (Service.dll.config line 8) at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult) at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject) at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) at System.Configuration.BaseConfigurationRecord.GetSection(String configKey) at System.Configuration.ConfigurationManager.GetSection(String sectionName) at ManagementService..ctor() in ManagementService.cs:line 42
(Sorry for the nasty stack trace).
I've looked at tons of tutorials and other questions here about this error and none of the suggestions or solutions have gone anywhere.
Here's the relevant portion of the app.config
<configSections>
<section name="ManagedServices" type="Service.Configuration.ManagedServicesSection, Service, Version=1.0.0.0, Culture=neutral " allowLocation="true" allowDefinition="Everywhere" restartOnExternalChanges="false" />
</configSections>
<ManagedServices>
<services>
<ManagedService serviceAssembly="Service" serviceType="Service.Runnables.HostManagerRunner" identifier="HostManager" priority="0">
<clear />
</ManagedService>
<ManagedService serviceAssembly="Service" serviceType="Service.Runnables.TimeoutMonitor" identifier="TimeoutMonitor" priority="0">
<add key="timeoutLength" value="30" />
<add key="runInterval" value="30" />
</ManagedService>
</services>
</ManagedServices>
Basically, this WCF service is used to manage other services which are loaded and started dynamically (informed via this configuration) on start up.
<ManagedServices>
is from the ManagedServicesSection
which inherits from ConfigurationSection
public class ManagedServicesSection : ConfigurationSection
{
[ConfigurationProperty("services", IsDefaultCollection = true)]
public ManagedServiceCollection ServiceCollection
{
get { return (ManagedServiceCollection) base["services"]; }
}
}
As you can see from this, <services>
is a MangedServiceCollection
which inherits from ConfigurationElementCollection
public class ManagedServiceCollection : ConfigurationElementCollection
{
public ManagedServiceCollection()
{
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
public ManagedService this[int index]
{
get { return BaseGet(index) as ManagedService; }
set
{
if (BaseGet(index) != null)
BaseRemoveAt(index);
BaseAdd(index, value);
}
}
public ManagedService this[string name]
{
get { return BaseGet(name) as ManagedService; }
set
{
if (BaseGet(name) != null)
BaseRemove(name);
BaseAdd(value);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new ManagedService();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ManagedService)element).Identifier;
}
}
This collection holds ManagedService
s which inherit from ConfigurationElement
:
public class ManagedService : ConfigurationElement
{
[ConfigurationProperty("serviceAssembly", IsRequired = true)]
public string ServiceAssembly
{
get { return (string) this["serviceAssembly"]; }
set { this["serviceAssembly"] = value; }
}
[ConfigurationProperty("serviceType", DefaultValue = "IRunnable", IsRequired = true)]
public string ServiceType
{
get { return (string) this["serviceType"]; }
set { this["serviceType"] = value; }
}
[ConfigurationProperty("identifier", IsRequired = true, IsKey = true)]
public string Identifier
{
get { return (string) this["identifier"]; }
set { this["identifier"] = value; }
}
[ConfigurationProperty("priority", DefaultValue = 0, IsRequired = false)]
public int Priority
{
get { return (int) this["priority"]; }
set { this["priority"] = value; }
}
[ConfigurationProperty("serviceParameters", IsDefaultCollection = true)]
public ServiceParameterCollection ServiceParameters
{
get { return (ServiceParameterCollection)base["serviceParamters"]; }
}
}
The code may be easier to look at in this pastie pastie.org/private/jkiylqsrklpcdbtfdrajq