【问题标题】:Access BehaviorExtensionElement through configuration settings通过配置设置访问 BehaviorExtensionElement
【发布时间】:2012-08-27 03:41:17
【问题描述】:

我的工作流服务带有我通过自定义 BehaviorExtensionElement 配置的扩展。 由于我还需要在我的应用程序的其他部分中重用一些配置属性,我想知道如何通过 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;
        }
    }
}

以及配置:

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

如何通过 ConfigurationManager 读取 RetryDelay 属性(当然还有其他属性)?

谢谢

弗朗西斯科

【问题讨论】:

    标签: wcf configuration workflow-foundation


    【解决方案1】:

    ConfigurationManager 没有明确用于 ServiceModel 部分的属性。相反,Microsoft 提供了 ServiceModelSectionGroup (MSDN),它允许您检索该部分以读取值。

    首先,您需要使用各种方式加载配置文件,以便在 ConfigurationManager (MSDN) 上打开它。下面我使用 OpenMappedExeConfiguration 方法。

    ExeConfigurationFileMap exeConfigurationFileMap = new ExeConfigurationFileMap
    {
        ExeConfigFilename = Assembly.GetEntryAssembly().Location + ".config"
    };
    Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration( exeConfigurationFileMap, ConfigurationUserLevel.None );
    

    之后,您需要通过执行以下操作来检索该部分:

    ServiceModelSectionGroup serviceModelGroup = ServiceModelSectionGroup.GetSectionGroup( configuration );
    

    您可以从那里访问任何服务模型配置值。我会将您的行为修改为以下示例。注意命名的行为。

    <behavior name="Configuration">
        <serviceConfiguration retryDelay="00:01:00" />
    </behavior>
    

    一旦你有了部分组,就只需要获取行为扩展并遍历它们,就像这样。

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

    您会看到我使用了 ServiceBehaviors,但 EndpointBehaviors 还有另一个属性。扩展它的一种方法是将服务模型部分组缓存在一个静态变量中(更少的磁盘 I/O),并编写一些方法来查询您将来可能编写的任何扩展的各种属性。

    希望这会有所帮助!

    【讨论】:

    • 正是我想要的
    猜你喜欢
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 2016-03-16
    相关资源
    最近更新 更多