Service Fabric的自定义ConfigurationSection实现

时间:2016-11-03 14:27:42

标签: c# configuration azure-service-fabric

Service Fabric中是否有$string = "This is a url from my website: http://www.mysite.com.br and I have a article interesting there, the link is http://www.mysite.com.br/articles/what-is-psychology/205967. I need that the secure url link works too https://www.mysite.com.br/articles/what-is-psychology/205967. the following urls must be valid too: www.mysite.com.br and mysite.com.br"; function urlMySite($string){ $verifyUrl = ''; $urls = array("mysite.com.br"); $text = explode(" ", $string); $alltext = ""; for($i = 0; $i < count($texto); $i++){ foreach ($urls as $value){ $pos = strpos($text[$i], $value); if (!($pos === false)){ $verifyUrl = " <a href='".$text[$i]."' target='_blank'>".$text[$i]."</a> "; if (strpos($verifyUrl, 'http://') !== true) { $verifyUrl = " <a href='http://".$text[$i]."' target='_blank'>".$text[$i]."</a> "; } $alltext .= $verifyUrl; } else { $alltext .= " ".$text[$i]." "; } } } return $alltext; } 的等价物?

我正在尝试将现有应用程序移植到Service Fabric,但我找不到任何有关如何实现配置节的示例。

例如,如果我在System.Configuration.ConfigurationSection

app.config

<configSections> <sectionGroup name="MySection" type="Company.Core.MyConfiguration"> <section name="Watermark" type="System.Configuration.NameValueSectionHandler"/> </sectionGroup> </configSections> <MySection> <Watermark> <add key="FileName" value="some.png" /> </Watermark> </MySection> 的实施:

ConfigurationSection

然后在代码中我可以引用这样的配置:

public class MyConfiguration : ConfigurationSection
{
    private readonly Watermark _watermark = new Watermark();

    public WatermarkConfiguration WatermarkConfiguration
    {
        get
        {
            return new WatermarkConfiguration
                {
                    FileName = _watermark.FileName
                };
        }
    }

    public class Watermark
    {
        private readonly NameValueCollection _watermark = 
            ConfigurationManager.GetSection("MySection/Watermark") as NameValueCollection;

        [ConfigurationProperty("FileName")]
        public string FileName
        {
            get { return _watermark["FileName"]; }
            set { _watermark["FileName"] = value; }
        }
    }
}

public class WatermarkConfiguration
{
    public string FileName { get; set; }
}

我希望有人之前遇到过这种情况,并且可以分享他们实施变化的见解。

1 个答案:

答案 0 :(得分:1)

不,它没有可扩展的ConfigurationSections。服务配置是通过Service Fabric中的配置包完成的 - 基本上是一组版本的配置文件,它是您部署到集群(see here for more info on this structure)的整体服务包的一部分。

配置包中包含您想要的任何格式的配置文件 - XML,JSON,YAML等等。您可以使用一个名为Settings.xml的特殊配置文件。 Service Fabric有C#API,看起来很像System.Configuration类来访问该文件中的设置。

例如,在Settings.xml中提供此配置:

<?xml version="1.0" encoding="utf-8" ?>
<Settings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2011/01/fabric">

  <Section Name="IoTHubConfigInformation">
    <Parameter Name="ConnectionString" Value="blah" />
  </Section>

</Settings>

你会这样做:

string iotHubConnectionString =
            this.Context.CodePackageActivationContext
                .GetConfigurationPackageObject("Config")
                .Settings
                .Sections["IoTHubConfigInformation"]
                .Parameters["ConnectionString"]
                .Value;

但您无法像使用System.Configuration那样定义自己的部分。相反,您必须提供一些包装类。