Reputation: 3858
I have workflow service with an extension that I configuration through a custom BehaviorExtensionElement. Since I need to reuse some configuration properties also in other pieces of my application, I would like to know how it is possible to read the configuration element through the ConfigurationManager.
public class ServiceConfigurationElement : BehaviorExtensionElement
{
public const string RetryDelayKey = "retryDelay";
/// <summary>
/// Creates a behavior extension based on the current configuration settings.
/// </summary>
/// <returns>
/// The behavior extension.
/// </returns>
protected override object CreateBehavior()
{
var behavior = new ServiceConfigurationBehavior
{
RetryDelay = this.CommsRetryDelay
};
return behavior;
}
/// <summary>
/// Gets the type of behavior.
/// </summary>
/// <returns>
/// A <see cref="T:System.Type"/>.
/// </returns>
public override Type BehaviorType
{
get
{
return typeof(ServiceConfigurationBehavior);
}
}
[ConfigurationProperty(RetryDelayKey, IsKey = false, DefaultValue = true)]
public TimeSpan RetryDelay
{
get
{
return (TimeSpan)this[RetryDelayKey];
}
set
{
this[RetryDelayKey] = value;
}
}
}
And the configuration:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceConfiguration retryDelay="00:01:00" />
</behavior>
</serviceBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="serviceConfiguration" type="MyNamespace.ConfigurationElement, MyAssembly"/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
How can I read the RetryDelay property (and others, of course) through the ConfigurationManager?
Thanks
Francesco
Upvotes: 1
Views: 1694
Reputation: 66
ConfigurationManager does not have a property explicitly for the ServiceModel section. Instead, Microsoft provided the ServiceModelSectionGroup (MSDN) which will allow you to retrieve that section to read values.
First you will need to load the configuration file using the various ways to open it on the ConfigurationManager (MSDN). Below I use the OpenMappedExeConfiguration method.
ExeConfigurationFileMap exeConfigurationFileMap = new ExeConfigurationFileMap
{
ExeConfigFilename = Assembly.GetEntryAssembly().Location + ".config"
};
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration( exeConfigurationFileMap, ConfigurationUserLevel.None );
After that, you will need to retrieve the section by doing the following:
ServiceModelSectionGroup serviceModelGroup = ServiceModelSectionGroup.GetSectionGroup( configuration );
From there you can access any of the service model configuration values. I will modify your behavior to the following for an example. Note the named behavior.
<behavior name="Configuration">
<serviceConfiguration retryDelay="00:01:00" />
</behavior>
Once you have the section group, it's just a matter of getting the behavior extensions and iterating through them, like so.
ServiceBehaviorElementCollection serviceBehaviors = serviceModelGroup.Behaviors.ServiceBehaviors;
foreach ( ServiceBehaviorElement behavior in serviceBehaviors )
{
if ( behavior.Name == "Configuration" )
{
ServiceConfigurationElement serviceConfiguration = behavior[ typeof( ServiceConfigurationElement ) ] as ServiceConfigurationElement;
Console.WriteLine( serviceConfiguration.RetryDelay.ToString() );
// do whatever you like here
}
}
You'll see that I used the ServiceBehaviors, but there is another property for the EndpointBehaviors as well. One way to extend this would be to cache the service model section group in a static variable (less disk I/O) and write a few methods to query the various attributes of any extensions you might write in the future.
Hope this helps!
Upvotes: 3