Service Fabric 的自定义 ConfigurationSection 实现
Custom ConfigurationSection implementation for Service Fabric
Service Fabric 中是否有等同于 System.Configuration.ConfigurationSection
的内容?
我正在尝试将现有应用程序移植到 Service Fabric,但找不到任何有关如何实现配置部分的示例。
例如,如果我在 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; }
}
然后在代码中我可以像这样引用这个配置:
var myConfiguration = new MyConfiguration();
var watermarkName = myConfiguration.WatermarkConfiguration.FileName;
我希望有人以前遇到过这个问题,并且可以分享他们对实施变化的见解。
不,它没有可扩展的 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 那样定义自己的部分。相反,您必须提供一些包装器 类.
Service Fabric 中是否有等同于 System.Configuration.ConfigurationSection
的内容?
我正在尝试将现有应用程序移植到 Service Fabric,但找不到任何有关如何实现配置部分的示例。
例如,如果我在 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; }
}
然后在代码中我可以像这样引用这个配置:
var myConfiguration = new MyConfiguration();
var watermarkName = myConfiguration.WatermarkConfiguration.FileName;
我希望有人以前遇到过这个问题,并且可以分享他们对实施变化的见解。
不,它没有可扩展的 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 那样定义自己的部分。相反,您必须提供一些包装器 类.