C#WCF System.Configuration.ConfigurationErrorsException:无法识别的元素’ManagedService’

在WCF应用程序中,我有一些自定义配置类可供app.config使用。 但是,我从WCF服务主机获取以下堆栈跟踪(它尝试在WCF服务的构造函数中检索自定义配置):

System.Reflection.TargetInvocationException:调用目标抛出了exception。 —> System.Configuration.ConfigurationErrorsException:无法识别的元素’ManagedService’。 (Service.dll.config第8行)在System.Configuration.BaseConfigurationRecord.EvaluateOne(String []键,SectionInput输入,Boolean isTrusted,FactoryRecord factoryRecord,SectionRecord sectionRecord,Object parentResult)处于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 System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey,Boolean getLkg,Boolean chec).Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey,Boolean getLkg,Boolean checkPermission,Boolean getRuntimeObject,Boolean requestIsHere,Object&result,Object&resultRuntimeObject) 位于ManagementService.cs中ManagementService..ctor()的System.Configuration.ConfigurationManager.GetSection(String sectionName)的System.Configuration.BaseConfigurationRecord.GetSection(String configKey)中的kPermission,Boolean getRuntimeObject,Boolean requestIsHere,Object&result,Object&resultRuntimeObject):第42行—内部exception堆栈跟踪的结束—在System.Reflection.RuntimeConstructorInfo.Invoke的System.RuntimeMethodHandle._InvokeConstructor(IRuntimeMethodInfo方法,Object [] args,SignatureStruct&signature,RuntimeType declaringType)中(BindingFlags invokeAttr,Binder binder, Object []参数,CultureInfo文化)
System.ServiceModel.ServiceHostBase.InitializeDescription上System.ServiceModel.ServiceHost.CreateDescription(IDictionary`2&implementedContracts)的System.ServiceModel.Description.ServiceDescription.GetService(Type serviceType)上的System.ServiceModel.Description.ServiceDescription.CreateImplementation(Type serviceType) (UriSchemeKeylection baseAddresses)位于Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService的Microsoft.Tools.SvcHost.ServiceHostHelper.CreateServiceHost(Type type,ServiceKind kind)的System.ServiceModel.ServiceHost..ctor(类型serviceType,Uri [] baseAddresses) ServiceInfo info)System.Configuration.ConfigurationErrorsException:无法识别的元素’ManagedService’。 (Service.dll.config第8行)在System.Configuration.BaseConfigurationRecord.EvaluateOne(String []键,SectionInput输入,Boolean isTrusted,FactoryRecord factoryRecord,SectionRecord sectionRecord,Object parentResult)处于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 System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey,Boolean getLkg,Boolean chec).Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey,Boolean getLkg,Boolean checkPermission,Boolean getRuntimeObject,Boolean requestIsHere,Object&result,Object&resultRuntimeObject) 位于ManagementService.cs中ManagementService..ctor()的System.Configuration.ConfigurationManager.GetSection(String sectionName)的System.Configuration.BaseConfigurationRecord.GetSection(String configKey)中的kPermission,Boolean getRuntimeObject,Boolean requestIsHere,Object&result,Object&resultRuntimeObject):第42行

(抱歉讨厌的堆栈跟踪)。

我在这里看了很多关于这个错误的教程和其他问题,并没有提出任何建议或解决方案。

这是app.config的相关部分

  

基本上,此WCF服务用于管理在启动时动态加载和启动的其他服务(通过此配置通知)。

来自ManagedServicesSection ,它inheritance自ConfigurationSection

 public class ManagedServicesSection : ConfigurationSection { [ConfigurationProperty("services", IsDefaultCollection = true)] public ManagedServiceCollection ServiceCollection { get { return (ManagedServiceCollection) base["services"]; } } } 

从中可以看出, 是一个MangedServiceCollection ,它inheritance自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; } } 

此集合包含inheritance自ConfigurationElement ManagedService

 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

我无法完成您的示例,ServiceParameterCollection丢失了…所以我已经为您准备了我的工作样本。 开始了…

首先让我们创建配置类,注意AddItemName ConfigurationCollection参数(我认为这是你在代码中缺少的):

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; namespace GP.Solutions.WF.DocumentProvider.Entities.SharePoint { ///  /// Base SharePoint 2010 provider contiguration ///  [Serializable] public class SharePoint2010Settings : ConfigurationSection { ///  /// DocumentStorageRoot ///  [ConfigurationProperty("SiteUrl", IsRequired = true, DefaultValue = "")] public string SiteUrl { get { return (string)base["SiteUrl"]; } set { base["SiteUrl"] = value; } } ///  /// TitleProperty ///  [ConfigurationProperty("TitleProperty", IsRequired = true, DefaultValue = "Title")] public string TitleProperty { get { return (string)base["TitleProperty"]; } set { base["TitleProperty"] = value; } } ///  /// ProvideFileAsLink ///  [ConfigurationProperty("ProvideFileAsLink", IsRequired = true, DefaultValue = true)] public bool ProvideFileAsLink { get { return (bool)base["ProvideFileAsLink"]; } set { base["ProvideFileAsLink"] = value; } } ///  /// DocumentCategories ///  [ConfigurationProperty("DocumentCategories")] public SharePointDocumentCategoryCollection DocumentCategories { get { return (SharePointDocumentCategoryCollection)base["DocumentCategories"]; } set { base["DocumentCategories"] = value; } } } ///  /// Configuration element that holds SharePointDocumentCategory collection ///  [ConfigurationCollection(typeof(SharePointDocumentCategory), AddItemName = "DocumentCategory", CollectionType = ConfigurationElementCollectionType.BasicMap)] public class SharePointDocumentCategoryCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new SharePointDocumentCategory(); } protected override object GetElementKey(ConfigurationElement element) { return ((SharePointDocumentCategory)element).CategoryName; } } ///  /// Configuration element that holds information for specific document category ///  [Serializable] public class SharePointDocumentCategory: ConfigurationElement { ///  /// CategoryName ///  [ConfigurationProperty("CategoryName", IsRequired = true, DefaultValue = "")] public string CategoryName { get { return (string)base["CategoryName"]; } set { base["CategoryName"] = value; } } ///  /// FolderName ///  [ConfigurationProperty("FolderName", IsRequired = true, DefaultValue = "")] public string FolderName { get { return (string)base["FolderName"]; } set { base["FolderName"] = value; } } ///  /// TitleProperty ///  [ConfigurationProperty("OverwriteFiles", IsRequired = true, DefaultValue = true)] public bool OverwriteFiles { get { return (bool)base["OverwriteFiles"]; } set { base["OverwriteFiles"] = value; } } ///  /// DocumentCategories ///  [ConfigurationProperty("CategoryFields")] public SharePointCategoryFieldsCollection CategoryFields { get { return (SharePointCategoryFieldsCollection)base["CategoryFields"]; } set { base["CategoryFields"] = value; } } } ///  /// Configuration element that holds SharePointDocumentCategory collection ///  [ConfigurationCollection(typeof(SharePointDocumentCategory), AddItemName = "CategoryField", CollectionType = ConfigurationElementCollectionType.BasicMap)] public class SharePointCategoryFieldsCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new SharePointCategoryField(); } protected override object GetElementKey(ConfigurationElement element) { return ((SharePointCategoryField)element).FieldName; } } ///  /// Class that determines specific field ///  [Serializable] public class SharePointCategoryField : ConfigurationElement { ///  /// FolderName ///  [ConfigurationProperty("FieldName", IsRequired = true, DefaultValue = "")] public string FieldName { get { return (string)base["FieldName"]; } set { base["FieldName"] = value; } } } } 

这是web.config部分: