WCF アプリケーションには、.NET で使用するカスタム構成クラスがいくつかありますapp.config
。ただし、WCF サービス ホストから次のスタック トレースを取得しています (WCF サービスのコンストラクターでカスタム構成を取得しようとします)。
System.Reflection.TargetInvocationException: 呼び出しのターゲットによって例外がスローされました。---> 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、 System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject,
System.ServiceModel.Description.ServiceDescription.CreateImplementation (型 serviceType) で System.ServiceModel.Description.ServiceDescription.GetService (型 serviceType) でSystem.ServiceModel.ServiceHost..ctor の (UriSchemeKeyedCollection baseAddresses) (型 serviceType、Uri[] baseAddresses) Microsoft.Tools.SvcHost.ServiceHostHelper.CreateServiceHost (型の種類、ServiceKind 種類) の Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService ( ServiceInfo 情報) System.Configuration.ConfigurationErrorsException: 要素 'ManagedService' を認識できません。(Service.dll.config 行 8) System.Configuration.BaseConfigurationRecord で。
(厄介なスタックトレースで申し訳ありません)。
このエラーに関する多くのチュートリアルやその他の質問をここで見ましたが、提案や解決策はどこにも行きませんでした.
の関連部分は次のとおりです。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;
}
}
このコレクションはManagedService
から継承する を保持します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"]; }
}
}
コードはこのペーストで見やすいかもしれません。pastie.org/private/jkiylqsrklpcdbtfdrajq