C # WCF System.Configuration.ConfigurationErrorsException: elemento non riconosciuto ‘ManagedService’

In un’applicazione WCF ho alcune classi di configurazione personalizzate da utilizzare in app.config . Tuttavia, sto ottenendo il seguente Stack Trace dall’host del servizio WCF (tenta di recuperare la configurazione personalizzata nel costruttore del servizio WCF):

System.Reflection.TargetInvocationException: l’eccezione di una chiamata è stata lanciata dalla destinazione. —> System.Configuration.ConfigurationErrorsException: elemento ‘ManagedService’ non riconosciuto. (Service.dll.config line 8) su System.Configuration.BaseConfigurationRecord.EvaluateOne (String [] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult) su System.Configuration.BaseConfigurationRecord.Evaluate (FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object & result, Object & resultRuntimeObject) su System.Configuration.BaseConfigurationRecord.GetSectionRecursive (String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object & result, Object & resultRuntimeObject) sul sistema .Configuration.BaseConfigurationRecord.GetSectionRecursive (String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object & result, Object & resultRuntimeObject) su System.Configuration.BaseConfigurationRecord.GetSectionRecursive (String configKey, Boolean getLkg, Boolean chec kPermission, booleano getRuntimeObject, booleano requestIsHere, Object & result, Object & resultRuntimeObject) su System.Configuration.BaseConfigurationRecord.GetSection (String configKey) su System.Configuration.ConfigurationManager.GetSection (String sectionName) su ManagementService..ctor () in ManagementService.cs: riga 42 — Fine della traccia dello stack di eccezioni interne — a System.RuntimeMethodHandle._InvokeConstructor (metodo IRuntimeMethodInfo, Object [] args, SignatureStruct & signature, RuntimeType declaringType) in System.Reflection.RuntimeConstructorInfo.Invoke (BindingFlags invokeAttr, Binder binder, Parametri dell’object [], cultura CultureInfo)
in System.ServiceModel.Description.ServiceDescription.CreateImplementation (Type serviceType) in System.ServiceModel.Description.ServiceDescription.GetService (Type serviceType) in System.ServiceModel.ServiceHost.CreateDescription (IDictionary`2 & authorizedContracts) su System.ServiceModel.ServiceHostBase.InitializeDescription (UriSchemeKeyedCollection baseAddresses) su System.ServiceModel.ServiceHost..ctor (Digitare serviceType, Uri [] baseAddresses) su Microsoft.Tools.SvcHost.ServiceHostHelper.CreateServiceHost (Tipo di tipo, tipo ServiceKind) su Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService ( Informazioni ServiceInfo) System.Configuration.ConfigurationErrorsException: elemento ‘ManagedService’ non riconosciuto. (Service.dll.config line 8) su System.Configuration.BaseConfigurationRecord.EvaluateOne (String [] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult) su System.Configuration.BaseConfigurationRecord.Evaluate (FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object & result, Object & resultRuntimeObject) su System.Configuration.BaseConfigurationRecord.GetSectionRecursive (String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object & result, Object & resultRuntimeObject) sul sistema .Configuration.BaseConfigurationRecord.GetSectionRecursive (String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object & result, Object & resultRuntimeObject) su System.Configuration.BaseConfigurationRecord.GetSectionRecursive (String configKey, Boolean getLkg, Boolean chec kPermission, booleano getRuntimeObject, booleano requestIsHere, Object & result, Object & resultRuntimeObject) su System.Configuration.BaseConfigurationRecord.GetSection (String configKey) su System.Configuration.ConfigurationManager.GetSection (String sectionName) su ManagementService..ctor () in ManagementService.cs: linea 42

(Ci scusiamo per la ctriggers traccia dello stack).

Ho esaminato tonnellate di tutorial e altre domande qui su questo errore e nessuno dei suggerimenti o delle soluzioni è andato da nessuna parte.

Ecco la parte pertinente di app.config

  

Fondamentalmente, questo servizio WCF viene utilizzato per gestire altri servizi che vengono caricati e avviati dynamicmente (informati tramite questa configurazione) all’avvio.

proviene dalla ManagedServicesSection che eredita da ConfigurationSection

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

Come puoi vedere da questo, è un MangedServiceCollection che eredita da 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; } } 

Questa raccolta contiene i ManagedService che ereditano da 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"]; } } } 

Il codice potrebbe essere più semplice da consultare in questo pastie pastie.org/private/jkiylqsrklpcdbtfdrajq

Non ho potuto soddisfare il tuo campione, mancava ServiceParameterCollection … quindi ti ho preparato il mio samle di lavoro. Eccoci qui…

Prima consente di creare classi di configurazione, prendere nota del parametro ConfigurationIccection di AddItemName (penso che questo sia ciò che ti manca nel tuo codice):

 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; } } } } 

Ed ecco la parte web.config: