C# WCF 系统.配置.配置错误异常: 无法识别的元素'ManagedService'



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

System.Reflection.TargetInvocationException:已引发异常通过调用的目标。--->System.Configuration.ConfigurationErrorsException:无法识别元素"ManagedService"。(Service.dll.config第8行)System.Configuration.BaseConfigurationRecord.EEvaluateOne(String[]键,SectionInput输入,布尔值为可信,FactoryRecordfactoryRecord、SectionRecord SectionRecord、Object parentResult)System.Configuration.BaseConfigurationRecord.Eevaluate(FactoryRecordfactoryRecord,SectionRecord SectionRecord,Object parentRequal,布尔型getLkg,布尔型getRuntimeObject,Object&结果,对象&resultRuntimeObject)System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串configKey,布尔getLkg,布尔checkPermission,布尔getRuntimeObject,Boolean requestIsHere,Object&结果,对象&resultRuntimeObject)System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串configKey,布尔getLkg,布尔checkPermission,布尔getRuntimeObject,Boolean requestIsHere,Object&结果,对象&resultRuntimeObject)System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串configKey,布尔getLkg,布尔checkPermission,布尔getRuntimeObject,Boolean requestIsHere,Object&结果,对象&resultRuntimeObject)System.Configuration.BaseConfigurationRecord.GetSection(字符串configKey)System.ConfigurationManager.GetSection(字符串sectionName)。。中的ctor()ManagementService.cs:line 42-内部异常堆栈跟踪结束---在System.RuntimeMethodHandle_InvokeConstructor(IRuntimeMethodInfo方法,Object[]参数,SignatureStruct&签名,运行时类型declaringType)System.Reflection.RuntimeConstructorInfo.IInvoke(BindingFlagsinvokeAttr、Binder绑定器、Object[]参数、CultureInfo区域性)
System.ServiceModel.Description.ServiceDescription.CreateImplementation(类型serviceType)System.ServiceModel.Description.ServiceDescription.GetService(类型serviceType)System.ServiceModel.ServiceHost.CreateDescription(IDictionary `2&已实施的合同)System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollectionbaseAddresses)(类型serviceType,Uri[]baseAddresses)Microsoft.Tools.SvcHost.ServiceHostHelper.CreateServiceHost(类型类型,ServiceKind种类)Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfoinfo)System.Configuration.ConfigurationErrorsException:无法识别元素"ManagedService"。(Service.dll.config第8行)System.Configuration.BaseConfigurationRecord.EEvaluateOne(String[]键,SectionInput输入,布尔值为可信,FactoryRecordfactoryRecord、SectionRecord SectionRecord、Object parentResult)System.Configuration.BaseConfigurationRecord.Eevaluate(FactoryRecordfactoryRecord,SectionRecord SectionRecord,Object parentRequal,布尔型getLkg,布尔型getRuntimeObject,Object&结果,对象&resultRuntimeObject)System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串configKey,布尔getLkg,布尔checkPermission,布尔getRuntimeObject,Boolean requestIsHere,Object&结果,对象&resultRuntimeObject)System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串configKey,布尔getLkg,布尔checkPermission,布尔getRuntimeObject,Boolean requestIsHere,Object&结果,对象&resultRuntimeObject)System.Configuration.BaseConfigurationRecord.GetSectionRecursive(字符串configKey,布尔getLkg,布尔checkPermission,布尔getRuntimeObject,Boolean requestIsHere,Object&结果,对象&resultRuntimeObject)System.Configuration.BaseConfigurationRecord.GetSection(字符串configKey)System.ConfigurationManager.GetSection(字符串sectionName)。。中的ctor()ManagementService.cs:line 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>是继承自ConfigurationElementCollectionMangedServiceCollection

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:的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-pastie.org/private/jkiylqsrkpcdbtfdrajq 中,代码可能更容易查看

我无法编译您的示例,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
{
/// <summary>
/// Base SharePoint 2010 provider contiguration
/// </summary>
[Serializable]
public class SharePoint2010Settings : ConfigurationSection
{
/// <summary>
/// DocumentStorageRoot
/// </summary>
[ConfigurationProperty("SiteUrl", IsRequired = true, DefaultValue = "")]
public string SiteUrl
{
get { return (string)base["SiteUrl"]; }
set { base["SiteUrl"] = value; }
}
/// <summary>
/// TitleProperty
/// </summary>
[ConfigurationProperty("TitleProperty", IsRequired = true, DefaultValue = "Title")]
public string TitleProperty
{
get { return (string)base["TitleProperty"]; }
set { base["TitleProperty"] = value; }
}
/// <summary>
/// ProvideFileAsLink
/// </summary>
[ConfigurationProperty("ProvideFileAsLink", IsRequired = true, DefaultValue = true)]
public bool ProvideFileAsLink
{
get { return (bool)base["ProvideFileAsLink"]; }
set { base["ProvideFileAsLink"] = value; }
}
/// <summary>
/// DocumentCategories
/// </summary>
[ConfigurationProperty("DocumentCategories")]
public SharePointDocumentCategoryCollection DocumentCategories
{
get { return (SharePointDocumentCategoryCollection)base["DocumentCategories"]; }
set { base["DocumentCategories"] = value; }
}
}
/// <summary>
/// Configuration element that holds SharePointDocumentCategory collection
/// </summary>
[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;
}
}
/// <summary>
/// Configuration element that holds information for specific document category
/// </summary>
[Serializable]
public class SharePointDocumentCategory: ConfigurationElement
{
/// <summary>
/// CategoryName
/// </summary>
[ConfigurationProperty("CategoryName", IsRequired = true, DefaultValue = "")]
public string CategoryName
{
get { return (string)base["CategoryName"]; }
set { base["CategoryName"] = value; }
}
/// <summary>
/// FolderName
/// </summary>
[ConfigurationProperty("FolderName", IsRequired = true, DefaultValue = "")]
public string FolderName
{
get { return (string)base["FolderName"]; }
set { base["FolderName"] = value; }
}

/// <summary>
/// TitleProperty
/// </summary>
[ConfigurationProperty("OverwriteFiles", IsRequired = true, DefaultValue = true)]
public bool OverwriteFiles
{
get { return (bool)base["OverwriteFiles"]; }
set { base["OverwriteFiles"] = value; }
}
/// <summary>
/// DocumentCategories
/// </summary>
[ConfigurationProperty("CategoryFields")]
public SharePointCategoryFieldsCollection CategoryFields
{
get { return (SharePointCategoryFieldsCollection)base["CategoryFields"]; }
set { base["CategoryFields"] = value; }
}
}
/// <summary>
/// Configuration element that holds SharePointDocumentCategory collection
/// </summary>
[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;
}
}
/// <summary>
/// Class that determines specific field
/// </summary>
[Serializable]
public class SharePointCategoryField : ConfigurationElement
{
/// <summary>
/// FolderName
/// </summary>
[ConfigurationProperty("FieldName", IsRequired = true, DefaultValue = "")]
public string FieldName
{
get { return (string)base["FieldName"]; }
set { base["FieldName"] = value; }
}
}
}

这是web.config的一部分:

<configSections>
<sectionGroup name="CustomConfiguration">
<section name="SharePoint2010Section" type="GP.Solutions.WF.DocumentProvider.Entities.SharePoint.SharePoint2010Settings,CustomConfiguration" allowDefinition="Everywhere" allowLocation="true"/>
</sectionGroup>
</configSections>
<CustomConfiguration>
<SharePoint2010Section SiteUrl="http://server" TitleProperty="Title" ProvideFileAsLink="false">
<DocumentCategories>
<DocumentCategory CategoryName="Vhodni računi" FolderName="" OverwriteFiles="true">
<CategoryFields>
<CategoryField FieldName="Datum" />
<CategoryField FieldName="Vrednost" />
</CategoryFields>
</DocumentCategory>
<DocumentCategory CategoryName="Zahtevek za dopust" FolderName="" OverwriteFiles="true">
<CategoryFields>
<CategoryField FieldName="Datum od" />
<CategoryField FieldName="Datum do" />
</CategoryFields>
</DocumentCategory>
</DocumentCategories>
</SharePoint2010Section>
</CustomConfiguration>

最新更新