【发布时间】:2023-03-15 06:49:01
【问题描述】:
在 WCF 应用程序中,我有一些自定义配置类用于app.config。但是,我从 WCF 服务主机获得了以下堆栈跟踪(它尝试在 WCF 服务的构造函数中检索自定义配置):
System.Reflection.TargetInvocationException:已抛出异常 通过调用的目标。 ---> System.Configuration.ConfigurationErrorsException:无法识别 元素“托管服务”。 (Service.dll.config 第 8 行)在 System.Configuration.BaseConfigurationRecord.EvaluateOne(字符串 [] 键、SectionInput 输入、布尔 isTrusted、FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult) 在 System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& 结果, Object& 结果运行时间对象)在 System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串 configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject、布尔型 requestIsHere、Object& 结果、Object& 结果运行时间对象)在 System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串 configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject、布尔型 requestIsHere、Object& 结果、Object& 结果运行时间对象)在 System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串 configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject、布尔型 requestIsHere、Object& 结果、Object& 结果运行时间对象)在 System.Configuration.BaseConfigurationRecord.GetSection(字符串 配置密钥)在 System.Configuration.ConfigurationManager.GetSection(字符串 sectionName) 在 ManagementService..ctor() 中 ManagementService.cs:line 42 --- 内部异常堆栈跟踪结束 --- 在 System.RuntimeMethodHandle._InvokeConstructor(IRuntimeMethodInfo 方法、Object[] 参数、SignatureStruct& 签名、RuntimeType 声明类型)在 System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr、Binder binder、Object[] 参数、CultureInfo 文化)
在 System.ServiceModel.Description.ServiceDescription.CreateImplementation(类型 服务类型)在 System.ServiceModel.Description.ServiceDescription.GetService(类型 服务类型)在 System.ServiceModel.ServiceHost.CreateDescription(IDictionary`2& 实施合同)在 System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection baseAddresses) 在 System.ServiceModel.ServiceHost..ctor(Type serviceType, Uri[] baseAddresses) 在 Microsoft.Tools.SvcHost.ServiceHostHelper.CreateServiceHost(类型类型, ServiceKind 种类)在 Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo 信息)System.Configuration.ConfigurationErrorsException:无法识别 元素“托管服务”。 (Service.dll.config 第 8 行)在 System.Configuration.BaseConfigurationRecord.EvaluateOne(字符串 [] 键、SectionInput 输入、布尔 isTrusted、FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult) 在 System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& 结果, Object& 结果运行时间对象)在 System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串 configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject、布尔型 requestIsHere、Object& 结果、Object& 结果运行时间对象)在 System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串 configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject、布尔型 requestIsHere、Object& 结果、Object& 结果运行时间对象)在 System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串 configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject、布尔型 requestIsHere、Object& 结果、Object& 结果运行时间对象)在 System.Configuration.BaseConfigurationRecord.GetSection(字符串 配置密钥)在 System.Configuration.ConfigurationManager.GetSection(字符串 sectionName) 在 ManagementService..ctor() 中 ManagementService.cs:第 42 行
(对不起,讨厌的堆栈跟踪)。
我在这里查看了大量关于此错误的教程和其他问题,但没有任何建议或解决方案出现在任何地方。
这是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>
基本上,此 WCF 服务用于管理在启动时动态加载和启动(通过此配置通知)的其他服务。
<ManagedServices> 来自 ManagedServicesSection,它继承自 ConfigurationSection
public class ManagedServicesSection : ConfigurationSection
{
[ConfigurationProperty("services", IsDefaultCollection = true)]
public ManagedServiceCollection ServiceCollection
{
get { return (ManagedServiceCollection) base["services"]; }
}
}
从这里可以看出,<services> 是一个MangedServiceCollection,它继承自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;
}
}
此集合包含继承自 ConfigurationElement 的 ManagedServices:
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"]; }
}
}
这段代码在pastie.org/private/jkiylqsrklpcdbtfdrajq 中可能更容易查看
【问题讨论】:
标签: c# exception configuration